1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Rpc; |
4
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\Exception\ApiCallException; |
6
|
|
|
use Bankiru\Api\Doctrine\Mapping\ApiMetadata; |
7
|
|
|
use ScayTrase\Api\Rpc\RpcClientInterface; |
8
|
|
|
use ScayTrase\Api\Rpc\RpcRequestInterface; |
9
|
|
|
|
10
|
|
|
/** @internal */ |
11
|
|
|
abstract class SingleRequestApi implements Counter, Searcher, Finder |
12
|
|
|
{ |
13
|
|
|
/** {@inheritdoc} */ |
14
|
|
View Code Duplication |
public function count(RpcClientInterface $client, ApiMetadata $metadata, array $parameters) |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$request = $this->createCountRequest($metadata, $parameters); |
17
|
|
|
$response = $client->invoke($request)->getResponse($request); |
18
|
|
|
|
19
|
|
|
if (!$response->isSuccessful()) { |
20
|
|
|
throw ApiCallException::callFailed($response); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return (int)$response->getBody(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** {@inheritdoc} */ |
27
|
|
View Code Duplication |
public function find(RpcClientInterface $client, ApiMetadata $metadata, array $identifier) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$request = $this->createFindRequest($metadata, $identifier); |
30
|
|
|
$response = $client->invoke([$request])->getResponse($request); |
31
|
|
|
|
32
|
|
|
if (!$response->isSuccessful()) { |
33
|
|
|
throw ApiCallException::callFailed($response); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $response->getBody(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** {@inheritdoc} */ |
40
|
|
View Code Duplication |
public function search(RpcClientInterface $client, ApiMetadata $metadata, array $parameters) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$request = $this->createSearchRequest($metadata, $parameters); |
43
|
|
|
$response = $client->invoke($request)->getResponse($request); |
44
|
|
|
|
45
|
|
|
if (!$response->isSuccessful()) { |
46
|
|
|
throw ApiCallException::callFailed($response); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return new \ArrayIterator($response->getBody()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ApiMetadata $metadata |
54
|
|
|
* @param array $criteria |
55
|
|
|
* |
56
|
|
|
* @return RpcRequestInterface |
57
|
|
|
*/ |
58
|
|
|
abstract protected function createCountRequest(ApiMetadata $metadata, array $criteria); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ApiMetadata $metadata |
62
|
|
|
* @param array $identifier |
63
|
|
|
* |
64
|
|
|
* @return RpcRequestInterface |
65
|
|
|
*/ |
66
|
|
|
abstract protected function createFindRequest(ApiMetadata $metadata, array $identifier); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param ApiMetadata $metadata |
70
|
|
|
* @param array $parameters |
71
|
|
|
* |
72
|
|
|
* @return RpcRequestInterface |
73
|
|
|
*/ |
74
|
|
|
abstract protected function createSearchRequest(ApiMetadata $metadata, array $parameters); |
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.