Completed
Push — master ( 0fa951...277729 )
by Julien
05:41 queued 02:18
created

SdkClient   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 287
Duplicated Lines 0 %

Test Coverage

Coverage 34.25%

Importance

Changes 0
Metric Value
dl 0
loc 287
ccs 25
cts 73
cp 0.3425
rs 10
c 0
b 0
f 0
wmc 23

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 3
A getCachePrefix() 0 3 1
A getSerializer() 0 3 1
A getCacheItemPool() 0 3 1
A setCacheItemPool() 0 8 1
A getModelHydrator() 0 3 1
A getRestClient() 0 3 1
B getRepository() 0 24 4
A getMapping() 0 3 1
A setFileCachePath() 0 6 1
C createProxy() 0 71 8
1
<?php
2
3
namespace Mapado\RestClientSdk;
4
5
use Mapado\RestClientSdk\Model\ModelHydrator;
6
use Mapado\RestClientSdk\Model\Serializer;
7
use ProxyManager\Configuration;
8
use ProxyManager\Factory\LazyLoadingGhostFactory;
9
use ProxyManager\Proxy\GhostObjectInterface;
10
use Psr\Cache\CacheItemPoolInterface;
11
12
/**
13
 * Sdk Client
14
 */
15
class SdkClient
16
{
17
    /**
18
     * @var RestClient
19
     */
20
    protected $restClient;
21
22
    /**
23
     * Cache item pool.
24
     *
25
     * @var CacheItemPoolInterface
26
     */
27
    protected $cacheItemPool;
28
29
    /**
30
     * Cache prefix.
31
     *
32
     * @var string
33
     */
34
    protected $cachePrefix;
35
36
    /**
37
     * @var Mapping
38
     */
39
    private $mapping;
40
41
    /**
42
     * @var Serializer
43
     */
44
    private $serializer;
45
46
    /**
47
     * @var ModelHydrator
48
     */
49
    private $modelHydrator;
50
51
    /**
52
     * @var array
53
     */
54
    private $repositoryList = [];
55
56
    /**
57
     * proxyManagerConfig
58
     *
59
     * @var Configuration
60
     */
61
    private $proxyManagerConfig;
62
63
    /**
64
     * unitOfWork
65
     *
66
     * @var UnitOfWork
67
     */
68
    private $unitOfWork;
69
70
    /**
71
     * Constructor.
72
     *
73
     * @param RestClient      $restClient
74
     * @param Mapping         $mapping
75
     * @param UnitOfWork|null $unitOfWork
76
     * @param Serializer|null $serializer
77
     */
78
    public function __construct(
79
        RestClient $restClient,
80
        Mapping $mapping,
81
        UnitOfWork $unitOfWork = null,
82
        Serializer $serializer = null
83
    ) {
84 1
        $this->restClient = $restClient;
85 1
        $this->mapping = $mapping;
86 1
        if (!$unitOfWork) {
87
            $unitOfWork = new UnitOfWork($this->mapping);
88
        }
89 1
        $this->unitOfWork = $unitOfWork;
90 1
        if (!$serializer) {
91
            $serializer = new Serializer($this->mapping, $this->unitOfWork);
92
        }
93 1
        $this->serializer = $serializer;
94 1
        $this->serializer->setSdk($this);
95
96 1
        $this->modelHydrator = new ModelHydrator($this);
97 1
    }
98
99
    /**
100
     * setCacheItemPool
101
     *
102
     * @param CacheItemPoolInterface $cacheItemPool
103
     *
104
     * @return SdkClient
105
     */
106
    public function setCacheItemPool(
107
        CacheItemPoolInterface $cacheItemPool,
108
        $cachePrefix = ''
109
    ) {
110
        $this->cacheItemPool = $cacheItemPool;
111
        $this->cachePrefix = $cachePrefix;
112
113
        return $this;
114
    }
115
116
    /**
117
     * getCacheItemPool
118
     *
119
     * @return ?CacheItemPoolInterface
120
     */
121
    public function getCacheItemPool()
122
    {
123
        return $this->cacheItemPool;
124
    }
125
126
    /**
127
     * getCachePrefix
128
     *
129
     * @return string
130
     */
131
    public function getCachePrefix()
132
    {
133
        return $this->cachePrefix;
134
    }
135
136
    /**
137
     * getRepository
138
     *
139
     * @param string $modelName
140
     *
141
     * @return EntityRepository
142
     */
143
    public function getRepository($modelName)
144
    {
145
        // get repository by key
146 1
        $metadata = $this->mapping->getClassMetadataByKey($modelName);
147 1
        if (!$metadata) {
148
            // get by classname
149 1
            $metadata = $this->mapping->getClassMetadata($modelName);
150
        }
151
152 1
        $modelName = $metadata->getModelName();
153
154 1
        if (!isset($this->repositoryList[$modelName])) {
155
            $repositoryName =
156 1
                $metadata->getRepositoryName()
157 1
                ?: '\Mapado\RestClientSdk\EntityRepository';
158 1
            $this->repositoryList[$modelName] = new $repositoryName(
159 1
                $this,
160 1
                $this->restClient,
161 1
                $this->unitOfWork,
162 1
                $modelName
163
            );
164
        }
165
166 1
        return $this->repositoryList[$modelName];
167
    }
168
169
    /**
170
     * getRestClient
171
     *
172
     * @return RestClient
173
     */
174
    public function getRestClient()
175
    {
176 1
        return $this->restClient;
177
    }
178
179
    /**
180
     * getMapping
181
     *
182
     * @return Mapping
183
     */
184
    public function getMapping()
185
    {
186 1
        return $this->mapping;
187
    }
188
189
    /**
190
     * getSerializer
191
     *
192
     * @return Serializer
193
     */
194
    public function getSerializer()
195
    {
196 1
        return $this->serializer;
197
    }
198
199
    /**
200
     * getModelHydrator
201
     *
202
     * @return ModelHydrator
203
     */
204
    public function getModelHydrator()
205
    {
206
        return $this->modelHydrator;
207
    }
208
209
    /**
210
     * createProxy
211
     *
212
     * @param string $id
213
     *
214
     * @return \ProxyManager\Proxy\GhostObjectInterface
215
     */
216
    public function createProxy($id)
217
    {
218
        $key = $this->mapping->getKeyFromId($id);
219
        $classMetadata = $this->mapping->getClassMetadataByKey($key);
220
221
        $modelName = $classMetadata->getModelName();
222
223
        $sdk = $this;
224
225
        if ($this->proxyManagerConfig) {
226
            $factory = new LazyLoadingGhostFactory($this->proxyManagerConfig);
227
        } else {
228
            $factory = new LazyLoadingGhostFactory();
229
        }
230
231
        $proxyModelName = preg_replace('/^\\\\*/', '', $modelName);
232
233
        $initializer = function (
234
            GhostObjectInterface $proxy,
235
            string $method,
236
            array $parameters,
237
            &$initializer,
238
            array $properties
239
        ) use ($sdk, $classMetadata, $id, $proxyModelName) {
240
            $isAllowedMethod =
241
                'jsonSerialize' === $method ||
242
                '__set' === $method ||
243
                '__isset' === $method && 'id' === $parameters['name'];
244
245
            if (!$isAllowedMethod) {
246
                $initializer = null; // disable initialization
247
                // load data and modify the object here
248
                if ($id) {
249
                    $repository = $sdk->getRepository(
250
                        $classMetadata->getModelName()
251
                    );
252
                    $model = $repository->find($id);
253
254
                    $attributeList = $classMetadata->getAttributeList();
255
256
                    foreach ($attributeList as $attribute) {
257
                        $getter =
258
                            'get' . ucfirst($attribute->getAttributeName());
259
                        $value = $model->{$getter}();
260
                        $properties[
261
                            "\0" .
262
                            $proxyModelName .
263
                            "\0" .
264
                            $attribute->getAttributeName()
265
                        ] = $value;
266
                    }
267
                }
268
269
                return true; // confirm that initialization occurred correctly
270
            }
271
        };
272
273
        // initialize the proxy instance
274
        $instance = $factory->createProxy($modelName, $initializer, [
275
            'skippedProperties' => ["\0" . $proxyModelName . "\0id"],
276
        ]);
277
278
        // set the id of the object
279
        $idReflexion = new \ReflectionProperty(
280
            $modelName,
281
            $classMetadata->getIdentifierAttribute()->getAttributeName()
282
        );
283
        $idReflexion->setAccessible(true);
284
        $idReflexion->setValue($instance, $id);
285
286
        return $instance;
287
    }
288
289
    /**
290
     * Setter for fileCachePath
291
     *
292
     * @param string $fileCachePath
293
     *
294
     * @return SdkClient
295
     */
296
    public function setFileCachePath($fileCachePath)
297
    {
298
        $this->proxyManagerConfig = new Configuration();
299
        $this->proxyManagerConfig->setProxiesTargetDir($fileCachePath);
300
301
        return $this;
302
    }
303
}
304