Total Complexity | 82 |
Total Lines | 448 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like CynicDeserialiser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CynicDeserialiser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class CynicDeserialiser |
||
25 | { |
||
26 | /** |
||
27 | * @var IMetadataProvider |
||
28 | */ |
||
29 | private $metaProvider; |
||
30 | |||
31 | /** |
||
32 | * @var ProvidersWrapper |
||
33 | */ |
||
34 | private $wrapper; |
||
35 | |||
36 | /** |
||
37 | * @var ModelDeserialiser |
||
38 | */ |
||
39 | private $cereal; |
||
40 | |||
41 | /** |
||
42 | * CynicDeserialiser constructor. |
||
43 | * @param IMetadataProvider $meta |
||
44 | * @param ProvidersWrapper $wrapper |
||
45 | */ |
||
46 | public function __construct(IMetadataProvider $meta, ProvidersWrapper $wrapper) |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param ODataEntry $payload |
||
55 | * @throws ODataException |
||
56 | * @throws ReflectionException |
||
57 | * @throws InvalidOperationException |
||
58 | * @return mixed |
||
59 | */ |
||
60 | public function processPayload(ODataEntry &$payload) |
||
61 | { |
||
62 | $entryOk = $this->isEntryOK($payload); |
||
63 | if (!$entryOk) { |
||
64 | throw new InvalidOperationException('Payload not OK'); |
||
65 | } |
||
66 | list($sourceSet, $source) = $this->processEntryContent($payload); |
||
67 | if (!$sourceSet instanceof ResourceSet) { |
||
|
|||
68 | throw new InvalidOperationException('$sourceSet not instanceof ResourceSet'); |
||
69 | } |
||
70 | $numLinks = count($payload->links); |
||
71 | for ($i = 0; $i < $numLinks; $i++) { |
||
72 | $this->processLink($payload->links[$i], $sourceSet, $source); |
||
73 | } |
||
74 | if (!$this->isEntryProcessed($payload)) { |
||
75 | throw new InvalidOperationException('Payload not processed'); |
||
76 | } |
||
77 | return $source; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Check if supplied ODataEntry is well-formed. |
||
82 | * |
||
83 | * @param ODataEntry $payload |
||
84 | * @return bool |
||
85 | */ |
||
86 | protected function isEntryOK(ODataEntry $payload) |
||
87 | { |
||
88 | // check links |
||
89 | foreach ($payload->links as $link) { |
||
90 | /** @var ODataLink $link */ |
||
91 | $hasExpanded = null !== $link->getExpandedResult(); |
||
92 | |||
93 | $isEntry = ($link->getExpandedResult() ? $link->getExpandedResult()->getData() : null) |
||
94 | instanceof ODataEntry; |
||
95 | |||
96 | if ($hasExpanded) { |
||
97 | if ($isEntry) { |
||
98 | $this->isEntryOK($link->getExpandedResult()->getEntry()); |
||
99 | } else { |
||
100 | foreach ($link->getExpandedResult()->getFeed()->getEntries() as $expanded) { |
||
101 | $this->isEntryOK($expanded); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | $set = $this->getMetaProvider()->resolveResourceSet($payload->resourceSetName); |
||
108 | if (null === $set) { |
||
109 | $msg = 'Specified resource set could not be resolved'; |
||
110 | throw new InvalidArgumentException($msg); |
||
111 | } |
||
112 | return true; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return IMetadataProvider |
||
117 | */ |
||
118 | protected function getMetaProvider() |
||
119 | { |
||
120 | return $this->metaProvider; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param ODataEntry $content |
||
125 | * @throws ODataException |
||
126 | * @throws ReflectionException |
||
127 | * @throws Exception |
||
128 | * @throws InvalidOperationException |
||
129 | * @return array |
||
130 | */ |
||
131 | protected function processEntryContent(ODataEntry &$content) |
||
132 | { |
||
133 | assert(null === $content->id || is_string($content->id), 'Entry id must be null or string'); |
||
134 | |||
135 | $isCreate = null === $content->id || empty($content->id); |
||
136 | $set = $this->getMetaProvider()->resolveResourceSet($content->resourceSetName); |
||
137 | assert($set instanceof ResourceSet, get_class($set)); |
||
138 | $type = $set->getResourceType(); |
||
139 | $properties = $this->getDeserialiser()->bulkDeserialise($type, $content); |
||
140 | $properties = (object)$properties; |
||
141 | |||
142 | if ($isCreate) { |
||
143 | $result = $this->getWrapper()->createResourceforResourceSet($set, null, $properties); |
||
144 | assert(isset($result), get_class($result)); |
||
145 | $key = $this->generateKeyDescriptor($type, $result); |
||
146 | $keyProp = $key->getODataProperties(); |
||
147 | foreach ($keyProp as $keyName => $payload) { |
||
148 | $content->propertyContent[$keyName] = $payload; |
||
149 | } |
||
150 | } else { |
||
151 | $key = $this->generateKeyDescriptor($type, $content->propertyContent, $content->id); |
||
152 | assert($key instanceof KeyDescriptor, get_class($key)); |
||
153 | $source = $this->getWrapper()->getResourceFromResourceSet($set, $key); |
||
154 | assert(isset($source), get_class($source)); |
||
155 | $result = $this->getWrapper()->updateResource($set, $source, $key, $properties); |
||
156 | } |
||
157 | if (!$key instanceof KeyDescriptor) { |
||
158 | throw new InvalidOperationException(get_class($key)); |
||
159 | } |
||
160 | $content->id = $key; |
||
161 | |||
162 | $numLinks = count($content->links); |
||
163 | for ($i = 0; $i < $numLinks; $i++) { |
||
164 | $this->processLink($content->links[$i], $set, $result); |
||
165 | } |
||
166 | |||
167 | return [$set, $result]; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return ModelDeserialiser |
||
172 | */ |
||
173 | protected function getDeserialiser() |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return ProvidersWrapper |
||
180 | */ |
||
181 | protected function getWrapper() |
||
182 | { |
||
183 | return $this->wrapper; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param ResourceEntityType $type |
||
188 | * @param ODataPropertyContent|object $result |
||
189 | * @param string|null $id |
||
190 | * @throws ReflectionException |
||
191 | * @throws ODataException |
||
192 | * @return null|KeyDescriptor |
||
193 | */ |
||
194 | protected function generateKeyDescriptor(ResourceEntityType $type, $result, $id = null) |
||
195 | { |
||
196 | $isOData = $result instanceof ODataPropertyContent; |
||
197 | $keyProp = $type->getKeyProperties(); |
||
198 | if (null === $id) { |
||
199 | $keyPredicate = ''; |
||
200 | foreach ($keyProp as $prop) { |
||
201 | $iType = $prop->getInstanceType(); |
||
202 | assert($iType instanceof IType, get_class($iType)); |
||
203 | $keyName = $prop->getName(); |
||
204 | $rawKey = $isOData ? $result[$keyName]->getValue() : $result->{$keyName}; |
||
205 | $keyVal = $iType->convertToOData(strval($rawKey)); |
||
206 | assert(isset($keyVal), 'Key property ' . $keyName . ' must not be null'); |
||
207 | $keyPredicate .= $keyName . '=' . $keyVal . ', '; |
||
208 | } |
||
209 | $keyPredicate[strlen($keyPredicate) - 2] = ' '; |
||
210 | } else { |
||
211 | $idBits = explode('/', $id); |
||
212 | $keyRaw = $idBits[count($idBits) - 1]; |
||
213 | $rawBits = explode('(', $keyRaw, 2); |
||
214 | $rawBits = explode(')', $rawBits[count($rawBits) - 1]); |
||
215 | $keyPredicate = $rawBits[0]; |
||
216 | } |
||
217 | $keyPredicate = trim($keyPredicate); |
||
218 | /** @var KeyDescriptor|null $keyDesc */ |
||
219 | $keyDesc = null; |
||
220 | $isParsed = KeyDescriptor::tryParseKeysFromKeyPredicate($keyPredicate, $keyDesc); |
||
221 | assert(true === $isParsed, 'Key descriptor not successfully parsed'); |
||
222 | $keyDesc->validate($keyPredicate, $type); |
||
223 | // this is deliberate - ODataEntry/Feed has the structure we need for processing, and we're inserting |
||
224 | // keyDescriptor objects in id fields to indicate the given record has been processed |
||
225 | return $keyDesc; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @param ODataLink $link |
||
230 | * @param ResourceSet $sourceSet |
||
231 | * @param $source |
||
232 | * @throws InvalidOperationException |
||
233 | * @throws ODataException |
||
234 | * @throws ReflectionException |
||
235 | */ |
||
236 | protected function processLink(ODataLink &$link, ResourceSet $sourceSet, $source) |
||
237 | { |
||
238 | $hasUrl = null !== $link->getUrl(); |
||
239 | $result = $link->getExpandedResult() ? $link->getExpandedResult()->getData() : null; |
||
240 | $hasPayload = isset($result); |
||
241 | assert( |
||
242 | null == $result || $result instanceof ODataEntry || $result instanceof ODataFeed, |
||
243 | (null === $result ? 'null' : get_class($result)) |
||
244 | ); |
||
245 | $isFeed = ($link->getExpandedResult() ? $link->getExpandedResult()->getData() : null) instanceof ODataFeed; |
||
246 | |||
247 | // if nothing to hook up, bail out now |
||
248 | if (!$hasUrl && !$hasPayload) { |
||
249 | return; |
||
250 | } |
||
251 | |||
252 | if ($isFeed) { |
||
253 | $this->processLinkFeed($link, $sourceSet, $source, $hasUrl, $hasPayload); |
||
254 | } else { |
||
255 | $this->processLinkSingleton($link, $sourceSet, $source, $hasUrl, $hasPayload); |
||
256 | } |
||
257 | return; |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param ODataLink $link |
||
262 | * @param ResourceSet $sourceSet |
||
263 | * @param $source |
||
264 | * @param bool $hasUrl |
||
265 | * @param bool $hasPayload |
||
266 | * @throws InvalidOperationException |
||
267 | * @throws ODataException |
||
268 | * @throws ReflectionException |
||
269 | * @throws Exception |
||
270 | */ |
||
271 | protected function processLinkFeed(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload) |
||
272 | { |
||
273 | assert( |
||
274 | $link->getExpandedResult()->getData() instanceof ODataFeed, |
||
275 | get_class($link->getExpandedResult()->getData()) |
||
276 | ); |
||
277 | $propName = $link->getTitle(); |
||
278 | |||
279 | // if entries is empty, bail out - nothing to do |
||
280 | $numEntries = count($link->getExpandedResult()->getFeed()->getEntries()); |
||
281 | if (0 === $numEntries) { |
||
282 | return; |
||
283 | } |
||
284 | // check that each entry is of consistent resource set after checking it hasn't been processed |
||
285 | $first = $link->getExpandedResult()->getFeed()->getEntries()[0]->resourceSetName; |
||
286 | if ($link->getExpandedResult()->getFeed()->getEntries()[0]->id instanceof KeyDescriptor) { |
||
287 | return; |
||
288 | } |
||
289 | for ($i = 1; $i < $numEntries; $i++) { |
||
290 | if ($first !== $link->getExpandedResult()->getFeed()->getEntries()[$i]->resourceSetName) { |
||
291 | $msg = 'All entries in given feed must have same resource set'; |
||
292 | throw new InvalidArgumentException($msg); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | $targSet = $this->getMetaProvider()->resolveResourceSet($first); |
||
297 | assert($targSet instanceof ResourceSet); |
||
298 | $targType = $targSet->getResourceType(); |
||
299 | assert($targType instanceof ResourceEntityType); |
||
300 | $instanceType = $targType->getInstanceType(); |
||
301 | assert($instanceType instanceof ReflectionClass); |
||
302 | $targObj = $instanceType->newInstanceArgs(); |
||
303 | |||
304 | // assemble payload |
||
305 | $data = []; |
||
306 | $keys = []; |
||
307 | for ($i = 0; $i < $numEntries; $i++) { |
||
308 | $data[] = $this->getDeserialiser()->bulkDeserialise( |
||
309 | $targType, |
||
310 | $link->getExpandedResult()->getFeed()->getEntries()[$i] |
||
311 | ); |
||
312 | $keys[] = $hasUrl ? $this->generateKeyDescriptor( |
||
313 | $targType, |
||
314 | $link->getExpandedResult()->getFeed()->getEntries()[$i]->propertyContent |
||
315 | ) : null; |
||
316 | } |
||
317 | |||
318 | // creation |
||
319 | if (!$hasUrl && $hasPayload) { |
||
320 | $bulkResult = $this->getWrapper()->createBulkResourceforResourceSet($targSet, $data); |
||
321 | assert(is_array($bulkResult)); |
||
322 | for ($i = 0; $i < $numEntries; $i++) { |
||
323 | $targEntityInstance = $bulkResult[$i]; |
||
324 | $this->getWrapper()->hookSingleModel($sourceSet, $source, $targSet, $targEntityInstance, $propName); |
||
325 | $key = $this->generateKeyDescriptor($targType, $targEntityInstance); |
||
326 | $link->getExpandedResult()->getFeed()->getEntries()[$i]->id = $key; |
||
327 | } |
||
328 | } |
||
329 | // update |
||
330 | if ($hasUrl && $hasPayload) { |
||
331 | $bulkResult = $this->getWrapper()->updateBulkResource($targSet, $targObj, $keys, $data); |
||
332 | for ($i = 0; $i < $numEntries; $i++) { |
||
333 | $targEntityInstance = $bulkResult[$i]; |
||
334 | $this->getWrapper()->hookSingleModel($sourceSet, $source, $targSet, $targEntityInstance, $propName); |
||
335 | $link->getExpandedResult()->getFeed()->getEntries()[$i]->id = $keys[$i]; |
||
336 | } |
||
337 | } |
||
338 | assert(isset($bulkResult) && is_array($bulkResult)); |
||
339 | |||
340 | for ($i = 0; $i < $numEntries; $i++) { |
||
341 | assert($link->getExpandedResult()->getFeed()->getEntries()[$i]->id instanceof KeyDescriptor); |
||
342 | $numLinks = count($link->getExpandedResult()->getFeed()->getEntries()[$i]->links); |
||
343 | for ($j = 0; $j < $numLinks; $j++) { |
||
344 | $this->processLink($link->getExpandedResult()->getFeed()->getEntries()[$i]->links[$j], $targSet, $bulkResult[$i]); |
||
345 | } |
||
346 | } |
||
347 | |||
348 | return; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @param ODataLink $link |
||
353 | * @param ResourceSet $sourceSet |
||
354 | * @param $source |
||
355 | * @param $hasUrl |
||
356 | * @param $hasPayload |
||
357 | * @throws InvalidOperationException |
||
358 | * @throws ODataException |
||
359 | * @throws ReflectionException |
||
360 | */ |
||
361 | protected function processLinkSingleton(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload) |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @param ODataEntry $payload |
||
439 | * @param int $depth |
||
440 | * @return bool |
||
441 | */ |
||
442 | protected function isEntryProcessed(ODataEntry $payload, $depth = 0) |
||
472 | } |
||
473 | } |
||
474 |