1 | <?php |
||
20 | abstract class AbstractCallbackMediator extends AbstractCallbackList implements ProxyableWhen |
||
21 | { |
||
22 | /** Implementations are tasked to provide a list of appropriate request identifiers */ |
||
23 | abstract protected function identifyRequested(Request $request, $params); |
||
24 | /** Based on each of the identified particulars a list of provisiens must be supplied */ |
||
25 | abstract protected function considerProvisions($requested); |
||
26 | /** If an agreement wasreached to allow positive notification and preparation */ |
||
27 | abstract protected function notifyApproved($requested, $provided, Request $request, $params); |
||
28 | /** If declined to apply the nescessary notifications and preparations */ |
||
29 | abstract protected function notifyDeclined($requested, $provided, Request $request, $params); |
||
30 | |||
31 | /** |
||
32 | * Mediate the authorization or ultimately service denial based on client's |
||
33 | * request and implementation's provisioning. |
||
34 | * The outcome will trigger the appropriate notification mehtods to allow |
||
35 | * for appropriate header configuration if nescesary. |
||
36 | */ |
||
37 | 40 | public function when(Request $request, $params) |
|
38 | { |
||
39 | if (true == |
||
|
|||
40 | 40 | ($decision = $this->mediate($requested, $provided, $request, $params))) { |
|
41 | 39 | $this->notifyApproved($requested, $provided, $request, $params); |
|
42 | } else { |
||
43 | 2 | $this->notifyDeclined($requested, $provided, $request, $params); |
|
44 | } |
||
45 | |||
46 | 40 | return $decision; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Implementing classes will be asked to identify the request list and for |
||
51 | * each item in the list of requests identified a list of provisions must be |
||
52 | * supplied. |
||
53 | * The implementation gets to authorize against each possibility or the |
||
54 | * request will be declined if no satisfactory requirements are reached. |
||
55 | **/ |
||
56 | 40 | private function mediate(&$requested, &$provided, Request $request, $params) |
|
57 | { |
||
58 | 40 | if (is_array($requests = $this->identifyRequested($request, $params))) { |
|
59 | 40 | foreach ($requests as $requested) { |
|
60 | 40 | if (is_array($provisions = $this->considerProvisions($requested))) { |
|
61 | 40 | foreach ($provisions as $provided) { |
|
62 | 40 | if ($this->authorize($requested, $provided, $request)) { |
|
63 | 39 | return true; |
|
64 | } |
||
65 | } |
||
66 | } else { |
||
67 | 1 | throw new UnexpectedValueException('Provisions must be an array of 0 to many.'); |
|
68 | } |
||
69 | } |
||
70 | } else { |
||
71 | 1 | throw new UnexpectedValueException('Requests must be an array of 0 to many.'); |
|
72 | } |
||
73 | |||
74 | 2 | return false; |
|
75 | } |
||
76 | |||
77 | /** Affirm apprval or decline the r */ |
||
78 | 11 | protected function authorize($requested, $provided) |
|
82 | } |
||
83 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.