1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ReportBundle package |
5
|
|
|
* |
6
|
|
|
* (c) symball <http://simonball.me> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE file |
9
|
|
|
* that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symball\ReportBundle\Service; |
13
|
|
|
|
14
|
|
|
use Symball\ReportBundle\Service\ReportBuilder; |
15
|
|
|
use Symball\ReportBundle\Interfaces\PatternInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The report pattern service acts as a front-end for predefined workflows |
19
|
|
|
* using a plug-in architecture |
20
|
|
|
* |
21
|
|
|
* @author Simon Ball <simonball at simonball dot me> |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
class ReportPattern |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
private $patterns = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Add an extra patter object to the stack for later use |
31
|
|
|
* |
32
|
|
|
* @param PatternInterface $pattern |
33
|
|
|
* @param string $alias |
34
|
|
|
*/ |
35
|
|
|
public function addPattern(PatternInterface $pattern, $alias) |
36
|
|
|
{ |
37
|
|
|
$this->patterns[$alias] = $pattern; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* A front-end for running pattern operations that are loaded on to the stack |
42
|
|
|
* |
43
|
|
|
* @param string $alias |
44
|
|
|
* @param ReportBuilder $context |
45
|
|
|
* @return boolean Success condition |
46
|
|
|
* @throws \Exception when pattern not available |
47
|
|
|
*/ |
48
|
|
|
public function run($alias, ReportBuilder &$context) |
49
|
|
|
{ |
50
|
|
|
if (isset($this->patterns[$alias])) { |
51
|
|
|
$this->patterns[$alias]->run($context); |
52
|
|
|
|
53
|
|
|
return true; |
54
|
|
|
} else { |
55
|
|
|
throw new \Exception('Report pattern is not registered: ' . $alias); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get a simple array list of the currently loaded pattern objects |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function getPatternsLoaded() |
65
|
|
|
{ |
66
|
|
|
return array_keys($this->patterns); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.