| Total Complexity | 87 | 
| Total Lines | 459 | 
| Duplicated Lines | 0 % | 
| Changes | 4 | ||
| 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)  | 
            ||
| 47 |     { | 
            ||
| 48 | $this->metaProvider = $meta;  | 
            ||
| 49 | $this->wrapper = $wrapper;  | 
            ||
| 50 | $this->cereal = new ModelDeserialiser();  | 
            ||
| 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 | $hasUrl = null !== $link->getUrl();  | 
            ||
| 91 | $hasExpanded = isset($link->expandedResult);  | 
            ||
| 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 |             if ($hasExpanded) { | 
            ||
| 99 | $isGood = $link->expandedResult instanceof ODataEntry || $link->expandedResult instanceof ODataFeed;  | 
            ||
| 100 |                 if (!$isGood) { | 
            ||
| 101 | $msg = 'Expanded result must null, or be instance of ODataEntry or ODataFeed';  | 
            ||
| 102 | throw new InvalidArgumentException($msg);  | 
            ||
| 103 | }  | 
            ||
| 104 | }  | 
            ||
| 105 | $isEntry = ($link->getExpandedResult() ? $link->getExpandedResult()->getData() : null) instanceof ODataEntry;  | 
            ||
| 106 | |||
| 107 |             if ($hasExpanded) { | 
            ||
| 108 |                 if ($isEntry) { | 
            ||
| 109 | $this->isEntryOK($link->expandedResult);  | 
            ||
| 110 |                 } else { | 
            ||
| 111 |                     foreach ($link->expandedResult->entries as $expanded) { | 
            ||
| 112 | $this->isEntryOK($expanded);  | 
            ||
| 113 | }  | 
            ||
| 114 | }  | 
            ||
| 115 | }  | 
            ||
| 116 | }  | 
            ||
| 117 | |||
| 118 | $set = $this->getMetaProvider()->resolveResourceSet($payload->resourceSetName);  | 
            ||
| 119 |         if (null === $set) { | 
            ||
| 120 | $msg = 'Specified resource set could not be resolved';  | 
            ||
| 121 | throw new InvalidArgumentException($msg);  | 
            ||
| 122 | }  | 
            ||
| 123 | return true;  | 
            ||
| 124 | }  | 
            ||
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * @return IMetadataProvider  | 
            ||
| 128 | */  | 
            ||
| 129 | protected function getMetaProvider()  | 
            ||
| 130 |     { | 
            ||
| 131 | return $this->metaProvider;  | 
            ||
| 132 | }  | 
            ||
| 133 | |||
| 134 | /**  | 
            ||
| 135 | * @param ODataEntry $content  | 
            ||
| 136 | * @throws ODataException  | 
            ||
| 137 | * @throws ReflectionException  | 
            ||
| 138 | * @throws Exception  | 
            ||
| 139 | * @throws InvalidOperationException  | 
            ||
| 140 | * @return array  | 
            ||
| 141 | */  | 
            ||
| 142 | protected function processEntryContent(ODataEntry &$content)  | 
            ||
| 143 |     { | 
            ||
| 144 | assert(null === $content->id || is_string($content->id), 'Entry id must be null or string');  | 
            ||
| 145 | |||
| 146 | $isCreate = null === $content->id || empty($content->id);  | 
            ||
| 147 | $set = $this->getMetaProvider()->resolveResourceSet($content->resourceSetName);  | 
            ||
| 148 | assert($set instanceof ResourceSet, get_class($set));  | 
            ||
| 149 | $type = $set->getResourceType();  | 
            ||
| 150 | $properties = $this->getDeserialiser()->bulkDeserialise($type, $content);  | 
            ||
| 151 | $properties = (object)$properties;  | 
            ||
| 152 | |||
| 153 |         if ($isCreate) { | 
            ||
| 154 | $result = $this->getWrapper()->createResourceforResourceSet($set, null, $properties);  | 
            ||
| 155 | assert(isset($result), get_class($result));  | 
            ||
| 156 | $key = $this->generateKeyDescriptor($type, $result);  | 
            ||
| 157 | $keyProp = $key->getODataProperties();  | 
            ||
| 158 |             foreach ($keyProp as $keyName => $payload) { | 
            ||
| 159 | $content->propertyContent->properties[$keyName] = $payload;  | 
            ||
| 160 | }  | 
            ||
| 161 |         } else { | 
            ||
| 162 | $key = $this->generateKeyDescriptor($type, $content->propertyContent, $content->id);  | 
            ||
| 163 | assert($key instanceof KeyDescriptor, get_class($key));  | 
            ||
| 164 | $source = $this->getWrapper()->getResourceFromResourceSet($set, $key);  | 
            ||
| 165 | assert(isset($source), get_class($source));  | 
            ||
| 166 | $result = $this->getWrapper()->updateResource($set, $source, $key, $properties);  | 
            ||
| 167 | }  | 
            ||
| 168 |         if (!$key instanceof KeyDescriptor) { | 
            ||
| 169 | throw new InvalidOperationException(get_class($key));  | 
            ||
| 170 | }  | 
            ||
| 171 | $content->id = $key;  | 
            ||
| 172 | |||
| 173 | $numLinks = count($content->links);  | 
            ||
| 174 |         for ($i = 0; $i < $numLinks; $i++) { | 
            ||
| 175 | $this->processLink($content->links[$i], $set, $result);  | 
            ||
| 176 | }  | 
            ||
| 177 | |||
| 178 | return [$set, $result];  | 
            ||
| 179 | }  | 
            ||
