Total Complexity | 82 |
Total Lines | 459 |
Duplicated Lines | 0 % |
Changes | 10 | ||
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 |
||
18 | class CynicDeserialiser |
||
19 | { |
||
20 | /** |
||
21 | * @var IMetadataProvider |
||
22 | */ |
||
23 | private $metaProvider; |
||
24 | |||
25 | /** |
||
26 | * @var ProvidersWrapper |
||
27 | */ |
||
28 | private $wrapper; |
||
29 | |||
30 | /** |
||
31 | * @var ModelDeserialiser |
||
32 | */ |
||
33 | private $cereal; |
||
34 | |||
35 | /** |
||
36 | * CynicDeserialiser constructor. |
||
37 | * @param IMetadataProvider $meta |
||
38 | * @param ProvidersWrapper $wrapper |
||
39 | */ |
||
40 | public function __construct(IMetadataProvider $meta, ProvidersWrapper $wrapper) |
||
41 | { |
||
42 | $this->metaProvider = $meta; |
||
43 | $this->wrapper = $wrapper; |
||
44 | $this->cereal = new ModelDeserialiser(); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param ODataEntry $payload |
||
49 | * @return mixed |
||
50 | * @throws InvalidOperationException |
||
51 | * @throws \POData\Common\ODataException |
||
52 | * @throws \ReflectionException |
||
53 | */ |
||
54 | public function processPayload(ODataEntry &$payload) |
||
55 | { |
||
56 | $entryOk = $this->isEntryOK($payload); |
||
57 | if (!$entryOk) { |
||
58 | throw new InvalidOperationException('Payload not OK'); |
||
59 | } |
||
60 | list($sourceSet, $source) = $this->processEntryContent($payload); |
||
61 | if (!$sourceSet instanceof ResourceSet) { |
||
|
|||
62 | throw new InvalidOperationException('$sourceSet not instanceof ResourceSet'); |
||
63 | } |
||
64 | $numLinks = count($payload->links); |
||
65 | for ($i = 0; $i < $numLinks; $i++) { |
||
66 | $this->processLink($payload->links[$i], $sourceSet, $source); |
||
67 | } |
||
68 | if (!$this->isEntryProcessed($payload)) { |
||
69 | throw new InvalidOperationException('Payload not processed'); |
||
70 | } |
||
71 | return $source; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Check if supplied ODataEntry is well-formed. |
||
76 | * |
||
77 | * @param ODataEntry $payload |
||
78 | * @return bool |
||
79 | */ |
||
80 | protected function isEntryOK(ODataEntry $payload) |
||
81 | { |
||
82 | // check links |
||
83 | foreach ($payload->links as $link) { |
||
84 | $hasUrl = isset($link->url); |
||
85 | $hasExpanded = isset($link->expandedResult); |
||
86 | if ($hasUrl) { |
||
87 | if (!is_string($link->url)) { |
||
88 | $msg = 'Url must be either string or null'; |
||
89 | throw new \InvalidArgumentException($msg); |
||
90 | } |
||
91 | } |
||
92 | if ($hasExpanded) { |
||
93 | $isGood = $link->expandedResult instanceof ODataEntry || $link->expandedResult instanceof ODataFeed; |
||
94 | if (!$isGood) { |
||
95 | $msg = 'Expanded result must null, or be instance of ODataEntry or ODataFeed'; |
||
96 | throw new \InvalidArgumentException($msg); |
||
97 | } |
||
98 | } |
||
99 | $isEntry = $link->expandedResult instanceof ODataEntry; |
||
100 | |||
101 | if ($hasExpanded) { |
||
102 | if ($isEntry) { |
||
103 | $this->isEntryOK($link->expandedResult); |
||
104 | } else { |
||
105 | foreach ($link->expandedResult->entries as $expanded) { |
||
106 | $this->isEntryOK($expanded); |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | |||
112 | $set = $this->getMetaProvider()->resolveResourceSet($payload->resourceSetName); |
||
113 | if (null === $set) { |
||
114 | $msg = 'Specified resource set could not be resolved'; |
||
115 | throw new \InvalidArgumentException($msg); |
||
116 | } |
||
117 | return true; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param ODataEntry $payload |
||
122 | * @param int $depth |
||
123 | * @return bool |
||
124 | */ |
||
125 | protected function isEntryProcessed(ODataEntry $payload, $depth = 0) |
||
126 | { |
||
127 | assert(is_int($depth) && 0 <= $depth && 100 >= $depth, 'Maximum recursion depth exceeded'); |
||
128 | if (!$payload->id instanceof KeyDescriptor) { |
||
129 | return false; |
||
130 | } |
||
131 | foreach ($payload->links as $link) { |
||
132 | $expand = $link->expandedResult; |
||
133 | if (null === $expand) { |
||
134 | continue; |
||
135 | } |
||
136 | if ($expand instanceof ODataEntry) { |
||
137 | if (!$this->isEntryProcessed($expand, $depth + 1)) { |
||
138 | return false; |
||
139 | } else { |
||
140 | continue; |
||
141 | } |
||
142 | } |
||
143 | if ($expand instanceof ODataFeed) { |
||
144 | foreach ($expand->entries as $entry) { |
||
145 | if (!$this->isEntryProcessed($entry, $depth + 1)) { |
||
146 | return false; |
||
147 | } |
||
148 | } |
||
149 | continue; |
||
150 | } |
||
151 | assert(false, 'Expanded result cannot be processed'); |
||
152 | } |
||
153 | |||
154 | return true; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param ODataEntry $content |
||
159 | * @return array |
||
160 | * @throws InvalidOperationException |
||
161 | * @throws \POData\Common\ODataException |
||
162 | * @throws \ReflectionException |
||
163 | * @throws \Exception |
||
164 | */ |
||
165 | protected function processEntryContent(ODataEntry &$content) |
||
166 | { |
||
167 | assert(null === $content->id || is_string($content->id), 'Entry id must be null or string'); |
||
168 | |||
169 | $isCreate = null === $content->id || empty($content->id); |
||
170 | $set = $this->getMetaProvider()->resolveResourceSet($content->resourceSetName); |
||
171 | assert($set instanceof ResourceSet, get_class($set)); |
||
172 | $type = $set->getResourceType(); |
||
173 | $properties = $this->getDeserialiser()->bulkDeserialise($type, $content); |
||
174 | $properties = (object) $properties; |
||
175 | |||
176 | if ($isCreate) { |
||
177 | $result = $this->getWrapper()->createResourceforResourceSet($set, null, $properties); |
||
178 | assert(isset($result), get_class($result)); |
||
179 | $key = $this->generateKeyDescriptor($type, $result); |
||
180 | $keyProp = $key->getODataProperties(); |
||
181 | foreach ($keyProp as $keyName => $payload) { |
||
182 | $content->propertyContent->properties[$keyName] = $payload; |
||
183 | } |
||
184 | } else { |
||
185 | $key = $this->generateKeyDescriptor($type, $content->propertyContent, $content->id); |
||
186 | assert($key instanceof KeyDescriptor, get_class($key)); |
||
187 | $source = $this->getWrapper()->getResourceFromResourceSet($set, $key); |
||
188 | assert(isset($source), get_class($source)); |
||
189 | $result = $this->getWrapper()->updateResource($set, $source, $key, $properties); |
||
190 | } |
||
191 | if (!$key instanceof KeyDescriptor) { |
||
192 | throw new InvalidOperationException(get_class($key)); |
||
193 | } |
||
194 | $content->id = $key; |
||
195 | |||
196 | $numLinks = count($content->links); |
||
197 | for ($i = 0; $i < $numLinks; $i++) { |
||
198 | $this->processLink($content->links[$i], $set, $result); |
||
199 | } |
||
200 | |||
201 | return [$set, $result]; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @return IMetadataProvider |
||
206 | */ |
||
207 | protected function getMetaProvider() |
||
208 | { |
||
209 | return $this->metaProvider; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @return ProvidersWrapper |
||
214 | */ |
||
215 | protected function getWrapper() |
||
216 | { |
||
217 | return $this->wrapper; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @return ModelDeserialiser |
||
222 | */ |
||
223 | protected function getDeserialiser() |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @param ResourceEntityType $type |
||
230 | * @param ODataPropertyContent|object $result |
||
231 | * @param string|null $id |
||
232 | * @return null|KeyDescriptor |
||
233 | * @throws \POData\Common\ODataException |
||
234 | * @throws \ReflectionException |
||
235 | */ |
||
236 | protected function generateKeyDescriptor(ResourceEntityType $type, $result, $id = null) |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @param ODataLink $link |
||
272 | * @param ResourceSet $sourceSet |
||
273 | * @param $source |
||
274 | * @throws InvalidOperationException |
||
275 | * @throws \POData\Common\ODataException |
||
276 | * @throws \ReflectionException |
||
277 | */ |
||
278 | protected function processLink(ODataLink &$link, ResourceSet $sourceSet, $source) |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param ODataLink $link |
||
304 | * @param ResourceSet $sourceSet |
||
305 | * @param $source |
||
306 | * @param $hasUrl |
||
307 | * @param $hasPayload |
||
308 | * @throws InvalidOperationException |
||
309 | * @throws \POData\Common\ODataException |
||
310 | * @throws \ReflectionException |
||
311 | */ |
||
312 | protected function processLinkSingleton(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload) |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @param ODataLink $link |
||
390 | * @param ResourceSet $sourceSet |
||
391 | * @param $source |
||
392 | * @param bool $hasUrl |
||
393 | * @param bool $hasPayload |
||
394 | * @throws InvalidOperationException |
||
395 | * @throws \POData\Common\ODataException |
||
396 | * @throws \ReflectionException |
||
397 | * @throws \Exception |
||
398 | */ |
||
399 | protected function processLinkFeed(ODataLink &$link, ResourceSet $sourceSet, $source, $hasUrl, $hasPayload) |
||
479 |