| Total Complexity | 49 | 
| Total Lines | 467 | 
| Duplicated Lines | 0 % | 
| Changes | 2 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like JsonODataV1Writer 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 JsonODataV1Writer, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 28 | class JsonODataV1Writer implements IODataWriter  | 
            ||
| 29 | { | 
            ||
| 30 | /**  | 
            ||
| 31 | * Json output writer.  | 
            ||
| 32 | */  | 
            ||
| 33 | protected $writer;  | 
            ||
| 34 | |||
| 35 | protected $urlKey = ODataConstants::JSON_URI_STRING;  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * Constructs and initializes the Json output writer.  | 
            ||
| 39 | */  | 
            ||
| 40 | public function __construct(string $eol, bool $prettyPrint)  | 
            ||
| 43 | }  | 
            ||
| 44 | |||
| 45 | /**  | 
            ||
| 46 | * serialize exception.  | 
            ||
| 47 | *  | 
            ||
| 48 | * @param ODataException $exception Exception to serialize  | 
            ||
| 49 | *  | 
            ||
| 50 | * serialize the inner exception if $exception is an ODataException  | 
            ||
| 51 | *  | 
            ||
| 52 | * @throws Exception  | 
            ||
| 53 | * @return string  | 
            ||
| 54 | */  | 
            ||
| 55 | public static function serializeException(ODataException $exception, ServiceConfiguration $config)  | 
            ||
| 56 |     { | 
            ||
| 57 |         $writer = new JsonWriter('', $config->getLineEndings(), $config->getPrettyOutput()); | 
            ||
| 58 | // Wrapper for error.  | 
            ||
| 59 | $writer  | 
            ||
| 60 | ->startObjectScope()  | 
            ||
| 61 | ->writeName(ODataConstants::JSON_ERROR)// "error"  | 
            ||
| 62 | ->startObjectScope();  | 
            ||
| 63 | |||
| 64 | // "code"  | 
            ||
| 65 |         if (null !== $exception->getCode()) { | 
            ||
| 66 | $writer  | 
            ||
| 67 | ->writeName(ODataConstants::JSON_ERROR_CODE)  | 
            ||
| 68 | ->writeValue($exception->getCode());  | 
            ||
| 69 | }  | 
            ||
| 70 | |||
| 71 | // "message"  | 
            ||
| 72 | $writer  | 
            ||
| 73 | ->writeName(ODataConstants::JSON_ERROR_MESSAGE)  | 
            ||
| 74 | ->startObjectScope()  | 
            ||
| 75 | ->writeName(ODataConstants::XML_LANG_ATTRIBUTE_NAME)// "lang"  | 
            ||
| 76 |             ->writeValue('en-US') | 
            ||
| 77 | ->writeName(ODataConstants::JSON_ERROR_VALUE)  | 
            ||
| 78 | ->writeValue($exception->getMessage())  | 
            ||
| 79 | ->endScope()  | 
            ||
| 80 | ->endScope()  | 
            ||
| 81 | ->endScope();  | 
            ||
| 82 | |||
| 83 | return $writer->getJsonOutput();  | 
            ||
| 84 | }  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * Determines if the given writer is capable of writing the response or not.  | 
            ||
| 88 | *  | 
            ||
| 89 | * @param Version $responseVersion the OData version of the response  | 
            ||
| 90 | * @param string $contentType the Content Type of the response  | 
            ||
| 91 | *  | 
            ||
| 92 | * @return bool true if the writer can handle the response, false otherwise  | 
            ||
| 93 | */  | 
            ||
| 94 | public function canHandle(Version $responseVersion, $contentType)  | 
            ||
| 95 |     { | 
            ||
| 96 |         if ($responseVersion != Version::v1()) { | 
            ||
| 97 | return false;  | 
            ||
| 98 | }  | 
            ||
| 99 | |||
| 100 |         $parts = explode(';', $contentType); | 
            ||
| 101 | |||
| 102 | return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts);  | 
            ||
| 103 | }  | 
            ||
