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 ScayTrase\Api\Rpc\RpcClientInterface; |
12
|
|
|
|
13
|
|
|
final class TestApi implements CrudsApiInterface, EntityCacheAwareInterface, StaticApiFactoryInterface |
14
|
|
|
{ |
15
|
|
|
/** @var RpcClientInterface */ |
16
|
|
|
private $client; |
17
|
|
|
/** @var ApiMetadata */ |
18
|
|
|
private $metadata; |
19
|
|
|
/** @var EntityDataCacheInterface */ |
20
|
|
|
private $cache; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* TestApi constructor. |
24
|
|
|
* |
25
|
|
|
* @param RpcClientInterface $client |
26
|
|
|
* @param ApiMetadata $metadata |
27
|
|
|
*/ |
28
|
|
|
public function __construct(RpcClientInterface $client, ApiMetadata $metadata) |
29
|
|
|
{ |
30
|
|
|
$this->client = $client; |
31
|
|
|
$this->metadata = $metadata; |
32
|
|
|
$this->cache = new VoidEntityCache($metadata); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** {@inheritdoc} */ |
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 RpcRequestMock($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 RpcRequestMock($this->getMethod('create'), $data); |
53
|
|
|
|
54
|
|
|
$id = $this->client->invoke($request)->getResponse($request)->getBody(); |
55
|
|
|
|
56
|
|
|
return $this->getMetadata()->isIdentifierNatural() ? null : $id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** {@inheritdoc} */ |
60
|
|
|
public function find(array $identifier) |
61
|
|
|
{ |
62
|
|
|
$body = $this->cache->get($identifier); |
63
|
|
|
|
64
|
|
|
if (null !== $body) { |
65
|
|
|
return $body; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$request = new RpcRequestMock($this->getMethod('find'), $identifier); |
69
|
|
|
$body = $this->client->invoke($request)->getResponse($request)->getBody(); |
70
|
|
|
$this->cache->set($identifier, $body); |
71
|
|
|
|
72
|
|
|
return $body; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** {@inheritdoc} */ |
76
|
|
|
public function patch(array $identifier, array $patch, array $data) |
77
|
|
|
{ |
78
|
|
|
$request = new RpcRequestMock($this->getMethod('patch'), $patch); |
79
|
|
|
|
80
|
|
|
return $this->client->invoke($request)->getResponse($request)->isSuccessful(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** {@inheritdoc} */ |
84
|
|
|
public function search(array $criteria = [], array $orderBy = null, $limit = null, $offset = null) |
85
|
|
|
{ |
86
|
|
|
$request = new RpcRequestMock( |
87
|
|
|
$this->getMethod('search'), |
88
|
|
|
[ |
89
|
|
|
'criteria' => $criteria, |
90
|
|
|
'order' => $orderBy, |
91
|
|
|
'limit' => $limit, |
92
|
|
|
'offset' => $offset, |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
return new \ArrayIterator($this->client->invoke($request)->getResponse($request)->getBody()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** {@inheritdoc} */ |
100
|
|
|
public function remove(array $identifier) |
101
|
|
|
{ |
102
|
|
|
$request = new RpcRequestMock($this->getMethod('remove'), $identifier); |
103
|
|
|
|
104
|
|
|
return $this->client->invoke($request)->getResponse($request)->isSuccessful(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** @return RpcClientInterface */ |
108
|
|
|
public function getClient() |
109
|
|
|
{ |
110
|
|
|
return $this->client; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** @return ApiMetadata */ |
114
|
|
|
public function getMetadata() |
115
|
|
|
{ |
116
|
|
|
return $this->metadata; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** {@inheritdoc} */ |
120
|
|
|
public function setEntityCache(EntityDataCacheInterface $cache) |
121
|
|
|
{ |
122
|
|
|
$this->cache = $cache; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $method |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
private function getMethod($method) |
131
|
|
|
{ |
132
|
|
|
return $this->metadata->getMethodContainer()->getMethod($method); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|