Completed
Push — master ( 992af9...54ad9f )
by Julien
08:47 queued 03:26
created

SdkClient::setFileCachePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Mapado\RestClientSdk;
4
5
use Mapado\RestClientSdk\Model\ModelHydrator;
6
use Mapado\RestClientSdk\Model\Serializer;
7
use Mapado\RestClientSdk\UnitOfWork;
8
use ProxyManager\Configuration;
9
use ProxyManager\Factory\LazyLoadingGhostFactory;
10
use ProxyManager\Proxy\LazyLoadingInterface;
11
use Psr\Cache\CacheItemPoolInterface;
12
13
/**
14
 * Sdk Client
15
 */
16
class SdkClient
17
{
18
    /**
19
     * @var RestClient
20
     */
21
    protected $restClient;
22
23
    /**
24
     * @var Mapping
25
     */
26
    private $mapping;
27
28
    /**
29
     * @var Serializer
30
     */
31
    private $serializer;
32
33
    /**
34
     * @var ModelHydrator
35
     */
36
    private $modelHydrator;
37
38
    /**
39
     * @var array
40
     */
41
    private $repositoryList = [];
42
43
    /**
44
     * proxyManagerConfig
45
     *
46
     * @var Configuration
47
     * @access private
48
     */
49
    private $proxyManagerConfig;
50
51
    /**
52
     * Cache item pool.
53
     *
54
     * @var CacheItemPoolInterface
55
     * @access private
56
     */
57
    protected $cacheItemPool;
58
59
    /**
60
     * Cache prefix.
61
     *
62
     * @var string
63
     * @access private
64
     */
65
    protected $cachePrefix;
66
67
    /**
68
     * unitOfWork
69
     *
70
     * @var UnitOfWork
71
     * @access private
72
     */
73
    private $unitOfWork;
74
75
    /**
76
     * Constructor.
77
     *
78
     * @param RestClient      $restClient
79
     * @param Mapping         $mapping
80
     * @param Serializer|null $serializer
81
     */
82
    public function __construct(RestClient $restClient, Mapping $mapping, UnitOfWork $unitOfWork, Serializer $serializer = null)
83
    {
84 1
        $this->restClient = $restClient;
85 1
        $this->mapping = $mapping;
86 1
        $this->unitOfWork = $unitOfWork;
87 1
        if (!$serializer) {
88
            $serializer = new Serializer($this->mapping, $this->unitOfWork);
89
        }
90 1
        $this->serializer = $serializer;
91 1
        $this->serializer->setSdk($this);
92
93
94 1
        $this->modelHydrator = new ModelHydrator($this);
95 1
    }
96
97
    /**
98
     * setCacheItemPool
99
     *
100
     * @param CacheItemPoolInterface $cacheItemPool
101
     * @access public
102
     * @return SdkClient
103
     */
104
    public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool, $cachePrefix = '')
105
    {
106
        $this->cacheItemPool = $cacheItemPool;
107
        $this->cachePrefix = $cachePrefix;
108
109
        return $this;
110
    }
111
112
    /**
113
     * getCacheItemPool
114
     *
115
     * @access public
116
     * @return CacheItemPoolInterface
117
     */
118
    public function getCacheItemPool()
119
    {
120
        return $this->cacheItemPool;
121
    }
122
123
    /**
124
     * getCachePrefix
125
     *
126
     * @access public
127
     * @return string
128
     */
129
    public function getCachePrefix()
130
    {
131
        return $this->cachePrefix;
132
    }
133
134
    /**
135
     * getRepository
136
     *
137
     * @param string $modelName
138
     * @access public
139
     * @return EntityRepository
140
     */
141
    public function getRepository($modelName)
