Completed
Pull Request — master (#14)
by Pavel
04:42
created

Test/TestApi.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
        if ($this->getMetadata()->isIdentifierNatural()) {
57
            return null;
58
        }
59
60
        return is_scalar($id) ? $id : (object)$id;
61
    }
62
63
    /** {@inheritdoc} */
64
    public function find(array $identifier)
65
    {
66
        $body = $this->cache->get($identifier);
67
68
        if (null !== $body) {
69
            return $body;
70
        }
71
72
        $request = new RpcRequestMock($this->getMethod('find'), $identifier);
73
        $body    = (object)$this->client->invoke($request)->getResponse($request)->getBody();
74
        $this->cache->set($identifier, $body);
75
76
        return $body;
77
    }
78
79
    /** {@inheritdoc} */
80
    public function patch(array $identifier, array $patch, array $data)
81
    {
82
        $request = new RpcRequestMock($this->getMethod('patch'), ['identifier' => $identifier, 'patch' => $patch]);
83
84
        return $this->client->invoke($request)->getResponse($request)->isSuccessful();
85
    }
86
87
    /** {@inheritdoc} */
88
    public function search(array $criteria = [], array $orderBy = null, $limit = null, $offset = null)
89
    {
90
        $request = new RpcRequestMock(
91
            $this->getMethod('search'),
92
            [
93
                'criteria' => $criteria,
94
                'order'    => $orderBy,
95
                'limit'    => $limit,
96
                'offset'   => $offset,
97
            ]
98
        );
99
100
        $data = $this->client->invoke($request)->getResponse($request)->getBody();
101
        foreach ((array)$data as &$item) {
0 ignored issues
show
The expression (array) $data cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
102
            $item = (object)$item;
103
        }
104
105
        return new \ArrayIterator($data);
106
    }
107
108
    /** {@inheritdoc} */
109
    public function remove(array $identifier)
110
    {
111
        $request = new RpcRequestMock($this->getMethod('remove'), $identifier);
112
113
        return $this->client->invoke($request)->getResponse($request)->isSuccessful();
114
    }
115
116
    /** @return RpcClientInterface */
117
    public function getClient()
118
    {
119
        return $this->client;
120
    }
121
122
    /** @return ApiMetadata */
123
    public function getMetadata()
124
    {
125
        return $this->metadata;
126
    }
127
128
    /** {@inheritdoc} */
129
    public function setEntityCache(EntityDataCacheInterface $cache)
130
    {
131
        $this->cache = $cache;
132
    }
133
134
    /**
135
     * @param string $method
136
     *
137
     * @return string
138
     */
139
    private function getMethod($method)
140
    {
141
        return $this->metadata->getMethodContainer()->getMethod($method);
142
    }
143
}
144