Conditions | 8 |
Paths | 32 |
Total Lines | 181 |
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 |
||
39 | public function visit(Visitor $visitor, Generator $generator, $data) |
||
40 | { |
||
41 | $restContent = $data; |
||
42 | $contentInfo = $restContent->contentInfo; |
||
43 | $translatedContentName = $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo); |
||
44 | $contentType = $restContent->contentType; |
||
45 | $mainLocation = $restContent->mainLocation; |
||
46 | $currentVersion = $restContent->currentVersion; |
||
47 | |||
48 | $mediaType = ($restContent->currentVersion === null ? 'ContentInfo' : 'Content'); |
||
49 | |||
50 | $generator->startObjectElement('Content', $mediaType); |
||
51 | |||
52 | $visitor->setHeader('Content-Type', $generator->getMediaType($mediaType)); |
||
53 | $visitor->setHeader('Accept-Patch', $generator->getMediaType('ContentUpdate')); |
||
54 | |||
55 | $generator->startAttribute( |
||
56 | 'href', |
||
57 | $data->path === null ? |
||
58 | $this->router->generate('ezpublish_rest_loadContent', ['contentId' => $contentInfo->id]) : |
||
59 | $data->path |
||
60 | ); |
||
61 | $generator->endAttribute('href'); |
||
62 | |||
63 | $generator->startAttribute('remoteId', $contentInfo->remoteId); |
||
64 | $generator->endAttribute('remoteId'); |
||
65 | $generator->startAttribute('id', $contentInfo->id); |
||
66 | $generator->endAttribute('id'); |
||
67 | |||
68 | $generator->startObjectElement('ContentType'); |
||
69 | $generator->startAttribute( |
||
70 | 'href', |
||
71 | $this->router->generate( |
||
72 | 'ezpublish_rest_loadContentType', |
||
73 | ['contentTypeId' => $contentInfo->contentTypeId] |
||
74 | ) |
||
75 | ); |
||
76 | $generator->endAttribute('href'); |
||
77 | $generator->endObjectElement('ContentType'); |
||
78 | |||
79 | $generator->startValueElement('Name', $contentInfo->name); |
||
80 | $generator->endValueElement('Name'); |
||
81 | |||
82 | $generator->startValueElement('TranslatedName', $translatedContentName); |
||
83 | $generator->endValueElement('TranslatedName'); |
||
84 | |||
85 | $generator->startObjectElement('Versions', 'VersionList'); |
||
86 | $generator->startAttribute( |
||
87 | 'href', |
||
88 | $this->router->generate('ezpublish_rest_loadContentVersions', ['contentId' => $contentInfo->id]) |
||
89 | ); |
||
90 | $generator->endAttribute('href'); |
||
91 | $generator->endObjectElement('Versions'); |
||
92 | |||
93 | $generator->startObjectElement('CurrentVersion', 'Version'); |
||
94 | $generator->startAttribute( |
||
95 | 'href', |
||
96 | $this->router->generate( |
||
97 | 'ezpublish_rest_redirectCurrentVersion', |
||
98 | ['contentId' => $contentInfo->id] |
||
99 | ) |
||
100 | ); |
||
101 | $generator->endAttribute('href'); |
||
102 | |||
103 | // Embed current version, if available |
||
104 | if ($currentVersion !== null) { |
||
105 | $visitor->visitValueObject( |
||
106 | new VersionValue( |
||
107 | $currentVersion, |
||
108 | $contentType, |
||
109 | $restContent->relations |
||
110 | ) |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | $generator->endObjectElement('CurrentVersion'); |
||
115 | |||
116 | $generator->startObjectElement('Section'); |
||
117 | $generator->startAttribute( |
||
118 | 'href', |
||
119 | $this->router->generate('ezpublish_rest_loadSection', ['sectionId' => $contentInfo->sectionId]) |
||
120 | ); |
||
121 | $generator->endAttribute('href'); |
||
122 | $generator->endObjectElement('Section'); |
||
123 | |||
124 | // Main location will not exist if we're visiting the content draft |
||
125 | if ($data->mainLocation !== null) { |
||
126 | $generator->startObjectElement('MainLocation', 'Location'); |
||
127 | $generator->startAttribute( |
||
128 | 'href', |
||
129 | $this->router->generate( |
||
130 | 'ezpublish_rest_loadLocation', |
||
131 | ['locationPath' => trim($mainLocation->pathString, '/')] |
||
132 | ) |
||
133 | ); |
||
134 | $generator->endAttribute('href'); |
||
135 | $generator->endObjectElement('MainLocation'); |
||
136 | } |
||
137 | |||
138 | $generator->startObjectElement('Locations', 'LocationList'); |
||
139 | $generator->startAttribute( |
||
140 | 'href', |
||
141 | $this->router->generate( |
||
142 | 'ezpublish_rest_loadLocationsForContent', |
||
143 | ['contentId' => $contentInfo->id] |
||
144 | ) |
||
145 | ); |
||
146 | $generator->endAttribute('href'); |
||
147 | $generator->endObjectElement('Locations'); |
||
148 | |||
149 | $generator->startObjectElement('Owner', 'User'); |
||
150 | $generator->startAttribute( |
||
151 | 'href', |
||
152 | $this->router->generate('ezpublish_rest_loadUser', ['userId' => $contentInfo->ownerId]) |
||
153 | ); |
||
154 | $generator->endAttribute('href'); |
||
155 | $generator->endObjectElement('Owner'); |
||
156 | |||
157 | // Modification date will not exist if we're visiting the content draft |
||
158 | if ($contentInfo->modificationDate !== null) { |
||
159 | $generator->startValueElement( |
||
160 | 'lastModificationDate', |
||
161 | $contentInfo->modificationDate->format('c') |
||
162 | ); |
||
163 | $generator->endValueElement('lastModificationDate'); |
||
164 | } |
||
165 | |||
166 | // Published date will not exist if we're visiting the content draft |
||
167 | if ($contentInfo->publishedDate !== null) { |
||
168 | $generator->startValueElement( |
||
169 | 'publishedDate', |
||
170 | ($contentInfo->publishedDate !== null |
||
171 | ? $contentInfo->publishedDate->format('c') |
||
172 | : null) |
||
173 | ); |
||
174 | $generator->endValueElement('publishedDate'); |
||
175 | } |
||
176 | |||
177 | $generator->startValueElement( |
||
178 | 'mainLanguageCode', |
||
179 | $contentInfo->mainLanguageCode |
||
180 | ); |
||
181 | $generator->endValueElement('mainLanguageCode'); |
||
182 | |||
183 | $generator->startValueElement( |
||
184 | 'currentVersionNo', |
||
185 | $contentInfo->currentVersionNo |
||
186 | ); |
||
187 | $generator->endValueElement('currentVersionNo'); |
||
188 | |||
189 | $generator->startValueElement( |
||
190 | 'alwaysAvailable', |
||
191 | $this->serializeBool($generator, $contentInfo->alwaysAvailable) |
||
192 | ); |
||
193 | $generator->endValueElement('alwaysAvailable'); |
||
194 | |||
195 | $generator->startValueElement( |
||
196 | 'isHidden', |
||
197 | $this->serializeBool($generator, $contentInfo->isHidden) |
||
198 | ); |
||
199 | $generator->endValueElement('isHidden'); |
||
200 | |||
201 | $generator->startValueElement( |
||
202 | 'status', |
||
203 | $this->getStatusString($contentInfo->status) |
||
204 | ); |
||
205 | $generator->endValueElement('status'); |
||
206 | |||
207 | $generator->startObjectElement('ObjectStates', 'ContentObjectStates'); |
||
208 | $generator->startAttribute( |
||
209 | 'href', |
||
210 | $this->router->generate( |
||
211 | 'ezpublish_rest_getObjectStatesForContent', |
||
212 | ['contentId' => $contentInfo->id] |
||
213 | ) |
||
214 | ); |
||
215 | $generator->endAttribute('href'); |
||
216 | $generator->endObjectElement('ObjectStates'); |
||
217 | |||
218 | $generator->endObjectElement('Content'); |
||
219 | } |
||
220 | |||
246 |