142
    {
143
        // get repository by key
144 1
        $metadata = $this->mapping->getClassMetadataByKey($modelName);
145 1
        if (!$metadata) {
146
            // get by classname
147 1
            $metadata = $this->mapping->getClassMetadata($modelName);
148
        }
149
150 1
        $modelName = $metadata->getModelName();
151
152 1
        if (!isset($this->repositoryList[$modelName])) {
153 1
            $repositoryName = $metadata->getRepositoryName() ?: '\Mapado\RestClientSdk\EntityRepository';
154 1
            $this->repositoryList[$modelName] = new $repositoryName($this, $this->restClient, $this->unitOfWork, $modelName);
155
        }
156 1
        return $this->repositoryList[$modelName];
157
    }
158
159
    /**
160
     * getRestClient
161
     *
162
     * @access public
163
     * @return RestClient
164
     */
165
    public function getRestClient()
166
    {
167 1
        return $this->restClient;
168
    }
169
170
    /**
171
     * getMapping
172
     *
173
     * @access public
174
     * @return Mapping
175
     */
176
    public function getMapping()
177
    {
178 1
        return $this->mapping;
179
    }
180
181
    /**
182
     * getSerializer
183
     *
184
     * @access public
185
     * @return Serializer
186
     */
187
    public function getSerializer()
188
    {
189 1
        return $this->serializer;
190
    }
191
192
    /**
193
     * getModelHydrator
194
     *
195
     * @access public
196
     * @return ModelHydrator
197
     */
198
    public function getModelHydrator()
199
    {
200
        return $this->modelHydrator;
201
    }
202
203
    /**
204
     * createProxy
205
     *
206
     * @param string $id
207
     * @access public
208
     * @return \ProxyManager\Proxy\GhostObjectInterface
209
     */
210
    public function createProxy($id)
211
    {
212
        $key = $this->mapping->getKeyFromId($id);
213
        $classMetadata = $this->mapping->getClassMetadataByKey($key);
214
215
        $modelName = $classMetadata->getModelName();
216
217
        $sdk = $this;
218
219
        if ($this->proxyManagerConfig) {
220
            $factory = new LazyLoadingGhostFactory($this->proxyManagerConfig);
221
        } else {
222
            $factory = new LazyLoadingGhostFactory();
223
        }
224
225
        $initializer = function (
226
            LazyLoadingInterface &$proxy,
227
            $method,
228
            array $parameters,
229
            & $initializer
230
        ) use (
231
            $sdk,
232
            $classMetadata,
233
            $id
234
        ) {
235
            $isAllowedMethod = $method === 'getId'
236
                || $method === 'setId'
237
                || $method === 'jsonSerialize'
238
                || ($method === '__isset' && $parameters['name'] === 'id');
239
240
            if (!$isAllowedMethod) {
241
                $initializer   = null; // disable initialization
242
                // load data and modify the object here
243
                if ($id) {
244
                    $repository = $sdk->getRepository($classMetadata->getModelName());
245
                    $model = $repository->find($id);
246
247
                    $attributeList = $classMetadata->getAttributeList();
248
249
                    foreach ($attributeList as $attribute) {
250
                        $getter = 'get' . ucfirst($attribute->getAttributeName());
251
                        $setter = 'set' . ucfirst($attribute->getAttributeName());
252
                        $value = $model->$getter();
253
                        $proxy->$setter($value);
254
                    }
255
                }
256
257
                return true; // confirm that initialization occurred correctly
258
            }
259
        };
260
261
        $instance = $factory->createProxy($modelName, $initializer);
262
        $instance->setId($id);
0 ignored issues
show
Bug introduced by
The method setId() does not seem to exist on object<ProxyManager\Proxy\GhostObjectInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
263
264
        return $instance;
265
    }
266
267
    /**
268
     * Setter for fileCachePath
269
     *
270
     * @param string $fileCachePath
271
     * @return SdkClient
272
     */
273
    public function setFileCachePath($fileCachePath)
274
    {
275
        $this->proxyManagerConfig = new Configuration();
276
        $this->proxyManagerConfig->setProxiesTargetDir($fileCachePath);
277
278
        return $this;
279
    }
280
}
281