Total Complexity | 54 |
Total Lines | 350 |
Duplicated Lines | 0 % |
Coverage | 30.76% |
Changes | 9 | ||
Bugs | 0 | Features | 0 |
Complex classes like Website 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 Website, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | class Website extends WebsiteCore |
||
56 | { |
||
57 | /** @var int */ |
||
58 | public const SITE_LENGTH_MIN = 3; |
||
59 | |||
60 | /** @var int */ |
||
61 | public const SITE_LENGTH_MAX = 255; |
||
62 | |||
63 | /** @var string */ |
||
64 | public const SITE_REGEX = '^[a-z0-9][a-z0-9_-]+[a-z0-9]$'; |
||
65 | |||
66 | /** @var int */ |
||
67 | public const NAME_LENGTH_MIN = 3; |
||
68 | |||
69 | /** @var int */ |
||
70 | public const NAME_LENGTH_MAX = 255; |
||
71 | |||
72 | /** @var IConfig */ |
||
73 | private $config; |
||
74 | |||
75 | /** @var IL10N */ |
||
76 | private $l10n; |
||
77 | |||
78 | /** @var IUserManager */ |
||
79 | private $userManager; |
||
80 | |||
81 | /** @var IGroupManager */ |
||
82 | private $groupManager; |
||
83 | |||
84 | /** @var IURLGenerator */ |
||
85 | private $urlGenerator; |
||
86 | |||
87 | /** @var WebsitesService */ |
||
88 | private $websitesService; |
||
89 | |||
90 | /** @var ThemesService */ |
||
91 | private $themesService; |
||
92 | |||
93 | /** @var TemplatesService */ |
||
94 | private $templatesService; |
||
95 | |||
96 | /** @var MiscService */ |
||
97 | private $miscService; |
||
98 | |||
99 | /** @var StorageFolder */ |
||
100 | private $folder; |
||
101 | |||
102 | /** |
||
103 | * Website constructor. |
||
104 | * |
||
105 | * @param array|string|null $data |
||
106 | */ |
||
107 | 3 | public function __construct($data = null) |
|
108 | { |
||
109 | 3 | $this->config = \OC::$server->getConfig(); |
|
|
|||
110 | 3 | $this->l10n = \OC::$server->getL10N(Application::APP_NAME); |
|
111 | 3 | $this->userManager = \OC::$server->getUserManager(); |
|
112 | 3 | $this->groupManager = \OC::$server->getGroupManager(); |
|
113 | 3 | $this->urlGenerator = \OC::$server->getURLGenerator(); |
|
114 | 3 | $this->websitesService = \OC::$server->query(WebsitesService::class); |
|
1 ignored issue
–
show
|
|||
115 | 3 | $this->themesService = \OC::$server->query(ThemesService::class); |
|
1 ignored issue
–
show
|
|||
116 | 3 | $this->templatesService = \OC::$server->query(TemplatesService::class); |
|
1 ignored issue
–
show
|
|||
117 | 3 | $this->miscService = \OC::$server->query(MiscService::class); |
|
1 ignored issue
–
show
|
|||
118 | |||
119 | 3 | parent::__construct($data); |
|
120 | 3 | } |
|
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getTimeZone(): string |
||
126 | { |
||
127 | $serverTimeZone = date_default_timezone_get() ?: 'UTC'; |
||
128 | return $this->config->getUserValue($this->getUserId(), 'core', 'timezone', $serverTimeZone); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param string $path |
||
133 | * @param array $meta |
||
134 | * |
||
135 | * @throws InvalidPathException |
||
136 | * @throws WebsiteInvalidFilesystemException |
||
137 | * @throws WebsiteNotPermittedException |
||
138 | * @throws NotPermittedException |
||
139 | */ |
||
140 | public function assertViewerAccess(string $path, array $meta = []): void |
||
141 | { |
||
142 | $exceptionClass = WebsiteNotPermittedException::class; |
||
143 | if ($this->getType() === self::TYPE_PUBLIC) { |
||
144 | if (empty($meta['access'])) { |
||
145 | return; |
||
146 | } |
||
147 | |||
148 | $groupAccess = $meta['access']; |
||
149 | if (!is_array($groupAccess)) { |
||
150 | $groupAccess = explode(',', $groupAccess); |
||
151 | } |
||
152 | |||
153 | foreach ($groupAccess as $group) { |
||
154 | $group = trim($group); |
||
155 | |||
156 | if ($group === 'public') { |
||
157 | return; |
||
158 | } elseif ($group === 'private') { |
||
159 | continue; |
||
160 | } |
||
161 | |||
162 | if ($this->getViewer() && $this->groupManager->groupExists($group)) { |
||
163 | if ($this->groupManager->isInGroup($this->getViewer(), $group)) { |
||
164 | return; |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | $exceptionClass = NotPermittedException::class; |
||
170 | } |
||
171 | |||
172 | if ($this->getViewer()) { |
||
173 | if ($this->getViewer() === $this->getUserId()) { |
||
174 | return; |
||
175 | } |
||
176 | |||
177 | /** @var OCFolder $viewerOCFolder */ |
||
178 | $viewerOCFolder = \OC::$server->getUserFolder($this->getViewer()); |
||
179 | $viewerAccessClosure = function (OCNode $node) use ($viewerOCFolder) { |
||
180 | $nodeId = $node->getId(); |
||
181 | |||
182 | $viewerNodes = $viewerOCFolder->getById($nodeId); |
||
183 | foreach ($viewerNodes as $viewerNode) { |
||
184 | if ($viewerNode->isReadable()) { |
||
185 | return true; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | return false; |
||
190 | }; |
||
191 | |||
192 | $websiteFolder = $this->getWebsiteFolder(); |
||
193 | |||
194 | $path = $this->miscService->normalizePath($path); |
||
195 | while ($path && ($path !== '.')) { |
||
196 | try { |
||
197 | /** @var StorageFile|StorageFolder $file */ |
||
198 | $file = $websiteFolder->get($path); |
||
199 | } catch (NotFoundException $e) { |
||
200 | $file = null; |
||
201 | } |
||
202 | |||
203 | if ($file) { |
||
204 | if ($viewerAccessClosure($file->getOCNode())) { |
||
205 | return; |
||
206 | } |
||
207 | |||
208 | throw new $exceptionClass(); |
||
209 | } |
||
210 | |||
211 | $path = dirname($path); |
||
212 | } |
||
213 | |||
214 | if ($viewerAccessClosure($websiteFolder->getOCNode())) { |
||
215 | return; |
||
216 | } |
||
217 | } |
||
218 | |||
219 | throw new $exceptionClass(); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @throws WebsiteInvalidOwnerException |
||
224 | */ |
||
225 | 3 | public function assertValidOwner(): void |
|
236 | } |
||
237 | 3 | } |
|
238 | |||
239 | /** |
||
240 | * @throws WebsiteInvalidDataException |
||
241 | */ |
||
242 | 3 | public function assertValidName(): void |
|
249 | } |
||
250 | 3 | } |
|
251 | |||
252 | /** |
||
253 | * @throws WebsiteInvalidDataException |
||
254 | */ |
||
255 | 3 | public function assertValidSite(): void |
|
268 | } |
||
269 | 3 | } |
|
270 | |||
271 | /** |
||
272 | * @throws WebsiteInvalidDataException |
||
273 | */ |
||
274 | 3 | public function assertValidPath(): void |
|
275 | { |
||
276 | try { |
||
277 | 3 | $path = $this->miscService->normalizePath($this->getPath()); |
|
278 | 3 | if ($path === '') { |
|
279 | 3 | throw new InvalidPathException(); |
|
280 | } |
||
281 | } catch (InvalidPathException $e) { |
||
282 | throw new WebsiteInvalidDataException( |
||
283 | 'path', |
||
284 | $this->l10n->t('The path of the website is invalid.') |
||
285 | ); |
||
286 | } |
||
287 | |||
288 | 3 | $userFolder = new StorageFolder(\OC::$server->getUserFolder($this->getUserId())); |
|
289 | |||
290 | try { |
||
291 | 3 | $websiteBaseFolder = $userFolder->getFolder(dirname($path)); |
|
292 | |||
293 | try { |
||
294 | 3 | $websiteFolder = $websiteBaseFolder->getFolder(basename($path)); |
|
295 | |||
296 | if (!$websiteFolder->isLocal()) { |
||
297 | throw new WebsiteInvalidDataException( |
||
298 | 'path', |
||
299 | $this->l10n->t('The website\'s path is stored on a non-local storage.') |
||
300 | ); |
||
301 | } |
||
302 | 3 | } catch (NotFoundException $e) { |
|
303 | 3 | if (!$websiteBaseFolder->isLocal()) { |
|
304 | throw new WebsiteInvalidDataException( |
||
305 | 'path', |
||
306 | 3 | $this->l10n->t('The website\'s path is stored on a non-local storage.') |
|
307 | ); |
||
308 | } |
||
309 | } |
||
310 | } catch (InvalidPathException | NotFoundException $e) { |
||
311 | throw new WebsiteInvalidDataException( |
||
312 | 'path', |
||
313 | $this->l10n->t('Parent folder of the website\'s path not found.') |
||
314 | ); |
||
315 | } |
||
316 | 3 | } |
|
317 | |||
318 | /** |
||
319 | * @throws ThemeNotFoundException |
||
320 | * @throws ThemeNotCompatibleException |
||
321 | */ |
||
322 | 3 | public function assertValidTheme(): void |
|
323 | { |
||
324 | 3 | $this->themesService->assertValidTheme($this->getTheme()); |
|
325 | 3 | } |
|
326 | |||
327 | /** |
||
328 | * @throws TemplateNotFoundException |
||
329 | * @throws TemplateNotCompatibleException |
||
330 | */ |
||
331 | 3 | public function assertValidTemplate(): void |
|
332 | { |
||
333 | 3 | $this->templatesService->assertValidTemplate($this->getTemplateSource()); |
|
334 | 3 | } |
|
335 | |||
336 | /** |
||
337 | * @param string $userId |
||
338 | * |
||
339 | * @throws WebsiteForeignOwnerException |
||
340 | */ |
||
341 | public function assertOwnedBy(string $userId): void |
||
342 | { |
||
343 | if ($this->getUserId() !== $userId) { |
||
344 | throw new WebsiteForeignOwnerException(); |
||
345 | } |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * @return StorageFolder |
||
350 | * @throws WebsiteInvalidFilesystemException |
||
351 | */ |
||
352 | public function getWebsiteFolder(): StorageFolder |
||
353 | { |
||
354 | if ($this->folder !== null) { |
||
355 | try { |
||
356 | // NC doesn't guarantee that mounts are present for the whole request lifetime |
||
357 | // for example, if you call \OC\Files\Utils\Scanner::scan(), all mounts are reset |
||
358 | // this makes OCNode instances, which rely on mounts of different users than the current, unusable |
||
359 | // by calling OCFolder::get('') we can detect this situation and re-init the required mounts |
||
360 | $this->folder->get(''); |
||
361 | } catch (\Exception $e) { |
||
362 | $this->folder = null; |
||
363 | } |
||
364 | } |
||
365 | |||
366 | if ($this->folder === null) { |
||
367 | try { |
||
368 | $ocUserFolder = \OC::$server->getUserFolder($this->getUserId()); |
||
369 | $userFolder = new StorageFolder($ocUserFolder); |
||
370 | |||
371 | $websiteFolder = $userFolder->getFolder($this->getPath()); |
||
372 | $this->folder = $websiteFolder->fakeRoot(); |
||
373 | } catch (InvalidPathException | NotFoundException $e) { |
||
374 | throw new WebsiteInvalidFilesystemException($e); |
||
375 | } |
||
376 | } |
||
377 | |||
378 | return $this->folder; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @return string |
||
383 | * @throws WebsiteInvalidFilesystemException |
||
384 | */ |
||
385 | public function getWebsitePath(): string |
||
391 | } |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * @return string |
||
396 | */ |
||
397 | public function getWebsiteUrl(): string |
||
405 | } |
||
406 | } |
||
408 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.