| 104 | |||
| 105 | /**  | 
            ||
| 106 | * Write the given OData model in a specific response format.  | 
            ||
| 107 | *  | 
            ||
| 108 | * @param ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content  | 
            ||
| 109 | *  | 
            ||
| 110 | * @throws Exception  | 
            ||
| 111 | * @return JsonODataV1Writer  | 
            ||
| 112 | */  | 
            ||
| 113 | public function write($model)  | 
            ||
| 114 |     { | 
            ||
| 115 |         // { "d" : | 
            ||
| 116 | $this->writer  | 
            ||
| 117 | ->startObjectScope()  | 
            ||
| 118 |             ->writeName('d'); | 
            ||
| 119 | |||
| 120 |         if ($model instanceof ODataURL) { | 
            ||
| 121 | $this->writer->startObjectScope();  | 
            ||
| 122 | $this->writeUrl($model);  | 
            ||
| 123 |         } elseif ($model instanceof ODataURLCollection) { | 
            ||
| 124 | $this->writer->startArrayScope();  | 
            ||
| 125 | $this->writeUrlCollection($model);  | 
            ||
| 126 |         } elseif ($model instanceof ODataPropertyContent) { | 
            ||
| 127 | $this->writer->startObjectScope();  | 
            ||
| 128 | $this->writeProperties($model);  | 
            ||
| 129 |         } elseif ($model instanceof ODataFeed) { | 
            ||
| 130 | $this->writer->startArrayScope();  | 
            ||
| 131 | $this->writeFeed($model);  | 
            ||
| 132 |         } elseif ($model instanceof ODataEntry) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 133 | $this->writer->startObjectScope();  | 
            ||
| 134 | $this->writeEntry($model);  | 
            ||
| 135 | }  | 
            ||
| 136 | |||
| 137 | $this->writer->endScope();  | 
            ||
| 138 | $this->writer->endScope();  | 
            ||
| 139 | |||
| 140 | return $this;  | 
            ||
| 141 | }  | 
            ||
| 142 | |||
| 143 | /**  | 
            ||
| 144 | * @param ODataURL $url the url to write  | 
            ||
| 145 | *  | 
            ||
| 146 | * @throws Exception  | 
            ||
| 147 | * @return JsonODataV1Writer  | 
            ||
| 148 | */  | 
            ||
| 149 | public function writeUrl(ODataURL $url)  | 
            ||
| 150 |     { | 
            ||
| 151 | $this->writer  | 
            ||
| 152 | ->writeName($this->urlKey)  | 
            ||
| 153 | ->writeValue($url->getUrl());  | 
            ||
| 154 | |||
| 155 | return $this;  | 
            ||
| 156 | }  | 
            ||
| 157 | |||
| 158 | /**  | 
            ||
| 159 | * begin write OData links.  | 
            ||
| 160 | *  | 
            ||
| 161 | * @param ODataURLCollection $urls url collection to write  | 
            ||
| 162 | *  | 
            ||
| 163 | * @throws Exception  | 
            ||
| 164 | * @return JsonODataV1Writer  | 
            ||
| 165 | */  | 
            ||
| 166 | public function writeUrlCollection(ODataURLCollection $urls)  | 
            ||
| 167 |     { | 
            ||
| 168 |         foreach ($urls->getUrls() as $url) { | 
            ||
| 169 | $this->writer->startObjectScope();  | 
            ||
| 170 | $this->writeUrl($url);  | 
            ||
| 171 | $this->writer->endScope();  | 
            ||
| 172 | }  | 
            ||
| 173 | |||
| 174 | return $this;  | 
            ||
| 175 | }  | 
            ||
| 176 | |||
| 177 | /**  | 
            ||
| 178 | * Write the given collection of properties.  | 
            ||
| 179 | * (properties of an entity or complex type).  | 
            ||
| 180 | *  | 
            ||
| 181 | * @param ODataPropertyContent $properties Collection of properties  | 
            ||
| 182 | *  | 
            ||
| 183 | * @throws Exception  | 
            ||
| 184 | * @return JsonODataV1Writer  | 
            ||
| 185 | */  | 
            ||
| 186 | protected function writeProperties(ODataPropertyContent $properties = null)  | 
            ||
