1 | <?php |
||
19 | class MultiStrategy implements StrategyInterface |
||
20 | { |
||
21 | /** @var StrategyInterface[] */ |
||
22 | private $strategies; |
||
23 | |||
24 | /** @var Role[] */ |
||
25 | private $roles= []; |
||
26 | |||
27 | /** |
||
28 | * MultiStrategy constructor. |
||
29 | * |
||
30 | * @param StrategyInterface[] $strategies List of strategies to be applied. |
||
31 | */ |
||
32 | 2 | public function __construct(array $strategies) |
|
36 | |||
37 | /** |
||
38 | * Applies the defined strategies on the provided request. |
||
39 | * |
||
40 | * @param Request $request request to handle |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | public function apply(Request $request) |
||
45 | { |
||
46 | $exceptions = []; |
||
47 | |||
48 | foreach ($this->strategies as $strategy) { |
||
49 | try { |
||
50 | $name = $strategy->apply($request); |
||
51 | $this->roles = $strategy->getRoles(); |
||
52 | |||
53 | if ($name && $strategy->stopPropagation()) { |
||
54 | return $name; |
||
55 | } |
||
56 | } catch (\InvalidArgumentException $e) { |
||
57 | $exceptions[] = $e; |
||
58 | } |
||
59 | } |
||
60 | |||
61 | throw new \InvalidArgumentException($exceptions[0]->getMessage(), $exceptions[0]); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Decider to stop other strategies running after from being considered. |
||
66 | * |
||
67 | * @return boolean |
||
68 | */ |
||
69 | public function stopPropagation() |
||
73 | |||
74 | /** |
||
75 | * Provides the list of registered roles. |
||
76 | * |
||
77 | * @return Role[] |
||
78 | */ |
||
79 | public function getRoles() |
||
83 | } |
||
84 |