| 180 | |||
| 181 | /**  | 
            ||
| 182 | * @return ModelDeserialiser  | 
            ||
| 183 | */  | 
            ||
| 184 | protected function getDeserialiser()  | 
            ||
| 187 | }  | 
            ||
| 188 | |||
| 189 | /**  | 
            ||
| 190 | * @return ProvidersWrapper  | 
            ||
| 191 | */  | 
            ||
| 192 | protected function getWrapper()  | 
            ||
| 193 |     { | 
            ||
| 194 | return $this->wrapper;  | 
            ||
| 195 | }  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * @param ResourceEntityType $type  | 
            ||
| 199 | * @param ODataPropertyContent|object $result  | 
            ||
| 200 | * @param string|null $id  | 
            ||
| 201 | * @throws ReflectionException  | 
            ||
| 202 | * @throws ODataException  | 
            ||
| 203 | * @return null|KeyDescriptor  | 
            ||
| 204 | */  | 
            ||
| 205 | protected function generateKeyDescriptor(ResourceEntityType $type, $result, $id = null)  | 
            ||
| 237 | }  | 
            ||
| 238 | |||
| 239 | /**  | 
            ||
| 240 | * @param ODataLink $link  | 
            ||
| 241 | * @param ResourceSet $sourceSet  | 
            ||
| 242 | * @param $source  | 
            ||
| 243 | * @throws InvalidOperationException  | 
            ||
| 244 | * @throws ODataException  | 
            ||
| 245 | * @throws ReflectionException  | 
            ||
| 246 | */  | 
            ||
| 247 | protected function processLink(ODataLink &$link, ResourceSet $sourceSet, $source)  | 
            ||
| 248 |     { | 
            ||
| 249 | $hasUrl = null !== $link->getUrl();  | 
            ||
| 250 | $result = $link->getExpandedResult() ? $link->getExpandedResult()->getData() : null;  | 
            ||
| 251 | $hasPayload = isset($result);  | 
            ||
| 252 | assert(  | 
            ||
| 253 | null == $result || $result instanceof ODataEntry || $result instanceof ODataFeed,  | 
            ||
| 254 | (null === $result ? 'null' : get_class($result))  | 
            ||
| 255 | );  | 
            ||
| 256 | $isFeed = ($link->getExpandedResult() ? $link->getExpandedResult()->getData() : null) instanceof ODataFeed;  | 
            ||
| 257 | |||
| 258 | // if nothing to hook up, bail out now  | 
            ||
| 259 |         if (!$hasUrl && !$hasPayload) { | 
            ||
| 260 | return;  | 
            ||
| 261 | }  | 
            ||
| 262 | |||
| 263 |         if ($isFeed) { | 
            ||
| 264 | $this->processLinkFeed($link, $sourceSet, $source, $hasUrl, $hasPayload);  | 
            ||
| 265 |         } else { | 
            ||
| 266 | $this->processLinkSingleton($link, $sourceSet, $source, $hasUrl, $hasPayload);  | 
            ||
| 267 | }  | 
            ||
| 268 | return;  | 
            ||
| 269 | }  | 
            ||
| 270 | |||
| 271 | /**  | 
            ||
| 272 | * @param ODataLink $link  | 
            ||
| 273 | * @param ResourceSet $sourceSet  | 
            ||
| 274 | * @param $source  | 
            ||
| 275 | * @param bool $hasUrl  | 
            ||
| 276 | * @param bool $hasPayload  | 
            ||
| 277 | * @throws InvalidOperationException  | 
            ||
| 278 | * @throws ODataException  | 
            ||
| 279 | * @throws ReflectionException  | 
            ||
| 280 | * @throws Exception  | 
            ||
| 281 | */  | 
            ||
| 282 | protected function processLinkFeed(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload)  | 
            ||
| 360 | }  | 
            ||
| 361 | |||
| 362 | /**  | 
            ||
| 363 | * @param ODataLink $link  | 
            ||
| 364 | * @param ResourceSet $sourceSet  | 
            ||
| 365 | * @param $source  | 
            ||
| 366 | * @param $hasUrl  | 
            ||
| 367 | * @param $hasPayload  | 
            ||
| 368 | * @throws InvalidOperationException  | 
            ||
| 369 | * @throws ODataException  | 
            ||
| 370 | * @throws ReflectionException  | 
            ||
| 371 | */  | 
            ||
| 372 | protected function processLinkSingleton(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload)  | 
            ||
| 446 | }  | 
            ||
| 447 | |||
| 448 | /**  | 
            ||
| 449 | * @param ODataEntry $payload  | 
            ||
| 450 | * @param int $depth  | 
            ||
| 451 | * @return bool  | 
            ||
| 452 | */  | 
            ||
| 453 | protected function isEntryProcessed(ODataEntry $payload, $depth = 0)  | 
            ||
| 483 | }  | 
            ||
| 484 | }  | 
            ||
| 485 |