Total Complexity | 53 |
Total Lines | 361 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like SystemTagPlugin 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 SystemTagPlugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class SystemTagPlugin extends \Sabre\DAV\ServerPlugin { |
||
54 | |||
55 | // namespace |
||
56 | public const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
||
57 | public const ID_PROPERTYNAME = '{http://owncloud.org/ns}id'; |
||
58 | public const DISPLAYNAME_PROPERTYNAME = '{http://owncloud.org/ns}display-name'; |
||
59 | public const USERVISIBLE_PROPERTYNAME = '{http://owncloud.org/ns}user-visible'; |
||
60 | public const USERASSIGNABLE_PROPERTYNAME = '{http://owncloud.org/ns}user-assignable'; |
||
61 | public const GROUPS_PROPERTYNAME = '{http://owncloud.org/ns}groups'; |
||
62 | public const CANASSIGN_PROPERTYNAME = '{http://owncloud.org/ns}can-assign'; |
||
63 | public const SYSTEM_TAGS_PROPERTYNAME = '{http://nextcloud.org/ns}system-tags'; |
||
64 | |||
65 | /** |
||
66 | * @var \Sabre\DAV\Server $server |
||
67 | */ |
||
68 | private $server; |
||
69 | |||
70 | /** |
||
71 | * @var ISystemTagManager |
||
72 | */ |
||
73 | protected $tagManager; |
||
74 | |||
75 | /** |
||
76 | * @var IUserSession |
||
77 | */ |
||
78 | protected $userSession; |
||
79 | |||
80 | /** |
||
81 | * @var IGroupManager |
||
82 | */ |
||
83 | protected $groupManager; |
||
84 | |||
85 | /** @var array<int, string[]> */ |
||
86 | private array $cachedTagMappings = []; |
||
87 | /** @var array<string, ISystemTag> */ |
||
88 | private array $cachedTags = []; |
||
89 | |||
90 | private ISystemTagObjectMapper $tagMapper; |
||
91 | |||
92 | public function __construct( |
||
93 | ISystemTagManager $tagManager, |
||
94 | IGroupManager $groupManager, |
||
95 | IUserSession $userSession, |
||
96 | ISystemTagObjectMapper $tagMapper, |
||
97 | ) { |
||
98 | $this->tagManager = $tagManager; |
||
99 | $this->userSession = $userSession; |
||
100 | $this->groupManager = $groupManager; |
||
101 | $this->tagMapper = $tagMapper; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * This initializes the plugin. |
||
106 | * |
||
107 | * This function is called by \Sabre\DAV\Server, after |
||
108 | * addPlugin is called. |
||
109 | * |
||
110 | * This method should set up the required event subscriptions. |
||
111 | * |
||
112 | * @param \Sabre\DAV\Server $server |
||
113 | * @return void |
||
114 | */ |
||
115 | public function initialize(\Sabre\DAV\Server $server) { |
||
116 | $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
||
117 | |||
118 | $server->protectedProperties[] = self::ID_PROPERTYNAME; |
||
119 | |||
120 | $server->on('propFind', [$this, 'handleGetProperties']); |
||
121 | $server->on('propPatch', [$this, 'handleUpdateProperties']); |
||
122 | $server->on('method:POST', [$this, 'httpPost']); |
||
123 | |||
124 | $this->server = $server; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * POST operation on system tag collections |
||
129 | * |
||
130 | * @param RequestInterface $request request object |
||
131 | * @param ResponseInterface $response response object |
||
132 | * @return null|false |
||
133 | */ |
||
134 | public function httpPost(RequestInterface $request, ResponseInterface $response) { |
||
135 | $path = $request->getPath(); |
||
136 | |||
137 | // Making sure the node exists |
||
138 | $node = $this->server->tree->getNodeForPath($path); |
||
139 | if ($node instanceof SystemTagsByIdCollection || $node instanceof SystemTagsObjectMappingCollection) { |
||
140 | $data = $request->getBodyAsString(); |
||
141 | |||
142 | $tag = $this->createTag($data, $request->getHeader('Content-Type')); |
||
143 | |||
144 | if ($node instanceof SystemTagsObjectMappingCollection) { |
||
145 | // also add to collection |
||
146 | $node->createFile($tag->getId()); |
||
147 | $url = $request->getBaseUrl() . 'systemtags/'; |
||
148 | } else { |
||
149 | $url = $request->getUrl(); |
||
150 | } |
||
151 | |||
152 | if ($url[strlen($url) - 1] !== '/') { |
||
153 | $url .= '/'; |
||
154 | } |
||
155 | |||
156 | $response->setHeader('Content-Location', $url . $tag->getId()); |
||
157 | |||
158 | // created |
||
159 | $response->setStatus(201); |
||
160 | return false; |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Creates a new tag |
||
166 | * |
||
167 | * @param string $data JSON encoded string containing the properties of the tag to create |
||
168 | * @param string $contentType content type of the data |
||
169 | * @return ISystemTag newly created system tag |
||
170 | * |
||
171 | * @throws BadRequest if a field was missing |
||
172 | * @throws Conflict if a tag with the same properties already exists |
||
173 | * @throws UnsupportedMediaType if the content type is not supported |
||
174 | */ |
||
175 | private function createTag($data, $contentType = 'application/json') { |
||
176 | if (explode(';', $contentType)[0] === 'application/json') { |
||
177 | $data = json_decode($data, true, 512, JSON_THROW_ON_ERROR); |
||
178 | } else { |
||
179 | throw new UnsupportedMediaType(); |
||
180 | } |
||
181 | |||
182 | if (!isset($data['name'])) { |
||
183 | throw new BadRequest('Missing "name" attribute'); |
||
184 | } |
||
185 | |||
186 | $tagName = $data['name']; |
||
187 | $userVisible = true; |
||
188 | $userAssignable = true; |
||
189 | |||
190 | if (isset($data['userVisible'])) { |
||
191 | $userVisible = (bool)$data['userVisible']; |
||
192 | } |
||
193 | |||
194 | if (isset($data['userAssignable'])) { |
||
195 | $userAssignable = (bool)$data['userAssignable']; |
||
196 | } |
||
197 | |||
198 | $groups = []; |
||
199 | if (isset($data['groups'])) { |
||
200 | $groups = $data['groups']; |
||
201 | if (is_string($groups)) { |
||
202 | $groups = explode('|', $groups); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | if ($userVisible === false || $userAssignable === false || !empty($groups)) { |
||
207 | if (!$this->userSession->isLoggedIn() || !$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) { |
||
208 | throw new BadRequest('Not sufficient permissions'); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | try { |
||
213 | $tag = $this->tagManager->createTag($tagName, $userVisible, $userAssignable); |
||
214 | if (!empty($groups)) { |
||
215 | $this->tagManager->setTagGroups($tag, $groups); |
||
216 | } |
||
217 | return $tag; |
||
218 | } catch (TagAlreadyExistsException $e) { |
||
219 | throw new Conflict('Tag already exists', 0, $e); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | |||
224 | /** |
||
225 | * Retrieves system tag properties |
||
226 | * |
||
227 | * @param PropFind $propFind |
||
228 | * @param \Sabre\DAV\INode $node |
||
229 | * |
||
230 | * @return void |
||
231 | */ |
||
232 | public function handleGetProperties( |
||
233 | PropFind $propFind, |
||
234 | \Sabre\DAV\INode $node |
||
235 | ) { |
||
236 | if ($node instanceof Node) { |
||
237 | $this->propfindForFile($propFind, $node); |
||
238 | return; |
||
239 | } |
||
240 | |||
241 | if (!($node instanceof SystemTagNode) && !($node instanceof SystemTagMappingNode)) { |
||
242 | return; |
||
243 | } |
||
244 | |||
245 | $propFind->handle(self::ID_PROPERTYNAME, function () use ($node) { |
||
246 | return $node->getSystemTag()->getId(); |
||
247 | }); |
||
248 | |||
249 | $propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function () use ($node) { |
||
250 | return $node->getSystemTag()->getName(); |
||
251 | }); |
||
252 | |||
253 | $propFind->handle(self::USERVISIBLE_PROPERTYNAME, function () use ($node) { |
||
254 | return $node->getSystemTag()->isUserVisible() ? 'true' : 'false'; |
||
255 | }); |
||
256 | |||
257 | $propFind->handle(self::USERASSIGNABLE_PROPERTYNAME, function () use ($node) { |
||
258 | // this is the tag's inherent property "is user assignable" |
||
259 | return $node->getSystemTag()->isUserAssignable() ? 'true' : 'false'; |
||
260 | }); |
||
261 | |||
262 | $propFind->handle(self::CANASSIGN_PROPERTYNAME, function () use ($node) { |
||
263 | // this is the effective permission for the current user |
||
264 | return $this->tagManager->canUserAssignTag($node->getSystemTag(), $this->userSession->getUser()) ? 'true' : 'false'; |
||
265 | }); |
||
266 | |||
267 | $propFind->handle(self::GROUPS_PROPERTYNAME, function () use ($node) { |
||
268 | if (!$this->groupManager->isAdmin($this->userSession->getUser()->getUID())) { |
||
269 | // property only available for admins |
||
270 | throw new Forbidden(); |
||
271 | } |
||
272 | $groups = []; |
||
273 | // no need to retrieve groups for namespaces that don't qualify |
||
274 | if ($node->getSystemTag()->isUserVisible() && !$node->getSystemTag()->isUserAssignable()) { |
||
275 | $groups = $this->tagManager->getTagGroups($node->getSystemTag()); |
||
276 | } |
||
277 | return implode('|', $groups); |
||
278 | }); |
||
279 | } |
||
280 | |||
281 | private function propfindForFile(PropFind $propFind, Node $node): void { |
||
282 | if ($node instanceof Directory |
||
283 | && $propFind->getDepth() !== 0 |
||
284 | && !is_null($propFind->getStatus(self::SYSTEM_TAGS_PROPERTYNAME))) { |
||
285 | $fileIds = [$node->getId()]; |
||
286 | |||
287 | // note: pre-fetching only supported for depth <= 1 |
||
288 | $folderContent = $node->getNode()->getDirectoryListing(); |
||
289 | foreach ($folderContent as $info) { |
||
290 | $fileIds[] = $info->getId(); |
||
291 | } |
||
292 | |||
293 | $tags = $this->tagMapper->getTagIdsForObjects($fileIds, 'files'); |
||
294 | |||
295 | $this->cachedTagMappings = $this->cachedTagMappings + $tags; |
||
296 | $emptyFileIds = array_diff($fileIds, array_keys($tags)); |
||
297 | |||
298 | // also cache the ones that were not found |
||
299 | foreach ($emptyFileIds as $fileId) { |
||
300 | $this->cachedTagMappings[$fileId] = []; |
||
301 | } |
||
302 | } |
||
303 | |||
304 | $propFind->handle(self::SYSTEM_TAGS_PROPERTYNAME, function () use ($node) { |
||
305 | $user = $this->userSession->getUser(); |
||
306 | if ($user === null) { |
||
307 | return; |
||
308 | } |
||
309 | |||
310 | $tags = $this->getTagsForFile($node->getId(), $user); |
||
311 | return new SystemTagList($tags, $this->tagManager, $user); |
||
312 | }); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @param int $fileId |
||
317 | * @return ISystemTag[] |
||
318 | */ |
||
319 | private function getTagsForFile(int $fileId, IUser $user): array { |
||
320 | |||
321 | if (isset($this->cachedTagMappings[$fileId])) { |
||
322 | $tagIds = $this->cachedTagMappings[$fileId]; |
||
323 | } else { |
||
324 | $tags = $this->tagMapper->getTagIdsForObjects([$fileId], 'files'); |
||
325 | $fileTags = current($tags); |
||
326 | if ($fileTags) { |
||
327 | $tagIds = $fileTags; |
||
328 | } else { |
||
329 | $tagIds = []; |
||
330 | } |
||
331 | } |
||
332 | |||
333 | $tags = array_filter(array_map(function(string $tagId) { |
||
334 | return $this->cachedTags[$tagId] ?? null; |
||
335 | }, $tagIds)); |
||
336 | |||
337 | $uncachedTagIds = array_filter($tagIds, function(string $tagId): bool { |
||
338 | return !isset($this->cachedTags[$tagId]); |
||
339 | }); |
||
340 | |||
341 | if (count($uncachedTagIds)) { |
||
342 | $retrievedTags = $this->tagManager->getTagsByIds($uncachedTagIds); |
||
343 | foreach ($retrievedTags as $tag) { |
||
344 | $this->cachedTags[$tag->getId()] = $tag; |
||
345 | } |
||
346 | $tags += $retrievedTags; |
||
347 | } |
||
348 | |||
349 | return array_filter($tags, function(ISystemTag $tag) use ($user) { |
||
350 | return $this->tagManager->canUserSeeTag($tag, $user); |
||
351 | }); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Updates tag attributes |
||
356 | * |
||
357 | * @param string $path |
||
358 | * @param PropPatch $propPatch |
||
359 | * |
||
360 | * @return void |
||
361 | */ |
||
362 | public function handleUpdateProperties($path, PropPatch $propPatch) { |
||
414 | }); |
||
415 | } |
||
416 | } |
||
417 |