Skip to content

Commit 267c88c

Browse files
committed
Merge pull request #11 from scaytrase/feature/fixes-and-simplification
Fixes and simplification
2 parents 3059848 + 0149155 commit 267c88c

File tree

9 files changed

+65
-52
lines changed

9 files changed

+65
-52
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
composer.lock
22
composer.phar
33
vendor/
4-
.idea/
54

65
build/
76
target/

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/9706918a-39d4-4822-8e25-d0a01182b10b/big.png)](https://insight.sensiolabs.com/projects/9706918a-39d4-4822-8e25-d0a01182b10b)
2+
3+
# RPC Library
4+
5+
## JSON-RPC Implementation

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"autoload": {
2323
"psr-4": {
24-
"": "src/"
24+
"ScayTrase\\Api\\": "src/ScayTrase/Api/"
2525
}
2626
},
2727
"suggest": {

phpmd.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@
5555
<rule ref="rulesets/unusedcode.xml/UnusedPrivateField"/>
5656
<rule ref="rulesets/unusedcode.xml/UnusedLocalVariable"/>
5757
<rule ref="rulesets/unusedcode.xml/UnusedPrivateMethod"/>
58-
</ruleset>
58+
</ruleset>

src/ScayTrase/Api/JsonRpc/JsonRpcResponseCollection.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,4 @@ public function getResponse(RpcRequestInterface $request)
131131

132132
return $this->hashedResponses[spl_object_hash($request)];
133133
}
134-
135-
/** {@inheritdoc} */
136-
public function count()
137-
{
138-
$this->sync();
139-
return count($this->responses);
140-
}
141134
}

src/ScayTrase/Api/Rpc/Decorators/CacheableResponseCollection.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ public function getResponse(RpcRequestInterface $request)
7070
return $item->get();
7171
}
7272

73-
/** {@inheritdoc} */
74-
public function count()
75-
{
76-
return count($this->items) + $this->proxiedCollection->count();
77-
}
78-
7973
/** {@inheritdoc} */
8074
public function getIterator()
8175
{

src/ScayTrase/Api/Rpc/Decorators/LazyResponseCollection.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,6 @@ private function init()
4848
$this->initialized = true;
4949
}
5050

51-
/** {@inheritdoc} */
52-
public function count()
53-
{
54-
if (!$this->initialized) {
55-
$this->init();
56-
}
57-
58-
return $this->collection->count();
59-
}
60-
6151
public function append(RpcRequestInterface $request)
6252
{
6353
if ($this->isFrozen()) {

src/ScayTrase/Api/Rpc/Decorators/LoggableResponseCollection.php

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
use Psr\Log\LoggerInterface;
1212
use ScayTrase\Api\Rpc\ResponseCollectionInterface;
1313
use ScayTrase\Api\Rpc\RpcRequestInterface;
14+
use ScayTrase\Api\Rpc\RpcResponseInterface;
1415

1516
final class LoggableResponseCollection implements \IteratorAggregate, ResponseCollectionInterface
1617
{
1718
/** @var LoggerInterface */
1819
private $logger;
1920
/** @var ResponseCollectionInterface */
2021
private $decoratedCollection;
22+
/** @var string[] */
23+
private $loggedResponses = [];
2124

2225
/**
2326
* LoggableResponseCollection constructor.
@@ -35,43 +38,72 @@ public function __construct(ResponseCollectionInterface $decoratedCollection, Lo
3538
public function getResponse(RpcRequestInterface $request)
3639
{
3740
$response = $this->decoratedCollection->getResponse($request);
41+
$this->logResponseWithRequest($request, $response);
42+
43+
return $response;
44+
}
45+
46+
/** {@inheritdoc} */
47+
public function getIterator()
48+
{
49+
foreach ($this->decoratedCollection as $response) {
50+
$this->logResponse($response);
51+
yield $response;
52+
}
53+
}
54+
55+
/**
56+
* @param RpcRequestInterface $request
57+
* @param RpcResponseInterface $response
58+
*/
59+
private function logResponseWithRequest(RpcRequestInterface $request, RpcResponseInterface $response)
60+
{
61+
$hash = spl_object_hash($response);
62+
if (in_array($hash, $this->loggedResponses, true)) {
63+
return;
64+
}
3865

39-
$this->logger->debug(
40-
sprintf(
41-
'%s Response for RPC method "%s" is %s',
42-
spl_object_hash($request),
43-
$request->getMethod(),
44-
$response->isSuccessful() ? 'Successful' : 'Failed'
45-
),
46-
json_decode(json_encode($response->getBody()), true)
47-
);
4866
if ($response->isSuccessful()) {
67+
$this->logger->info(
68+
sprintf('Method "%s" call was successful', $request->getMethod()),
69+
['request_hash' => spl_object_hash($request)]
70+
);
4971
$this->logger->debug(
50-
sprintf('%s Response for RPC method "%s"', spl_object_hash($request), $request->getMethod()),
51-
json_decode(json_encode($response->getBody()), true)
72+
sprintf("Resposne:\n%s", json_encode($response->getBody(), JSON_PRETTY_PRINT)),
73+
['request_hash' => spl_object_hash($request)]
5274
);
5375
} else {
54-
$this->logger->debug(
55-
sprintf('%s Response for RPC method "%s"', spl_object_hash($request), $request->getMethod()),
56-
json_decode(json_encode($response->getError()), true)
76+
$this->logger->info(
77+
sprintf('Method "%s" call was failed', $request->getMethod()),
78+
['request_hash' => spl_object_hash($request)]
79+
);
80+
$this->logger->error(
81+
sprintf('ERROR %s: %s', $response->getError()->getCode(), $response->getError()->getMessage()),
82+
['request_hash' => spl_object_hash($request)]
5783
);
5884
}
5985

60-
return $response;
86+
$this->loggedResponses[] = $hash;
6187
}
6288

63-
/** {@inheritdoc} */
64-
public function count()
89+
private function logResponse(RpcResponseInterface $response)
6590
{
66-
return $this->decoratedCollection->count();
67-
}
91+
$hash = spl_object_hash($response);
92+
if (in_array($hash, $this->loggedResponses, true)) {
93+
return;
94+
}
6895

69-
/** {@inheritdoc} */
70-
public function getIterator()
71-
{
72-
foreach ($this->decoratedCollection as $response) {
73-
//todo: log
74-
yield $response;
96+
if ($response->isSuccessful()) {
97+
$this->logger->info('Successful RPC call');
98+
$this->logger->debug(
99+
sprintf("Resposne:\n%s", json_encode($response->getBody(), JSON_PRETTY_PRINT))
100+
);
101+
} else {
102+
$this->logger->error(
103+
sprintf('RPC Error %s: %s', $response->getError()->getCode(), $response->getError()->getMessage())
104+
);
75105
}
106+
107+
$this->loggedResponses[] = $hash;
76108
}
77109
}

src/ScayTrase/Api/Rpc/ResponseCollectionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use ScayTrase\Api\Rpc\Exception\RpcExceptionInterface;
1111

12-
interface ResponseCollectionInterface extends \Countable, \Traversable
12+
interface ResponseCollectionInterface extends \Traversable
1313
{
1414
/**
1515
* @param RpcRequestInterface $request

0 commit comments

Comments
 (0)