These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Admingenerator\GeneratorBundle\Builder\Admin; |
||
4 | |||
5 | use Admingenerator\GeneratorBundle\Generator\Action; |
||
6 | |||
7 | /** |
||
8 | * This builder generates php for custom actions |
||
9 | * @author Piotr Gołębiewski <[email protected]> |
||
10 | */ |
||
11 | class ActionsBuilder extends BaseBuilder |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $batchActions = null; |
||
17 | |||
18 | /** |
||
19 | * (non-PHPdoc) |
||
20 | * @see Admingenerator\GeneratorBundle\Builder.BaseBuilder::getYamlKey() |
||
21 | */ |
||
22 | public function getYamlKey() |
||
23 | { |
||
24 | return 'actions'; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * (non-PHPdoc) |
||
29 | * @see \Admingenerator\GeneratorBundle\Builder\BaseBuilder::getVariables() |
||
30 | */ |
||
31 | public function getVariables() |
||
32 | { |
||
33 | if (!$this->variables->count()) { |
||
34 | return array(); |
||
35 | } |
||
36 | |||
37 | // If credentials are not globally defined, |
||
38 | // check if an action have credentials |
||
39 | if (null === $this->getVariable('credentials')) { |
||
40 | $this->variables->set('credentials', false); |
||
41 | foreach (array_merge(array_values($this->getObjectActions()), array_values($this->getBatchActions())) as $action) { |
||
42 | if ($action->getCredentials()) { |
||
43 | $this->variables->set('credentials', true); |
||
44 | break; |
||
45 | } |
||
46 | } |
||
47 | } |
||
48 | |||
49 | return parent::getVariables(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Return a list of batch action from list.batch_actions |
||
54 | * @return array |
||
55 | */ |
||
56 | public function getBatchActions() |
||
57 | { |
||
58 | if (null === $this->batchActions) { |
||
59 | $this->batchActions = array(); |
||
60 | $this->findBatchActions(); |
||
61 | } |
||
62 | |||
63 | return $this->batchActions; |
||
64 | } |
||
65 | |||
66 | View Code Duplication | protected function setUserBatchActionConfiguration(Action $action) |
|
0 ignored issues
–
show
|
|||
67 | { |
||
68 | $batchActions = $this->getVariable('batch_actions', array()); |
||
69 | $builderOptions = is_array($batchActions) && array_key_exists($action->getName(), $batchActions) |
||
70 | ? $batchActions[$action->getName()] |
||
71 | : array(); |
||
72 | |||
73 | $globalOptions = $this->getGenerator()->getFromYaml( |
||
74 | 'params.batch_actions.'.$action->getName(), array() |
||
75 | ); |
||
76 | |||
77 | if (null !== $builderOptions) { |
||
78 | foreach ($builderOptions as $option => $value) { |
||
79 | $action->setProperty($option, $value); |
||
80 | } |
||
81 | } elseif (null !== $globalOptions) { |
||
82 | foreach ($globalOptions as $option => $value) { |
||
83 | $action->setProperty($option, $value); |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | |||
88 | protected function addBatchAction(Action $action) |
||
89 | { |
||
90 | $this->batchActions[$action->getName()] = $action; |
||
91 | } |
||
92 | |||
93 | View Code Duplication | protected function findBatchActions() |
|
94 | { |
||
95 | $batchActions = $this->getVariable('batch_actions', array()); |
||
96 | |||
97 | foreach ($batchActions as $actionName => $actionParams) { |
||
98 | $action = $this->findBatchAction($actionName); |
||
99 | if (!$action) { |
||
100 | $action = new Action($actionName); |
||
101 | } |
||
102 | |||
103 | if ($globalCredentials = $this->getGenerator()->getFromYaml('params.credentials')) { |
||
104 | // If generator is globally protected by credentials |
||
105 | // batch actions are also protected |
||
106 | $action->setCredentials($globalCredentials); |
||
107 | } |
||
108 | |||
109 | $this->setUserBatchActionConfiguration($action); |
||
110 | $this->addBatchAction($action); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 |
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.