Passed
Push — master ( 685595...680e76 )
by Julien
01:32
created

SdkClient::getCachePrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 Serializer|null $serializer
76
     */
77
    public function __construct(
78
        RestClient $restClient,
79
        Mapping $mapping,
80
        UnitOfWork $unitOfWork,
81
        Serializer $serializer = null
82
    ) {
83 1
        $this->restClient = $restClient;
84 1
        $this->mapping = $mapping;
85 1
        $this->unitOfWork = $unitOfWork;
86 1
        if (!$serializer) {
87
            $serializer = new Serializer($this->mapping, $this->unitOfWork);
88
        }
89 1
        $this->serializer = $serializer;
90 1
        $this->serializer->setSdk($this);
91
92 1
        $this->modelHydrator = new ModelHydrator($this);
93 1
    }
94
95
    /**
96
     * setCacheItemPool
97
     *
98
     * @param CacheItemPoolInterface $cacheItemPool
99
     *
100
     * @return SdkClient
101
     */
102
    public function setCacheItemPool(
103
        CacheItemPoolInterface $cacheItemPool,
104
        $cachePrefix = ''
105
    ) {
106
        $this->cacheItemPool = $cacheItemPool;
107
        $this->cachePrefix = $cachePrefix;
108
109
        return $this;
110
    }
111
112
    /**
113
     * getCacheItemPool
114
     *
115
     * @return ?CacheItemPoolInterface
116
     */
117
    public function getCacheItemPool()
118
    {
119
        return $this->cacheItemPool;
120
    }
121
122
    /**
123
     * getCachePrefix
124
     *
125
     * @return string
126
     */
127
    public function getCachePrefix()
128
    {
129
        return $this->cachePrefix;
130
    }
131
132
    /**
133
     * getRepository
134
     *
135
     * @param string $modelName
136
     *
137
     * @return EntityRepository
138
     */
139
    public function getRepository($modelName)
140
    {
141
        // get repository by key
142 1
        $metadata = $this->mapping->getClassMetadataByKey($modelName);
143 1
        if (!$metadata) {
144
            // get by classname
145 1
            $metadata = $this->mapping->getClassMetadata($modelName);
146
        }
147
148 1
        $modelName = $metadata->getModelName();
149
150 1
        if (!isset($this->repositoryList[$modelName])) {
151
            $repositoryName =
152 1
                $metadata->getRepositoryName()
153 1
                ?: '\Mapado\RestClientSdk\EntityRepository';
154 1
            $this->repositoryList[$modelName] = new $repositoryName(
155 1
                $this,
156 1
                $this->restClient,
157 1
                $this->unitOfWork,
158 1
                $modelName
159
            );
160
        }
161
162 1
        return $this->repositoryList[$modelName];
163
    }
164
165
    /**
166
     * getRestClient
167
     *
168
     * @return RestClient
169
     */
170
    public function getRestClient()
171
    {
172 1
        return $this->restClient;
173
    }
174
175
    /**
176
     * getMapping
177
     *
178
     * @return Mapping
179
     */
180
    public function getMapping()
181
    {
182 1
        return $this->mapping;
183
    }
184
185
    /**
186
     * getSerializer
187
     *
188
     * @return Serializer
189
     */
190
    public function getSerializer()
191
    {
192 1
        return $this->serializer;
193
    }
194
195
    /**
196
     * getModelHydrator
197
     *
198
     * @return ModelHydrator
199
     */
200
    public function getModelHydrator()
201
    {
202
        return $this->modelHydrator;
203
    }
204
205
    /**
206
     * createProxy
207
     *
208
     * @param string $id
209
     *
210
     * @return \ProxyManager\Proxy\GhostObjectInterface
211
     */
212
    public function createProxy($id)
213
    {
214
        $key = $this->mapping->getKeyFromId($id);
215
        $classMetadata = $this->mapping->getClassMetadataByKey($key);
216
217
        $modelName = $classMetadata->getModelName();
218
219
        $sdk = $this;
220
221
        if ($this->proxyManagerConfig) {
222
            $factory = new LazyLoadingGhostFactory($this->proxyManagerConfig);
223
        } else {
224
            $factory = new LazyLoadingGhostFactory();
225
        }
226
227
        $proxyModelName = preg_replace('/^\\\\*/', '', $modelName);
228
229
        $initializer = function (
230
            GhostObjectInterface $proxy,
231
            string $method,
232
            array $parameters,
233
            &$initializer,
234
            array $properties
235
        ) use ($sdk, $classMetadata, $id, $proxyModelName) {
236
            $isAllowedMethod =
237
                'jsonSerialize' === $method || '__set' === $method;
238
239
            if (!$isAllowedMethod) {
240
                $initializer = null; // disable initialization
241
                // load data and modify the object here
242
                if ($id) {
243
                    $repository = $sdk->getRepository(
244
                        $classMetadata->getModelName()
245
                    );
246
                    $model = $repository->find($id);
247
248
                    $attributeList = $classMetadata->getAttributeList();
249
250
                    foreach ($attributeList as $attribute) {
251
                        $getter =
252
                            'get' . ucfirst($attribute->getAttributeName());
253
                        $value = $model->{$getter}();
254
                        $properties[
255
                            '\0' .
256
                            $proxyModelName .
257
                            '\0' .
258
                            $attribute->getAttributeName()
259
                        ] = $value;
260
                    }
261
                }
262
263
                return true; // confirm that initialization occurred correctly
264
            }
265
        };
266
267
        // initialize the proxy instance
268
        $instance = $factory->createProxy($modelName, $initializer, [
269
            'skippedProperties' => [$proxyModelName . '\0id'],
270
        ]);
271
272
        // set the id of the object
273
        $idReflexion = new \ReflectionProperty(
274
            $modelName,
275
            $classMetadata->getIdentifierAttribute()->getAttributeName()
276
        );
277
        $idReflexion->setAccessible(true);
278
        $idReflexion->setValue($instance, $id);
279
280
        return $instance;
281
    }
282
283
    /**
284
     * Setter for fileCachePath
285
     *
286
     * @param string $fileCachePath
287
     *
288
     * @return SdkClient
289
     */
290
    public function setFileCachePath($fileCachePath)
291
    {
292
        $this->proxyManagerConfig = new Configuration();
293
        $this->proxyManagerConfig->setProxiesTargetDir($fileCachePath);
294
295
        return $this;
296
    }
297
}
298