1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vivait\Voter\Dispatcher; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Psr\Log\NullLogger; |
7
|
|
|
use Symfony\Component\EventDispatcher\Event; |
8
|
|
|
use Vivait\Voter\Model\ActionInterface; |
9
|
|
|
use Vivait\Voter\Model\EntityEvent; |
10
|
|
|
use Vivait\Voter\Model\VoterInterface; |
11
|
|
|
|
12
|
|
|
class ActionDispatcher implements ActionDispatcherInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var VoterInterface |
16
|
|
|
*/ |
17
|
|
|
private $voter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \SplObjectStorage|ActionInterface[] |
21
|
|
|
*/ |
22
|
|
|
private $actions; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var LoggerInterface |
26
|
|
|
*/ |
27
|
|
|
private $logger; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $name; |
33
|
|
|
|
34
|
|
|
public function __construct($name, VoterInterface $voter, array $actions = [], LoggerInterface $logger = null) |
35
|
|
|
{ |
36
|
|
|
$this->name = $name; |
37
|
|
|
$this->voter = $voter; |
38
|
|
|
$this->actions = new \SplObjectStorage(); |
39
|
|
|
|
40
|
|
|
$this->addActions($actions); |
41
|
|
|
|
42
|
|
|
if (!$logger) { |
43
|
|
|
$logger = new NullLogger(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->logger = $logger; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function performFromEvent(Event $event, $eventName) { |
50
|
|
|
$this->logger->info(sprintf('Calling inspection "%s" for event "%s"', $this->name, $eventName)); |
51
|
|
|
|
52
|
|
|
if ($event instanceOf EntityEvent) { |
53
|
|
|
$this->perform($event->getEntities()); |
54
|
|
|
} |
55
|
|
|
else { |
56
|
|
|
$this->perform([]); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function perform($entity) |
61
|
|
|
{ |
62
|
|
|
$entity_map = (is_array($entity)) ? |
63
|
|
|
array_map( |
64
|
|
|
function ($obj) { |
65
|
|
|
return get_class($obj); |
66
|
|
|
}, |
67
|
|
|
$entity |
68
|
|
|
) : [ |
69
|
|
|
get_class($entity) |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
if (!$this->voter->supports($entity_map)) { |
73
|
|
|
throw new \RuntimeException( |
74
|
|
|
sprintf('Inspection "%s" with voter "%s" does not support entities: %s', $this->name, get_class($this->voter), implode(', ', $entity_map)) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$result = $this->voter->result($entity); |
79
|
|
|
$this->logger->debug( |
80
|
|
|
sprintf('Inspection "%s" with voter "%s" returned result: %s', $this->name, get_class($this->voter), $result ? 'true' : 'false') |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
if ($result) { |
84
|
|
|
$this->performActions($entity); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $actions |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function addActions(array $actions) |
93
|
|
|
{ |
94
|
|
|
foreach ($actions as $action) { |
95
|
|
|
$this->addAction($action); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param ActionInterface $action |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function addAction(ActionInterface $action) |
106
|
|
|
{ |
107
|
|
|
$this->actions->attach($action, $action->requires()); |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return \SplObjectStorage|ActionInterface[] |
114
|
|
|
*/ |
115
|
|
|
public function getActions() |
116
|
|
|
{ |
117
|
|
|
return $this->actions; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param ActionInterface $action |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function removeAction(ActionInterface $action) |
125
|
|
|
{ |
126
|
|
|
$this->actions->detach($action); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $entity |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
private function performActions($entity) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
foreach ($this->actions as $action) { |
138
|
|
|
$result = $action->perform($entity); |
139
|
|
|
|
140
|
|
|
$this->logger->info( |
141
|
|
|
sprintf('Performing action "%s" with result: %s', get_class($action), $result ? 'true' : 'false') |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
if ($result === false) { |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Gets voter |
154
|
|
|
* @return VoterInterface |
155
|
|
|
*/ |
156
|
|
|
public function getVoter() |
157
|
|
|
{ |
158
|
|
|
return $this->voter; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Sets voter |
163
|
|
|
* @param \Vivait\Voter\Model\VoterInterface $voter |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
public function setVoter($voter) |
167
|
|
|
{ |
168
|
|
|
$this->voter = $voter; |
169
|
|
|
|
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Gets logger |
175
|
|
|
* @return LoggerInterface |
176
|
|
|
*/ |
177
|
|
|
public function getLogger() |
178
|
|
|
{ |
179
|
|
|
return $this->logger; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Sets logger |
184
|
|
|
* @param LoggerInterface $logger |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function setLogger($logger) |
188
|
|
|
{ |
189
|
|
|
$this->logger = $logger; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Gets name |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function getName() |
199
|
|
|
{ |
200
|
|
|
return $this->name; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Sets name |
205
|
|
|
* @param string $name |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
|
|
public function setName($name) |
209
|
|
|
{ |
210
|
|
|
$this->name = $name; |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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.