| Total Complexity | 53 |
| Total Lines | 503 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 25 | class JsonODataV1Writer implements IODataWriter |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Json output writer. |
||
| 29 | * |
||
| 30 | */ |
||
| 31 | protected $_writer; |
||
| 32 | |||
| 33 | |||
| 34 | protected $urlKey = ODataConstants::JSON_URI_STRING; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Constructs and initializes the Json output writer. |
||
| 38 | * |
||
| 39 | */ |
||
| 40 | public function __construct() |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Determines if the given writer is capable of writing the response or not |
||
| 47 | * @param Version $responseVersion the OData version of the response |
||
| 48 | * @param string $contentType the Content Type of the response |
||
| 49 | * @return boolean true if the writer can handle the response, false otherwise |
||
| 50 | */ |
||
| 51 | public function canHandle(Version $responseVersion, $contentType) |
||
| 52 | { |
||
| 53 | if ($responseVersion != Version::v1()) { |
||
| 54 | return false; |
||
| 55 | } |
||
| 56 | |||
| 57 | $parts = explode(";", $contentType); |
||
| 58 | |||
| 59 | return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Write the given OData model in a specific response format |
||
| 64 | * |
||
| 65 | * @param ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content. |
||
| 66 | * |
||
| 67 | * @return JsonODataV1Writer |
||
| 68 | */ |
||
| 69 | public function write($model) { |
||
| 98 | } |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @param ODataURL $url the url to write |
||
| 103 | * |
||
| 104 | * @return JsonODataV1Writer |
||
| 105 | */ |
||
| 106 | public function writeUrl(ODataURL $url) |
||
| 107 | { |
||
| 108 | $this->_writer |
||
| 109 | ->writeName($this->urlKey) |
||
| 110 | ->writeValue($url->url); |
||
| 111 | |||
| 112 | return $this; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * begin write OData links |
||
| 117 | * |
||
| 118 | * @param ODataURLCollection $urls url collection to write |
||
| 119 | * |
||
| 120 | * @return JsonODataV1Writer |
||
| 121 | */ |
||
| 122 | public function writeUrlCollection(ODataURLCollection $urls) |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Start writing a feed |
||
| 134 | * |
||
| 135 | * @param ODataFeed $feed Feed to write |
||
| 136 | * |
||
| 137 | * @return JsonODataV1Writer |
||
| 138 | */ |
||
| 139 | protected function writeFeed(ODataFeed $feed) |
||
| 150 | } |
||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * @param ODataEntry $entry Entry to write |
||
| 157 | * |
||
| 158 | * @return JsonODataV1Writer |
||
| 159 | */ |
||
| 160 | protected function writeEntry(ODataEntry $entry) |
||
| 161 | { |
||
| 162 | |||
| 163 | $this->writeEntryMetadata($entry); |
||
| 164 | foreach ($entry->links as $link) { |
||
| 165 | $this->writeLink($link); |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->writeCustomProperties($entry->customProperties); |
||
| 169 | |||
| 170 | $this->writeProperties($entry->propertyContent); |
||
| 171 | |||
| 172 | |||
| 173 | return $this; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Write metadata information for the entry. |
||
| 178 | * |
||
| 179 | * @param ODataEntry $entry Entry to write metadata for. |
||
| 180 | * |
||
| 181 | * @return JsonODataV1Writer |
||
| 182 | */ |
||
| 183 | protected function writeEntryMetadata(ODataEntry $entry) |
||
| 261 | } |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @param ODataLink $link Link to write. |
||
| 266 | * |
||
| 267 | * @return JsonODataV1Writer |
||
| 268 | */ |
||
| 269 | protected function writeLink(ODataLink $link) |
||
| 270 | { |
||
| 271 | |||
| 272 | // "<linkname>" : |
||
| 273 | $this->_writer->writeName($link->title); |
||
| 274 | |||
| 275 | if ($link->isExpanded) { |
||
| 276 | if (is_null($link->expandedResult)) { |
||
| 277 | $this->_writer->writeValue("null"); |
||
| 278 | } else { |
||
| 279 | $this->writeExpandedLink($link); |
||
| 280 | } |
||
| 281 | } else { |
||
| 282 | $this->_writer |
||
| 283 | ->startObjectScope() |
||
| 284 | ->writeName(ODataConstants::JSON_DEFERRED_STRING) |
||
| 285 | ->startObjectScope() |
||
| 286 | ->writeName($this->urlKey) |
||
| 287 | ->writeValue($link->url) |
||
| 288 | ->endScope() |
||
| 289 | ->endScope() |
||
| 290 | ; |
||
| 291 | } |
||
| 292 | |||
| 293 | return $this; |
||
| 294 | } |
||
| 295 | |||
| 296 | protected function writeExpandedLink(ODataLink $link) |
||
| 297 | { |
||
| 298 | if ($link->isCollection) { |
||
| 299 | $this->_writer->startArrayScope(); |
||
| 300 | $this->writeFeed($link->expandedResult); |
||
| 301 | } else { |
||
| 302 | $this->_writer->startObjectScope(); |
||
| 303 | $this->writeEntry($link->expandedResult); |
||
| 304 | } |
||
| 305 | |||
| 306 | $this->_writer->endScope(); |
||
| 307 | } |
||
| 308 | |||
| 309 | |||
| 310 | /** |
||
| 311 | * Write the given collection of properties. |
||
| 312 | * (properties of an entity or complex type) |
||
| 313 | * |
||
| 314 | * @param ODataPropertyContent $properties Collection of properties. |
||
| 315 | * |
||
| 316 | * @return JsonODataV1Writer |
||
| 317 | */ |
||
| 318 | protected function writeCustomProperties(ODataPropertyContent $properties) |
||
| 319 | {
|
||
| 320 | foreach ($properties->properties as $name => $property) {
|
||
| 321 | |||
| 322 | $this->writePropertyMeta($property); |
||
| 323 | $this->_writer->writeName($property->name); |
||
| 324 | |||
| 325 | if ($property->value == null) {
|
||
| 326 | $this->_writer->writeValue("null");
|
||
| 327 | } elseif ($property->value instanceof ODataPropertyContent) {
|
||
| 328 | $this->writeComplexProperty($property); |
||
| 329 | } elseif ($property->value instanceof ODataBagContent) {
|
||
| 330 | $this->writeBagContent($property->value); |
||
| 331 | } else {
|
||
| 332 | $this->_writer->writeValue($property->value, $property->typeName); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | return $this; |
||
| 336 | } |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * Write the given collection of properties. |
||
| 341 | * (properties of an entity or complex type) |
||
| 342 | * |
||
| 343 | * @param ODataPropertyContent $properties Collection of properties. |
||
| 344 | * |
||
| 345 | * @return JsonODataV1Writer |
||
| 346 | */ |
||
| 347 | protected function writeProperties(ODataPropertyContent $properties) |
||
| 348 | { |
||
| 349 | foreach ($properties->properties as $property) { |
||
| 350 | |||
| 351 | $this->writePropertyMeta($property); |
||
| 352 | $this->_writer->writeName($property->name); |
||
| 353 | |||
| 354 | if ($property->value == null) { |
||
| 355 | $this->_writer->writeValue("null"); |
||
| 356 | } elseif ($property->value instanceof ODataPropertyContent) { |
||
| 357 | $this->writeComplexProperty($property); |
||
| 358 | } elseif ($property->value instanceof ODataBagContent) { |
||
| 359 | $this->writeBagContent($property->value); |
||
| 360 | } else { |
||
| 361 | $this->_writer->writeValue($property->value, $property->typeName); |
||
| 362 | } |
||
| 363 | |||
| 364 | } |
||
| 365 | |||
| 366 | return $this; |
||
| 367 | } |
||
| 368 | |||
| 369 | |||
| 370 | protected function writePropertyMeta(ODataProperty $property) |
||
| 371 | { |
||
| 372 | return $this; //does nothing in v1 or v2, json light outputs stuff |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Begin write complex property. |
||
| 377 | * |
||
| 378 | * @param ODataProperty $property property to write. |
||
| 379 | * |
||
| 380 | * @return JsonODataV1Writer |
||
| 381 | */ |
||
| 382 | protected function writeComplexProperty(ODataProperty $property) |
||
| 383 | { |
||
| 384 | |||
| 385 | $this->_writer |
||
| 386 | // { |
||
| 387 | ->startObjectScope() |
||
| 388 | |||
| 389 | // __metadata : { Type : "typename" } |
||
| 390 | ->writeName(ODataConstants::JSON_METADATA_STRING) |
||
| 391 | ->startObjectScope() |
||
| 392 | ->writeName(ODataConstants::JSON_TYPE_STRING) |
||
| 393 | ->writeValue($property->typeName) |
||
| 394 | ->endScope(); |
||
| 395 | |||
| 396 | $this->writeProperties($property->value); |
||
| 397 | |||
| 398 | $this->_writer->endScope(); |
||
| 399 | |||
| 400 | return $this; |
||
| 401 | } |
||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * Begin an item in a collection |
||
| 406 | * |
||
| 407 | * @param ODataBagContent $bag bag property to write |
||
| 408 | * |
||
| 409 | * @return JsonODataV1Writer |
||
| 410 | */ |
||
| 411 | protected function writeBagContent(ODataBagContent $bag) |
||
| 443 | } |
||
| 444 | |||
| 445 | |||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * serialize exception. |
||
| 450 | * |
||
| 451 | * @param ODataException $exception Exception to serialize |
||
| 452 | * @param Boolean $serializeInnerException if set to true |
||
| 453 | * |
||
| 454 | * serialize the inner exception if $exception is an ODataException. |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | public static function serializeException(ODataException $exception, $serializeInnerException) |
||
| 488 | } |
||
| 489 | |||
| 490 | |||
| 491 | |||
| 492 | /** |
||
| 493 | * Get the Json final output. |
||
| 494 | * |
||
| 495 | * @return string |
||
| 496 | */ |
||
| 497 | public function getOutput() |
||
| 500 | } |
||
| 501 | |||
| 502 | |||
| 503 | /** |
||
| 504 | * @param ProvidersWrapper $providers |
||
| 505 | * @return IODataWriter |
||
| 506 | */ |
||
| 507 | public function writeServiceDocument(ProvidersWrapper $providers) { |
||
| 528 | |||
| 529 | } |
||
| 530 | } |