Completed
Pull Request — master (#14)
by Pavel
28:57
created

TestApi   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 119
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createApi() 0 4 1
A count() 0 6 1
A create() 0 6 1
A find() 0 14 2
A patch() 0 6 1
A search() 0 14 1
A remove() 0 6 1
A getClient() 0 4 1
A getMetadata() 0 4 1
A setEntityCache() 0 4 1
A getMethod() 0 4 1
1
<?php
2
3
namespace Bankiru\Api\Doctrine\Test;
4
5
use Bankiru\Api\Doctrine\ApiFactory\StaticApiFactoryInterface;
6
use Bankiru\Api\Doctrine\Cache\EntityCacheAwareInterface;
7
use Bankiru\Api\Doctrine\Cache\VoidEntityCache;
8
use Bankiru\Api\Doctrine\EntityDataCacheInterface;
9
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
10
use Bankiru\Api\Doctrine\Rpc\CrudsApiInterface;
11
use Bankiru\Api\Doctrine\Rpc\RpcRequest;
12
use ScayTrase\Api\Rpc\RpcClientInterface;
13
14
final class TestApi implements CrudsApiInterface, StaticApiFactoryInterface, EntityCacheAwareInterface
15
{
16
    /** @var RpcClientInterface */
17
    private $client;
18
    /** @var ApiMetadata */
19
    private $metadata;
20
    /** @var EntityDataCacheInterface */
21
    private $cache;
22
23
    /**
24
     * TestApi constructor.
25
     *
26
     * @param RpcClientInterface $client
27
     * @param ApiMetadata        $metadata
28
     */
29
    public function __construct(RpcClientInterface $client, ApiMetadata $metadata)
30
    {
31
        $this->client   = $client;
32
        $this->metadata = $metadata;
33
        $this->cache    = new VoidEntityCache($metadata);
34
    }
35
36
    public static function createApi(RpcClientInterface $client, ApiMetadata $metadata)
37
    {
38
        return new static($client, $metadata);
39
    }
40
41
    /** {@inheritdoc} */
42
    public function count(array $criteria)
43
    {
44
        $request = new RpcRequest($this->getMethod('count'), ['criteria' => $criteria]);
45
46
        return (int)$this->client->invoke($request)->getResponse($request)->getBody();
47
    }
48
49
    /** {@inheritdoc} */
50
    public function create(array $data)
51
    {
52
        $request = new RpcRequest($this->getMethod('create'), $data);
53
54
        return $this->client->invoke($request)->getResponse($request)->getBody();
55
    }
56
57
    /** {@inheritdoc} */
58
    public function find(array $identifier)
59
    {
60
        $body = $this->cache->get($identifier);
61
62
        if (null !== $body) {
63
            return $body;
64
        }
65
66
        $request = new RpcRequest($this->getMethod('find'), $identifier);
67
        $body    = $this->client->invoke($request)->getResponse($request)->getBody();
68
        $this->cache->set($identifier, $body);
69
70
        return $body;
71
    }
72
73
    /** {@inheritdoc} */
74
    public function patch(array $identifier, array $data, array $fields)
75
    {
76
        $request = new RpcRequest($this->getMethod('patch'), array_intersect_key($data, array_flip($fields)));
77
78
        return $this->client->invoke($request)->getResponse($request)->isSuccessful();
79
    }
80
81
    /** {@inheritdoc} */
82
    public function search(array $criteria = [], array $orderBy = null, $limit = null, $offset = null)
83
    {
84
        $request = new RpcRequest(
85
            $this->getMethod('search'),
86
            [
87
                'criteria' => $criteria,
88
                'order'    => $orderBy,
89
                'limit'    => $limit,
90
                'offset'   => $offset,
91
            ]
92
        );
93
94
        return new \ArrayIterator($this->client->invoke($request)->getResponse($request)->getBody());
95
    }
96
97
    /** {@inheritdoc} */
98
    public function remove(array $identifier)
99
    {
100
        $request = new RpcRequest($this->getMethod('remove'), $identifier);
101
102
        return $this->client->invoke($request)->getResponse($request)->isSuccessful();
103
    }
104
105
    /** @return RpcClientInterface */
106
    public function getClient()
107
    {
108
        return $this->client;
109
    }
110
111
    /** @return ApiMetadata */
112
    public function getMetadata()
113
    {
114
        return $this->metadata;
115
    }
116
117
    /** {@inheritdoc} */
118
    public function setEntityCache(EntityDataCacheInterface $cache)
119
    {
120
        $this->cache = $cache;
121
    }
122
123
    /**
124
     * @param string $method
125
     *
126
     * @return string
127
     */
128
    private function getMethod($method)
129
    {
130
        return $this->metadata->getMethodContainer()->getMethod($method);
131
    }
132
}
133