|
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('count', $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('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('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('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
|
|
|
'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('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
|
|
|
|