1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Respect\Rest package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Respect\Rest\Routines; |
10
|
|
|
|
11
|
|
|
use Respect\Rest\Request; |
12
|
|
|
use UnexpectedValueException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Mediates the callback selection process when choosing the appropriate |
16
|
|
|
* callback based on the request. Decisions are deligated to the implementation |
17
|
|
|
* classes. |
18
|
|
|
* @author Nick Lombard <[email protected]> |
19
|
|
|
*/ |
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
|
40 |
|
if (true == |
|
|
|
|
40
|
40 |
|
($decision = $this->mediate($requested, $provided, $request, $params))) { |
41
|
39 |
|
$this->notifyApproved($requested, $provided, $request, $params); |
42
|
39 |
|
} 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
|
8 |
|
} |
66
|
2 |
|
} else { |
67
|
1 |
|
throw new UnexpectedValueException('Provisions must be an array of 0 to many.'); |
68
|
|
|
} |
69
|
2 |
|
} |
70
|
2 |
|
} 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) |
79
|
|
|
{ |
80
|
11 |
|
return $requested == $provided; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.