| 187 |     { | 
            ||
| 188 |         if (null !== $properties) { | 
            ||
| 189 |             foreach ($properties as $property) { | 
            ||
| 190 | $this->writePropertyMeta($property);  | 
            ||
| 191 | $this->writer->writeName($property->getName());  | 
            ||
| 192 | |||
| 193 |                 if ($property->getValue() == null) { | 
            ||
| 194 |                     $this->writer->writeValue('null'); | 
            ||
| 195 |                 } elseif ($property->getValue() instanceof ODataPropertyContent) { | 
            ||
| 196 | $this->writeComplexProperty($property);  | 
            ||
| 197 |                 } elseif ($property->getValue() instanceof ODataBagContent) { | 
            ||
| 198 | $this->writeBagContent($property->getValue());  | 
            ||
| 199 |                 } else { | 
            ||
| 200 | $this->writer->writeValue($property->getValue(), $property->getTypeName());  | 
            ||
| 201 | }  | 
            ||
| 202 | }  | 
            ||
| 203 | }  | 
            ||
| 204 | |||
| 205 | return $this;  | 
            ||
| 206 | }  | 
            ||
| 207 | |||
| 208 | /**  | 
            ||
| 209 | * @param ODataProperty $property  | 
            ||
| 210 | * @return $this  | 
            ||
| 211 | */  | 
            ||
| 212 | protected function writePropertyMeta(ODataProperty $property)  | 
            ||
| 213 |     { | 
            ||
| 214 | return $this; //does nothing in v1 or v2, json light outputs stuff  | 
            ||
| 215 | }  | 
            ||
| 216 | |||
| 217 | /**  | 
            ||
| 218 | * Begin write complex property.  | 
            ||
| 219 | *  | 
            ||
| 220 | * @param ODataProperty $property property to write  | 
            ||
| 221 | *  | 
            ||
| 222 | * @throws Exception  | 
            ||
| 223 | * @return JsonODataV1Writer  | 
            ||
| 224 | */  | 
            ||
| 225 | protected function writeComplexProperty(ODataProperty $property)  | 
            ||
| 226 |     { | 
            ||
| 227 | $this->writer  | 
            ||
| 228 |             // { | 
            ||
| 229 | ->startObjectScope()  | 
            ||
| 230 |             // __metadata : { Type : "typename" } | 
            ||
| 231 | ->writeName(ODataConstants::JSON_METADATA_STRING)  | 
            ||
| 232 | ->startObjectScope()  | 
            ||
| 233 | ->writeName(ODataConstants::JSON_TYPE_STRING)  | 
            ||
| 234 | ->writeValue($property->getTypeName())  | 
            ||
| 235 | ->endScope();  | 
            ||
| 236 | |||
| 237 | $this->writeProperties($property->getValue());  | 
            ||
| 238 | |||
| 239 | $this->writer->endScope();  | 
            ||
| 240 | |||
| 241 | return $this;  | 
            ||
| 242 | }  | 
            ||
| 243 | |||
| 244 | /**  | 
            ||
| 245 | * Begin an item in a collection.  | 
            ||
| 246 | *  | 
            ||
| 247 | * @param ODataBagContent $bag bag property to write  | 
            ||
| 248 | *  | 
            ||
| 249 | * @throws Exception  | 
            ||
| 250 | * @return JsonODataV1Writer  | 
            ||
| 251 | */  | 
            ||
| 252 | protected function writeBagContent(ODataBagContent $bag)  | 
            ||
| 281 | }  | 
            ||
| 282 | |||
| 283 | /**  | 
            ||
| 284 | * Start writing a feed.  | 
            ||
| 285 | *  | 
            ||
| 286 | * @param ODataFeed $feed Feed to write  | 
            ||
| 287 | *  | 
            ||
| 288 | * @throws Exception  | 
            ||
| 289 | * @return JsonODataV1Writer  | 
            ||
| 290 | */  | 
            ||
| 291 | protected function writeFeed(ODataFeed $feed)  | 
            ||
