Total Complexity | 84 |
Total Lines | 452 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 1 | 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) |
||
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 | $hasUrl = null !== $link->getUrl(); |
||
91 | $hasExpanded = null !== $link->getExpandedResult(); |
||
92 | if ($hasUrl) { |
||
93 | if (!is_string($link->getUrl())) { |
||
94 | $msg = 'Url must be either string or null'; |
||
95 | throw new InvalidArgumentException($msg); |
||
96 | } |
||
97 | } |
||
98 | $isEntry = ($link->getExpandedResult() ? $link->getExpandedResult()->getData() : null) instanceof ODataEntry; |
||
99 | |||
100 | if ($hasExpanded) { |
||
101 | if ($isEntry) { |
||
102 | $this->isEntryOK($link->getExpandedResult()->getEntry()); |
||
103 | } else { |
||
104 | foreach ($link->getExpandedResult()->getFeed()->entries as $expanded) { |
||
105 | $this->isEntryOK($expanded); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
111 | $set = $this->getMetaProvider()->resolveResourceSet($payload->resourceSetName); |
||
112 | if (null === $set) { |
||
113 | $msg = 'Specified resource set could not be resolved'; |
||
114 | throw new InvalidArgumentException($msg); |
||
115 | } |
||
116 | return true; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return IMetadataProvider |
||
121 | */ |
||
122 | protected function getMetaProvider() |
||
123 | { |
||
124 | return $this->metaProvider; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param ODataEntry $content |
||
129 | * @throws ODataException |
||
130 | * @throws ReflectionException |
||
131 | * @throws Exception |
||
132 | * @throws InvalidOperationException |
||
133 | * @return array |
||
134 | */ |
||
135 | protected function processEntryContent(ODataEntry &$content) |
||
136 | { |
||
137 | assert(null === $content->id || is_string($content->id), 'Entry id must be null or string'); |
||
138 | |||
139 | $isCreate = null === $content->id || empty($content->id); |
||
140 | $set = $this->getMetaProvider()->resolveResourceSet($content->resourceSetName); |
||
141 | assert($set instanceof ResourceSet, get_class($set)); |
||
142 | $type = $set->getResourceType(); |
||
143 | $properties = $this->getDeserialiser()->bulkDeserialise($type, $content); |
||
144 | $properties = (object)$properties; |
||
145 | |||
146 | if ($isCreate) { |
||
147 | $result = $this->getWrapper()->createResourceforResourceSet($set, null, $properties); |
||
148 | assert(isset($result), get_class($result)); |
||
149 | $key = $this->generateKeyDescriptor($type, $result); |
||
150 | $keyProp = $key->getODataProperties(); |
||
151 | foreach ($keyProp as $keyName => $payload) { |
||
152 | $content->propertyContent->properties[$keyName] = $payload; |
||
153 | } |
||
154 | } else { |
||
155 | $key = $this->generateKeyDescriptor($type, $content->propertyContent, $content->id); |
||
156 | assert($key instanceof KeyDescriptor, get_class($key)); |
||
157 | $source = $this->getWrapper()->getResourceFromResourceSet($set, $key); |
||
158 | assert(isset($source), get_class($source)); |
||
159 | $result = $this->getWrapper()->updateResource($set, $source, $key, $properties); |
||
160 | } |
||
161 | if (!$key instanceof KeyDescriptor) { |
||
162 | throw new InvalidOperationException(get_class($key)); |
||
163 | } |
||
164 | $content->id = $key; |
||
165 | |||
166 | $numLinks = count($content->links); |
||
167 | for ($i = 0; $i < $numLinks; $i++) { |
||
168 | $this->processLink($content->links[$i], $set, $result); |
||
169 | } |
||
170 | |||
171 | return [$set, $result]; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @return ModelDeserialiser |
||
176 | */ |
||
177 | protected function getDeserialiser() |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @return ProvidersWrapper |
||
184 | */ |
||
185 | protected function getWrapper() |
||
186 | { |
||
187 | return $this->wrapper; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param ResourceEntityType $type |
||
192 | * @param ODataPropertyContent|object $result |
||
193 | * @param string|null $id |
||
194 | * @throws ReflectionException |
||
195 | * @throws ODataException |
||
196 | * @return null|KeyDescriptor |
||
197 | */ |
||
198 | protected function generateKeyDescriptor(ResourceEntityType $type, $result, $id = null) |
||
199 | { |
||
200 | $isOData = $result instanceof ODataPropertyContent; |
||
201 | $keyProp = $type->getKeyProperties(); |
||
202 | if (null === $id) { |
||
203 | $keyPredicate = ''; |
||
204 | foreach ($keyProp as $prop) { |
||
205 | $iType = $prop->getInstanceType(); |
||
206 | assert($iType instanceof IType, get_class($iType)); |
||
207 | $keyName = $prop->getName(); |
||
208 | $rawKey = $isOData ? $result->properties[$keyName]->value : $result->{$keyName}; |
||
209 | $keyVal = $iType->convertToOData(strval($rawKey)); |
||
210 | assert(isset($keyVal), 'Key property ' . $keyName . ' must not be null'); |
||
211 | $keyPredicate .= $keyName . '=' . $keyVal . ', '; |
||
212 | } |
||
213 | $keyPredicate[strlen($keyPredicate) - 2] = ' '; |
||
214 | } else { |
||
215 | $idBits = explode('/', $id); |
||
216 | $keyRaw = $idBits[count($idBits) - 1]; |
||
217 | $rawBits = explode('(', $keyRaw, 2); |
||
218 | $rawBits = explode(')', $rawBits[count($rawBits) - 1]); |
||
219 | $keyPredicate = $rawBits[0]; |
||
220 | } |
||
221 | $keyPredicate = trim($keyPredicate); |
||
222 | /** @var KeyDescriptor|null $keyDesc */ |
||
223 | $keyDesc = null; |
||
224 | $isParsed = KeyDescriptor::tryParseKeysFromKeyPredicate($keyPredicate, $keyDesc); |
||
225 | assert(true === $isParsed, 'Key descriptor not successfully parsed'); |
||
226 | $keyDesc->validate($keyPredicate, $type); |
||
227 | // this is deliberate - ODataEntry/Feed has the structure we need for processing, and we're inserting |
||
228 | // keyDescriptor objects in id fields to indicate the given record has been processed |
||
229 | return $keyDesc; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param ODataLink $link |
||
234 | * @param ResourceSet $sourceSet |
||
235 | * @param $source |
||
236 | * @throws InvalidOperationException |
||
237 | * @throws ODataException |
||
238 | * @throws ReflectionException |
||
239 | */ |
||
240 | protected function processLink(ODataLink &$link, ResourceSet $sourceSet, $source) |
||
241 | { |
||
242 | $hasUrl = null !== $link->getUrl(); |
||
243 | $result = $link->getExpandedResult() ? $link->getExpandedResult()->getData() : null; |
||
244 | $hasPayload = isset($result); |
||
245 | assert( |
||
246 | null == $result || $result instanceof ODataEntry || $result instanceof ODataFeed, |
||
247 | (null === $result ? 'null' : get_class($result)) |
||
248 | ); |
||
249 | $isFeed = ($link->getExpandedResult() ? $link->getExpandedResult()->getData() : null) instanceof ODataFeed; |
||
250 | |||
251 | // if nothing to hook up, bail out now |
||
252 | if (!$hasUrl && !$hasPayload) { |
||
253 | return; |
||
254 | } |
||
255 | |||
256 | if ($isFeed) { |
||
257 | $this->processLinkFeed($link, $sourceSet, $source, $hasUrl, $hasPayload); |
||
258 | } else { |
||
259 | $this->processLinkSingleton($link, $sourceSet, $source, $hasUrl, $hasPayload); |
||
260 | } |
||
261 | return; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param ODataLink $link |
||
266 | * @param ResourceSet $sourceSet |
||
267 | * @param $source |
||
268 | * @param bool $hasUrl |
||
269 | * @param bool $hasPayload |
||
270 | * @throws InvalidOperationException |
||
271 | * @throws ODataException |
||
272 | * @throws ReflectionException |
||
273 | * @throws Exception |
||
274 | */ |
||
275 | protected function processLinkFeed(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload) |
||
276 | { |
||
277 | assert( |
||
278 | $link->getExpandedResult()->getData() instanceof ODataFeed, |
||
279 | get_class($link->getExpandedResult()->getData()) |
||
280 | ); |
||
281 | $propName = $link->getTitle(); |
||
282 | |||
283 | // if entries is empty, bail out - nothing to do |
||
284 | $numEntries = count($link->getExpandedResult()->getFeed()->entries); |
||
285 | if (0 === $numEntries) { |
||
286 | return; |
||
287 | } |
||
288 | // check that each entry is of consistent resource set after checking it hasn't been processed |
||
289 | $first = $link->getExpandedResult()->getFeed()->entries[0]->resourceSetName; |
||
290 | if ($link->getExpandedResult()->getFeed()->entries[0]->id instanceof KeyDescriptor) { |
||
291 | return; |
||
292 | } |
||
293 | for ($i = 1; $i < $numEntries; $i++) { |
||
294 | if ($first !== $link->getExpandedResult()->getFeed()->entries[$i]->resourceSetName) { |
||
295 | $msg = 'All entries in given feed must have same resource set'; |
||
296 | throw new InvalidArgumentException($msg); |
||
297 | } |
||
298 | } |
||
299 | |||
300 | $targSet = $this->getMetaProvider()->resolveResourceSet($first); |
||
301 | assert($targSet instanceof ResourceSet); |
||
302 | $targType = $targSet->getResourceType(); |
||
303 | assert($targType instanceof ResourceEntityType); |
||
304 | $instanceType = $targType->getInstanceType(); |
||
305 | assert($instanceType instanceof ReflectionClass); |
||
306 | $targObj = $instanceType->newInstanceArgs(); |
||
307 | |||
308 | // assemble payload |
||
309 | $data = []; |
||
310 | $keys = []; |
||
311 | for ($i = 0; $i < $numEntries; $i++) { |
||
312 | $data[] = $this->getDeserialiser()->bulkDeserialise( |
||
313 | $targType, |
||
314 | $link->getExpandedResult()->getFeed()->entries[$i] |
||
315 | ); |
||
316 | $keys[] = $hasUrl ? $this->generateKeyDescriptor( |
||
317 | $targType, |
||
318 | $link->getExpandedResult()->getFeed()->entries[$i]->propertyContent |
||
319 | ) : null; |
||
320 | } |
||
321 | |||
322 | // creation |
||
323 | if (!$hasUrl && $hasPayload) { |
||
324 | $bulkResult = $this->getWrapper()->createBulkResourceforResourceSet($targSet, $data); |
||
325 | assert(is_array($bulkResult)); |
||
326 | for ($i = 0; $i < $numEntries; $i++) { |
||
327 | $targEntityInstance = $bulkResult[$i]; |
||
328 | $this->getWrapper()->hookSingleModel($sourceSet, $source, $targSet, $targEntityInstance, $propName); |
||
329 | $key = $this->generateKeyDescriptor($targType, $targEntityInstance); |
||
330 | $link->getExpandedResult()->getFeed()->entries[$i]->id = $key; |
||
331 | } |
||
332 | } |
||
333 | // update |
||
334 | if ($hasUrl && $hasPayload) { |
||
335 | $bulkResult = $this->getWrapper()->updateBulkResource($targSet, $targObj, $keys, $data); |
||
336 | for ($i = 0; $i < $numEntries; $i++) { |
||
337 | $targEntityInstance = $bulkResult[$i]; |
||
338 | $this->getWrapper()->hookSingleModel($sourceSet, $source, $targSet, $targEntityInstance, $propName); |
||
339 | $link->getExpandedResult()->getFeed()->entries[$i]->id = $keys[$i]; |
||
340 | } |
||
341 | } |
||
342 | assert(isset($bulkResult) && is_array($bulkResult)); |
||
343 | |||
344 | for ($i = 0; $i < $numEntries; $i++) { |
||
345 | assert($link->getExpandedResult()->getFeed()->entries[$i]->id instanceof KeyDescriptor); |
||
346 | $numLinks = count($link->getExpandedResult()->getFeed()->entries[$i]->links); |
||
347 | for ($j = 0; $j < $numLinks; $j++) { |
||
348 | $this->processLink($link->getExpandedResult()->getFeed()->entries[$i]->links[$j], $targSet, $bulkResult[$i]); |
||
349 | } |
||
350 | } |
||
351 | |||
352 | return; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @param ODataLink $link |
||
357 | * @param ResourceSet $sourceSet |
||
358 | * @param $source |
||
359 | * @param $hasUrl |
||
360 | * @param $hasPayload |
||
361 | * @throws InvalidOperationException |
||
362 | * @throws ODataException |
||
363 | * @throws ReflectionException |
||
364 | */ |
||
365 | protected function processLinkSingleton(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload) |
||
439 | } |
||
440 | |||
441 | /** |
||
442 | * @param ODataEntry $payload |
||
443 | * @param int $depth |
||
444 | * @return bool |
||
445 | */ |
||
446 | protected function isEntryProcessed(ODataEntry $payload, $depth = 0) |
||
476 | } |
||
477 | } |
||
478 |