PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Graph/Input/AbstractInput.php

https://github.com/Proudio-Interactive/phpUnderControl
PHP | 368 lines | 125 code | 27 blank | 216 comment | 4 complexity | 34c206f4a0064a2c173684e49c199e20 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of phpUnderControl.
  4. *
  5. * PHP Version 5.2.0
  6. *
  7. * Copyright (c) 2007-2010, Manuel Pichler <mapi@phpundercontrol.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category QualityAssurance
  40. * @package Graph
  41. * @subpackage Input
  42. * @author Manuel Pichler <mapi@phpundercontrol.org>
  43. * @copyright 2007-2010 Manuel Pichler. All rights reserved.
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://www.phpundercontrol.org/
  47. */
  48. /**
  49. * Abstract base class for graph input data.
  50. *
  51. * This class provides the main xpath and data extraction logic for conrete
  52. * implementations. This means an extending class must only define the xpath
  53. * rules and the required chart type.
  54. *
  55. * <code>
  56. * class MyChartInput extends phpucAbstractInput
  57. * {
  58. * public function __construct()
  59. * {
  60. * parent::__construct(
  61. * 'Coding Violations',
  62. * '06-coding-violations',
  63. * phpucChartI::TYPE_LINE
  64. * );
  65. *
  66. * $this->addRule(
  67. * new phpucInputRule(
  68. * 'PHP CodeSniffer',
  69. * '/cruisecontrol/checkstyle/file/error',
  70. * self::MODE_COUNT
  71. * )
  72. * );
  73. * }
  74. * }
  75. * </code>
  76. *
  77. * @category QualityAssurance
  78. * @package Graph
  79. * @subpackage Input
  80. * @author Manuel Pichler <mapi@phpundercontrol.org>
  81. * @copyright 2007-2010 Manuel Pichler. All rights reserved.
  82. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  83. * @version Release: @package_version@
  84. * @link http://www.phpundercontrol.org/
  85. *
  86. * @property-read string $title The human readable data title.
  87. * @property-read integer $type The output chart type.
  88. * @property-read array(string=>mixed) $data The extracted log file data.
  89. * @property-read string $yAxisLabel An optional label for the y-axis.
  90. * @property-read string $xAxisLabel An optional label for the x-axis.
  91. * @property-read array(string=>int) $graphDims The Graph-Dimensions
  92. */
  93. abstract class phpucAbstractInput implements phpucInputI
  94. {
  95. /**
  96. * The human readable input type title.
  97. *
  98. * @var string
  99. */
  100. protected $title = null;
  101. /**
  102. * An optional label for the y-axis.
  103. *
  104. * @var string
  105. */
  106. protected $yAxisLabel = '';
  107. /**
  108. * An optional label for the x-axis.
  109. *
  110. * @var string
  111. */
  112. protected $xAxisLabel = '';
  113. /**
  114. * Dimensions of Graph to export
  115. *
  116. * @var array(string=>int)
  117. */
  118. protected $graphDims = array();
  119. /**
  120. * The output image file name.
  121. *
  122. * @var string
  123. */
  124. private $fileName = null;
  125. /**
  126. * The output chart type.
  127. *
  128. * @var integer
  129. */
  130. private $type = null;
  131. /**
  132. * The extracted log file data.
  133. *
  134. * @var array(string=>array)
  135. */
  136. private $data = array();
  137. /**
  138. * List of input rules.
  139. *
  140. * @var array(phpucInputRule)
  141. */
  142. private $rules = array();
  143. /**
  144. * Constructs a new input type implementation.
  145. *
  146. * @param string $title The human readable input type title.
  147. * @param string $fileName The output image file name.
  148. * @param integer $type The output chart type.
  149. *
  150. * @throws InvalidArgumentException If the given type is unknown.
  151. */
  152. public function __construct( $title, $fileName, $type )
  153. {
  154. $this->title = $title;
  155. $this->fileName = $fileName;
  156. $this->graphDims= array('width' => 390,
  157. 'height' => 250,);
  158. $expected = array(
  159. phpucChartI::TYPE_PIE,
  160. phpucChartI::TYPE_LINE,
  161. phpucChartI::TYPE_DOT,
  162. phpucChartI::TYPE_TIME
  163. );
  164. if ( !in_array( $type, $expected ) )
  165. {
  166. throw new InvalidArgumentException( 'Invalid input type given.' );
  167. }
  168. $this->type = $type;
  169. }
  170. /**
  171. * Magic property getter method.
  172. *
  173. * @param string $name The property name.
  174. *
  175. * @return mixed
  176. * @throws OutOfRangeException If the requested property doesn't exist or
  177. * is writonly.
  178. * @ignore
  179. */
  180. public function __get( $name )
  181. {
  182. switch ( $name )
  183. {
  184. case 'type':
  185. case 'title':
  186. case 'rawData':
  187. case 'fileName':
  188. case 'yAxisLabel':
  189. case 'xAxisLabel':
  190. case 'graphDims':
  191. return $this->$name;
  192. case 'data':
  193. return $this->postProcessLog( $this->data );
  194. default:
  195. throw new OutOfRangeException(
  196. sprintf( 'Unknown or writonly property $%s.', $name )
  197. );
  198. break;
  199. }
  200. }
  201. /**
  202. * Magic property setter method.
  203. *
  204. * @param string $name The property name.
  205. * @param mixed $value The property value.
  206. *
  207. * @return void
  208. * @throws OutOfRangeException If the requested property doesn't exist or
  209. * is readonly.
  210. * @throws InvalidArgumentException If the given value has an unexpected
  211. * format or an invalid data type.
  212. * @ignore
  213. */
  214. public function __set( $name, $value )
  215. {
  216. throw new OutOfRangeException(
  217. sprintf( 'Unknown or readonly property $%s.', $name )
  218. );
  219. }
  220. /**
  221. * Evaluates all defined xpath rules against the given DOMXPath instance.
  222. *
  223. * @param DOMXPath $xpath The context dom xpath object.
  224. *
  225. * @return void
  226. */
  227. public function processLog( DOMXPath $xpath )
  228. {
  229. foreach ( $this->rules as $rule )
  230. {
  231. $label = $rule->label;
  232. if ( !isset( $this->data[$label] ) )
  233. {
  234. $this->data[$label] = array();
  235. }
  236. $nodeList = $xpath->query( $rule->xpath );
  237. switch ( $rule->mode )
  238. {
  239. case self::MODE_COUNT:
  240. $this->data[$label][] = $this->processLogCount( $nodeList );
  241. break;
  242. case self::MODE_SUM:
  243. $this->data[$label][] = $this->processLogSum( $nodeList );
  244. break;
  245. case self::MODE_VALUE:
  246. $this->data[$label][] = $this->processLogValue( $nodeList );
  247. break;
  248. case self::MODE_LIST:
  249. $this->data[$label][] = implode(
  250. ';', $this->processLogList( $nodeList )
  251. );
  252. break;
  253. }
  254. }
  255. }
  256. /**
  257. * Calculates the sum of all node values.
  258. *
  259. * @param DOMNodeList $nodeList Fetched node list.
  260. *
  261. * @return integer
  262. */
  263. protected function processLogSum( DOMNodeList $nodeList )
  264. {
  265. $sum = 0;
  266. foreach ( $nodeList as $node )
  267. {
  268. $sum += (float) $node->nodeValue;
  269. }
  270. return $sum;
  271. }
  272. /**
  273. * Counts all nodes in the node list
  274. *
  275. * @param DOMNodeList $nodeList Fetched node list.
  276. *
  277. * @return integer
  278. */
  279. protected function processLogCount( DOMNodeList $nodeList )
  280. {
  281. return $nodeList->length;
  282. }
  283. /**
  284. * Creates a concated string with all node values.
  285. *
  286. * @param DOMNodeList $nodeList Fetched node list.
  287. *
  288. * @return string
  289. */
  290. protected function processLogValue( DOMNodeList $nodeList )
  291. {
  292. $value = '';
  293. foreach ( $nodeList as $node )
  294. {
  295. $value .= $node->nodeValue;
  296. }
  297. return $value;
  298. }
  299. /**
  300. * Creates a list of metric values
  301. *
  302. * @param DOMNodeList $nodeList Fetched node list.
  303. *
  304. * @return array
  305. */
  306. protected function processLogList( DOMNodeList $nodeList )
  307. {
  308. $logList = array();
  309. foreach ( $nodeList as $node )
  310. {
  311. $logList[] = $node->nodeValue;
  312. }
  313. return $logList;
  314. }
  315. /**
  316. * Post processes the fetched data.
  317. *
  318. * Concrete implementations can overwrite this this method to post process
  319. * the fetched data before it is given to the graph object. This can be very
  320. * usefull in all cases where logs don't have the required format.
  321. *
  322. * @param array(string=>array) $logs Fetched log data.
  323. *
  324. * @return array(string=>mixed)
  325. */
  326. protected function postProcessLog( array $logs )
  327. {
  328. return $logs;
  329. }
  330. /**
  331. * Adds a xpath rule to this input object.
  332. *
  333. * @param phpucInputRule $rule The rule instance
  334. *
  335. * @return void
  336. */
  337. protected function addRule( phpucInputRule $rule )
  338. {
  339. $this->rules[] = $rule;
  340. }
  341. }