1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the BEAR.QueryRepository package. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
6
|
|
|
*/ |
7
|
|
|
namespace BEAR\QueryRepository; |
8
|
|
|
|
9
|
|
|
use BEAR\RepositoryModule\Annotation\AbstractCommand; |
10
|
|
|
use BEAR\RepositoryModule\Annotation\Purge; |
11
|
|
|
use BEAR\RepositoryModule\Annotation\Refresh; |
12
|
|
|
use BEAR\Resource\ResourceInterface; |
13
|
|
|
use BEAR\Resource\ResourceObject; |
14
|
|
|
use BEAR\Resource\Uri; |
15
|
|
|
use Ray\Aop\MethodInvocation; |
16
|
|
|
|
17
|
|
|
class RefreshAnnotatedCommand implements CommandInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var QueryRepositoryInterface |
21
|
|
|
*/ |
22
|
|
|
private $repository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ResourceInterface |
26
|
|
|
*/ |
27
|
|
|
private $resource; |
28
|
|
|
|
29
|
11 |
|
public function __construct( |
30
|
|
|
QueryRepositoryInterface $repository, |
31
|
|
|
ResourceInterface $resource |
32
|
|
|
) { |
33
|
11 |
|
$this->repository = $repository; |
34
|
11 |
|
$this->resource = $resource; |
35
|
11 |
|
} |
36
|
|
|
|
37
|
7 |
|
public function command(MethodInvocation $invocation, ResourceObject $ro) |
38
|
|
|
{ |
39
|
7 |
|
$annotations = $invocation->getMethod()->getAnnotations(); |
|
|
|
|
40
|
7 |
|
foreach ($annotations as $annotation) { |
41
|
2 |
|
$this->request($ro, $annotation); |
42
|
|
|
} |
43
|
7 |
|
} |
44
|
|
|
|
45
|
2 |
|
private function getUri(ResourceObject $resourceObject, AbstractCommand $annotation) : string |
46
|
|
|
{ |
47
|
2 |
|
$body = \is_array($resourceObject->body) ? $resourceObject->body : []; |
48
|
2 |
|
$query = $body + $resourceObject->uri->query; |
49
|
2 |
|
$uri = uri_template($annotation->uri, $query); |
50
|
|
|
|
51
|
2 |
|
return $uri; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
private function request(ResourceObject $resourceObject, $annotation) |
55
|
|
|
{ |
56
|
2 |
|
if (! $annotation instanceof AbstractCommand) { |
|
|
|
|
57
|
2 |
|
return; |
58
|
|
|
} |
59
|
2 |
|
$uri = new Uri($this->getUri($resourceObject, $annotation)); |
60
|
2 |
|
if ($annotation instanceof Purge) { |
|
|
|
|
61
|
2 |
|
$this->repository->purge($uri); |
62
|
|
|
} |
63
|
2 |
|
if ($annotation instanceof Refresh) { |
|
|
|
|
64
|
2 |
|
$this->repository->purge($uri); |
65
|
2 |
|
$ro = $this->resource->get->uri($uri)->eager->request(); |
|
|
|
|
66
|
2 |
|
$this->repository->put($ro); |
67
|
|
|
} |
68
|
2 |
|
} |
69
|
|
|
} |
70
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: