| Conditions | 8 |
| Paths | 32 |
| Total Lines | 171 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 30 | public function visit(Visitor $visitor, Generator $generator, $data) |
||
| 31 | { |
||
| 32 | $restContent = $data; |
||
| 33 | $contentInfo = $restContent->contentInfo; |
||
| 34 | $contentType = $restContent->contentType; |
||
| 35 | $mainLocation = $restContent->mainLocation; |
||
| 36 | $currentVersion = $restContent->currentVersion; |
||
| 37 | |||
| 38 | $mediaType = ($restContent->currentVersion === null ? 'ContentInfo' : 'Content'); |
||
| 39 | |||
| 40 | $generator->startObjectElement('Content', $mediaType); |
||
| 41 | |||
| 42 | $visitor->setHeader('Content-Type', $generator->getMediaType($mediaType)); |
||
| 43 | $visitor->setHeader('Accept-Patch', $generator->getMediaType('ContentUpdate')); |
||
| 44 | |||
| 45 | $generator->startAttribute( |
||
| 46 | 'href', |
||
| 47 | $data->path === null ? |
||
| 48 | $this->router->generate('ezpublish_rest_loadContent', array('contentId' => $contentInfo->id)) : |
||
| 49 | $data->path |
||
| 50 | ); |
||
| 51 | $generator->endAttribute('href'); |
||
| 52 | |||
| 53 | $generator->startAttribute('remoteId', $contentInfo->remoteId); |
||
| 54 | $generator->endAttribute('remoteId'); |
||
| 55 | $generator->startAttribute('id', $contentInfo->id); |
||
| 56 | $generator->endAttribute('id'); |
||
| 57 | |||
| 58 | $generator->startObjectElement('ContentType'); |
||
| 59 | $generator->startAttribute( |
||
| 60 | 'href', |
||
| 61 | $this->router->generate( |
||
| 62 | 'ezpublish_rest_loadContentType', |
||
| 63 | array('contentTypeId' => $contentInfo->contentTypeId) |
||
| 64 | ) |
||
| 65 | ); |
||
| 66 | $generator->endAttribute('href'); |
||
| 67 | $generator->endObjectElement('ContentType'); |
||
| 68 | |||
| 69 | $generator->startValueElement('Name', $contentInfo->name); |
||
| 70 | $generator->endValueElement('Name'); |
||
| 71 | |||
| 72 | $generator->startObjectElement('Versions', 'VersionList'); |
||
| 73 | $generator->startAttribute( |
||
| 74 | 'href', |
||
| 75 | $this->router->generate('ezpublish_rest_loadContentVersions', array('contentId' => $contentInfo->id)) |
||
| 76 | ); |
||
| 77 | $generator->endAttribute('href'); |
||
| 78 | $generator->endObjectElement('Versions'); |
||
| 79 | |||
| 80 | $generator->startObjectElement('CurrentVersion', 'Version'); |
||
| 81 | $generator->startAttribute( |
||
| 82 | 'href', |
||
| 83 | $this->router->generate( |
||
| 84 | 'ezpublish_rest_redirectCurrentVersion', |
||
| 85 | array('contentId' => $contentInfo->id) |
||
| 86 | ) |
||
| 87 | ); |
||
| 88 | $generator->endAttribute('href'); |
||
| 89 | |||
| 90 | // Embed current version, if available |
||
| 91 | if ($currentVersion !== null) { |
||
| 92 | $visitor->visitValueObject( |
||
| 93 | new VersionValue( |
||
| 94 | $currentVersion, |
||
| 95 | $contentType, |
||
| 96 | $restContent->relations |
||
| 97 | ) |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | |||
| 101 | $generator->endObjectElement('CurrentVersion'); |
||
| 102 | |||
| 103 | $generator->startObjectElement('Section'); |
||
| 104 | $generator->startAttribute( |
||
| 105 | 'href', |
||
| 106 | $this->router->generate('ezpublish_rest_loadSection', array('sectionId' => $contentInfo->sectionId)) |
||
| 107 | ); |
||
| 108 | $generator->endAttribute('href'); |
||
| 109 | $generator->endObjectElement('Section'); |
||
| 110 | |||
| 111 | // Main location will not exist if we're visiting the content draft |
||
| 112 | if ($data->mainLocation !== null) { |
||
| 113 | $generator->startObjectElement('MainLocation', 'Location'); |
||
| 114 | $generator->startAttribute( |
||
| 115 | 'href', |
||
| 116 | $this->router->generate( |
||
| 117 | 'ezpublish_rest_loadLocation', |
||
| 118 | array('locationPath' => trim($mainLocation->pathString, '/')) |
||
| 119 | ) |
||
| 120 | ); |
||
| 121 | $generator->endAttribute('href'); |
||
| 122 | $generator->endObjectElement('MainLocation'); |
||
| 123 | } |
||
| 124 | |||
| 125 | $generator->startObjectElement('Locations', 'LocationList'); |
||
| 126 | $generator->startAttribute( |
||
| 127 | 'href', |
||
| 128 | $this->router->generate( |
||
| 129 | 'ezpublish_rest_loadLocationsForContent', |
||
| 130 | array('contentId' => $contentInfo->id) |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | $generator->endAttribute('href'); |
||
| 134 | $generator->endObjectElement('Locations'); |
||
| 135 | |||
| 136 | $generator->startObjectElement('Owner', 'User'); |
||
| 137 | $generator->startAttribute( |
||
| 138 | 'href', |
||
| 139 | $this->router->generate('ezpublish_rest_loadUser', array('userId' => $contentInfo->ownerId)) |
||
| 140 | ); |
||
| 141 | $generator->endAttribute('href'); |
||
| 142 | $generator->endObjectElement('Owner'); |
||
| 143 | |||
| 144 | // Modification date will not exist if we're visiting the content draft |
||
| 145 | if ($contentInfo->modificationDate !== null) { |
||
| 146 | $generator->startValueElement( |
||
| 147 | 'lastModificationDate', |
||
| 148 | $contentInfo->modificationDate->format('c') |
||
| 149 | ); |
||
| 150 | $generator->endValueElement('lastModificationDate'); |
||
| 151 | } |
||
| 152 | |||
| 153 | // Published date will not exist if we're visiting the content draft |
||
| 154 | if ($contentInfo->publishedDate !== null) { |
||
| 155 | $generator->startValueElement( |
||
| 156 | 'publishedDate', |
||
| 157 | ($contentInfo->publishedDate !== null |
||
| 158 | ? $contentInfo->publishedDate->format('c') |
||
| 159 | : null) |
||
| 160 | ); |
||
| 161 | $generator->endValueElement('publishedDate'); |
||
| 162 | } |
||
| 163 | |||
| 164 | $generator->startValueElement( |
||
| 165 | 'mainLanguageCode', |
||
| 166 | $contentInfo->mainLanguageCode |
||
| 167 | ); |
||
| 168 | $generator->endValueElement('mainLanguageCode'); |
||
| 169 | |||
| 170 | $generator->startValueElement( |
||
| 171 | 'currentVersionNo', |
||
| 172 | $contentInfo->currentVersionNo |
||
| 173 | ); |
||
| 174 | $generator->endValueElement('currentVersionNo'); |
||
| 175 | |||
| 176 | $generator->startValueElement( |
||
| 177 | 'alwaysAvailable', |
||
| 178 | $this->serializeBool($generator, $contentInfo->alwaysAvailable) |
||
| 179 | ); |
||
| 180 | $generator->endValueElement('alwaysAvailable'); |
||
| 181 | |||
| 182 | $generator->startValueElement( |
||
| 183 | 'status', |
||
| 184 | $this->getStatusString($contentInfo->status) |
||
| 185 | ); |
||
| 186 | $generator->endValueElement('status'); |
||
| 187 | |||
| 188 | $generator->startObjectElement('ObjectStates', 'ContentObjectStates'); |
||
| 189 | $generator->startAttribute( |
||
| 190 | 'href', |
||
| 191 | $this->router->generate( |
||
| 192 | 'ezpublish_rest_getObjectStatesForContent', |
||
| 193 | array('contentId' => $contentInfo->id) |
||
| 194 | ) |
||
| 195 | ); |
||
| 196 | $generator->endAttribute('href'); |
||
| 197 | $generator->endObjectElement('ObjectStates'); |
||
| 198 | |||
| 199 | $generator->endObjectElement('Content'); |
||
| 200 | } |
||
| 201 | |||
| 227 |