| 292 |     { | 
            ||
| 293 |         foreach ($feed->getEntries() as $entry) { | 
            ||
| 294 | $this->writer->startObjectScope();  | 
            ||
| 295 | $this->writeEntry($entry);  | 
            ||
| 296 | $this->writer->endScope();  | 
            ||
| 297 | }  | 
            ||
| 298 | |||
| 299 | return $this;  | 
            ||
| 300 | }  | 
            ||
| 301 | |||
| 302 | /**  | 
            ||
| 303 | * @param ODataEntry $entry Entry to write  | 
            ||
| 304 | *  | 
            ||
| 305 | * @throws Exception  | 
            ||
| 306 | * @return JsonODataV1Writer  | 
            ||
| 307 | */  | 
            ||
| 308 | protected function writeEntry(ODataEntry $entry)  | 
            ||
| 318 | }  | 
            ||
| 319 | |||
| 320 | /**  | 
            ||
| 321 | * Write metadata information for the entry.  | 
            ||
| 322 | *  | 
            ||
| 323 | * @param ODataEntry $entry Entry to write metadata for  | 
            ||
| 324 | *  | 
            ||
| 325 | * @throws Exception  | 
            ||
| 326 | * @return JsonODataV1Writer  | 
            ||
| 327 | */  | 
            ||
| 328 | protected function writeEntryMetadata(ODataEntry $entry)  | 
            ||
| 403 | }  | 
            ||
| 404 | |||
| 405 | /**  | 
            ||
| 406 | * @param ODataLink $link Link to write  | 
            ||
| 407 | *  | 
            ||
| 408 | * @throws Exception  | 
            ||
| 409 | * @return JsonODataV1Writer  | 
            ||
| 410 | */  | 
            ||
| 411 | protected function writeLink(ODataLink $link)  | 
            ||
| 412 |     { | 
            ||
| 413 | |||
| 414 | // "<linkname>" :  | 
            ||
| 415 | $this->writer->writeName($link->getTitle());  | 
            ||
| 416 | |||
| 417 |         if ($link->isExpanded()) { | 
            ||
| 418 |             if (null === $link->getExpandedResult() || null === $link->getExpandedResult()->getData()) { | 
            ||
| 419 |                 $this->writer->writeValue('null'); | 
            ||
| 420 |             } else { | 
            ||
| 421 | $this->writeExpandedLink($link);  | 
            ||
| 422 | }  | 
            ||
| 423 |         } else { | 
            ||
| 424 | $this->writer  | 
            ||
| 425 | ->startObjectScope()  | 
            ||
| 426 | ->writeName(ODataConstants::JSON_DEFERRED_STRING)  | 
            ||
| 427 | ->startObjectScope()  | 
            ||
| 428 | ->writeName($this->urlKey)  | 
            ||
| 429 | ->writeValue($link->getUrl())  | 
            ||
| 430 | ->endScope()  | 
            ||
| 431 | ->endScope();  | 
            ||
| 432 | }  | 
            ||
| 433 | |||
| 434 | return $this;  | 
            ||
| 435 | }  | 
            ||
| 436 | |||
| 437 | /**  | 
            ||
| 438 | * @param ODataLink $link  | 
            ||
| 439 | * @throws Exception  | 
            ||
| 440 | */  | 
            ||
| 441 | protected function writeExpandedLink(ODataLink $link)  | 
            ||
| 452 | }  | 
            ||
| 453 | |||
| 454 | /**  | 
            ||
| 455 | * Get the Json final output.  | 
            ||
| 456 | *  | 
            ||
| 457 | * @return string  | 
            ||
| 458 | */  | 
            ||
| 459 | public function getOutput()  | 
            ||
| 462 | }  | 
            ||
| 463 | |||
| 464 | /**  | 
            ||
| 465 | * @param ProvidersWrapper $providers  | 
            ||
| 466 | *  | 
            ||
| 467 | * @throws Exception  | 
            ||
| 468 | * @return IODataWriter  | 
            ||
| 469 | */  | 
            ||
| 470 | public function writeServiceDocument(ProvidersWrapper $providers)  | 
            ||
| 497 |