Completed
Push — spike ( a372d1...20d8a1 )
by Akihito
04:14
created

RefreshSameCommand::command()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 19
ccs 12
cts 13
cp 0.9231
rs 9.4285
cc 3
eloc 12
nc 2
nop 2
crap 3.004
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\QueryRepository\Exception\UnmatchedQuery;
10
use BEAR\Resource\ResourceObject;
11
use Ray\Aop\MethodInvocation;
12
13
class RefreshSameCommand implements CommandInterface
14
{
15
    /**
16
     * @var QueryRepositoryInterface
17
     */
18
    private $repository;
19
20 11
    public function __construct(QueryRepositoryInterface $repository)
21
    {
22 11
        $this->repository = $repository;
23 11
    }
24
25 8
    public function command(MethodInvocation $invocation, ResourceObject $ro)
26
    {
27 8
        $method = $invocation->getMethod()->getName();
0 ignored issues
show
Bug introduced by
Consider using $invocation->getMethod()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
28 8
        if ($method === 'onGet' || $method === 'onPost') {
29
            return;
30
        }
31 8
        unset($invocation);
32 8
        $onGet = [$ro, 'onGet'];
33 8
        $getQuery = $this->getQuery($ro);
34 7
        $delUri = clone $ro->uri;
35 7
        $delUri->query = $getQuery;
36
37
        // delete data in repository
38 7
        $this->repository->purge($delUri);
39
40
        // GET for re-generate (in interceptor)
41 7
        $ro->uri->query = $getQuery;
42 7
        \call_user_func_array($onGet, $getQuery);
43 7
    }
44
45 8
    private function getQuery(ResourceObject $resourceObject) : array
46
    {
47 8
        $refParameters = (new \ReflectionMethod($resourceObject, 'onGet'))->getParameters();
48 8
        $getQuery = [];
49 8
        $query = $resourceObject->uri->query;
50 8
        foreach ($refParameters as $parameter) {
51 8
            if (! isset($query[$parameter->name])) {
52 1
                throw new UnmatchedQuery($resourceObject->uri->method . ' ' . (string) $resourceObject->uri);
53
            }
54 8
            $getQuery[$parameter->name] = $query[$parameter->name];
55
        }
56
57 7
        return $getQuery;
58
    }
59
}
60