Complex classes like FilesHooks 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FilesHooks, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class FilesHooks { |
||
43 | const USER_BATCH_SIZE = 50; |
||
44 | |||
45 | /** @var \OCP\Activity\IManager */ |
||
46 | protected $manager; |
||
47 | |||
48 | /** @var \OCA\Activity\Data */ |
||
49 | protected $activityData; |
||
50 | |||
51 | /** @var \OCA\Activity\UserSettings */ |
||
52 | protected $userSettings; |
||
53 | |||
54 | /** @var \OCP\IGroupManager */ |
||
55 | protected $groupManager; |
||
56 | |||
57 | /** @var \OCP\IDBConnection */ |
||
58 | protected $connection; |
||
59 | |||
60 | /** @var \OC\Files\View */ |
||
61 | protected $view; |
||
62 | |||
63 | /** @var string|false */ |
||
64 | protected $currentUser; |
||
65 | |||
66 | /** |
||
67 | * Constructor |
||
68 | * |
||
69 | * @param IManager $manager |
||
70 | * @param Data $activityData |
||
71 | * @param UserSettings $userSettings |
||
72 | * @param IGroupManager $groupManager |
||
73 | * @param View $view |
||
74 | * @param IDBConnection $connection |
||
75 | * @param string|false $currentUser |
||
76 | */ |
||
77 | 47 | public function __construct(IManager $manager, Data $activityData, UserSettings $userSettings, IGroupManager $groupManager, View $view, IDBConnection $connection, $currentUser) { |
|
78 | 47 | $this->manager = $manager; |
|
79 | 47 | $this->activityData = $activityData; |
|
80 | 47 | $this->userSettings = $userSettings; |
|
81 | 47 | $this->groupManager = $groupManager; |
|
82 | 47 | $this->view = $view; |
|
83 | 47 | $this->connection = $connection; |
|
84 | 47 | $this->currentUser = $currentUser; |
|
85 | 47 | } |
|
86 | |||
87 | /** |
||
88 | * @return string|false Current UserID if logged in, false otherwise |
||
89 | */ |
||
90 | 2 | protected function getCurrentUser() { |
|
93 | |||
94 | /** |
||
95 | * Store the create hook events |
||
96 | * @param string $path Path of the file that has been created |
||
97 | */ |
||
98 | 2 | public function fileCreate($path) { |
|
99 | 2 | if ($this->getCurrentUser() !== false) { |
|
100 | 1 | $this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, 'created_self', 'created_by'); |
|
101 | 1 | } else { |
|
102 | 1 | $this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, '', 'created_public'); |
|
103 | } |
||
104 | 2 | } |
|
105 | |||
106 | /** |
||
107 | * Store the update hook events |
||
108 | * @param string $path Path of the file that has been modified |
||
109 | */ |
||
110 | 1 | public function fileUpdate($path) { |
|
113 | |||
114 | /** |
||
115 | * Store the delete hook events |
||
116 | * @param string $path Path of the file that has been deleted |
||
117 | */ |
||
118 | 1 | public function fileDelete($path) { |
|
121 | |||
122 | /** |
||
123 | * Store the restore hook events |
||
124 | * @param string $path Path of the file that has been restored |
||
125 | */ |
||
126 | 1 | public function fileRestore($path) { |
|
129 | |||
130 | /** |
||
131 | * Creates the entries for file actions on $file_path |
||
132 | * |
||
133 | * @param string $filePath The file that is being changed |
||
134 | * @param int $activityType The activity type |
||
135 | * @param string $subject The subject for the actor |
||
136 | * @param string $subjectBy The subject for other users (with "by $actor") |
||
137 | */ |
||
138 | 3 | protected function addNotificationsForFileAction($filePath, $activityType, $subject, $subjectBy) { |
|
171 | |||
172 | /** |
||
173 | * Returns a "username => path" map for all affected users |
||
174 | * |
||
175 | * @param string $path |
||
176 | * @param string $uidOwner |
||
177 | * @return array |
||
178 | */ |
||
179 | protected function getUserPathsFromPath($path, $uidOwner) { |
||
182 | |||
183 | /** |
||
184 | * Return the source |
||
185 | * |
||
186 | * @param string $path |
||
187 | * @return array |
||
188 | */ |
||
189 | protected function getSourcePathAndOwner($path) { |
||
205 | |||
206 | /** |
||
207 | * Manage sharing events |
||
208 | * @param array $params The hook params |
||
209 | */ |
||
210 | 3 | public function share($params) { |
|
223 | |||
224 | /** |
||
225 | * Sharing a file or folder with a user |
||
226 | * |
||
227 | * @param string $shareWith |
||
228 | * @param int $fileSource File ID that is being shared |
||
229 | * @param string $itemType File type that is being shared (file or folder) |
||
230 | * @param string $fileTarget File path |
||
231 | */ |
||
232 | 2 | protected function shareFileOrFolderWithUser($shareWith, $fileSource, $itemType, $fileTarget) { |
|
245 | |||
246 | /** |
||
247 | * Sharing a file or folder with a group |
||
248 | * |
||
249 | * @param string $shareWith |
||
250 | * @param int $fileSource File ID that is being shared |
||
251 | * @param string $itemType File type that is being shared (file or folder) |
||
252 | * @param string $fileTarget File path |
||
253 | * @param int $shareId The Share ID of this share |
||
254 | */ |
||
255 | 6 | protected function shareFileOrFolderWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId) { |
|
274 | |||
275 | /** |
||
276 | * @param IUser[] $usersInGroup |
||
277 | * @param int $fileSource File ID that is being shared |
||
278 | * @param string $itemType File type that is being shared (file or folder) |
||
279 | * @param string $fileTarget File path |
||
280 | * @param int $shareId The Share ID of this share |
||
281 | */ |
||
282 | 4 | protected function addNotificationsForGroupUsers(array $usersInGroup, $fileSource, $itemType, $fileTarget, $shareId) { |
|
314 | |||
315 | /** |
||
316 | * Check when there was a naming conflict and the target is different |
||
317 | * for some of the users |
||
318 | * |
||
319 | * @param array $affectedUsers |
||
320 | * @param int $shareId |
||
321 | * @return mixed |
||
322 | */ |
||
323 | protected function fixPathsForShareExceptions(array $affectedUsers, $shareId) { |
||
337 | |||
338 | /** |
||
339 | * Sharing a file or folder via link/public |
||
340 | * |
||
341 | * @param int $fileSource File ID that is being shared |
||
342 | * @param string $itemType File type that is being shared (file or folder) |
||
343 | */ |
||
344 | 2 | protected function shareFileOrFolder($fileSource, $itemType) { |
|
362 | |||
363 | /** |
||
364 | * Add notifications for the user that shares a file/folder |
||
365 | * |
||
366 | * @param string $subject |
||
367 | * @param string $shareWith |
||
368 | * @param int $fileSource |
||
369 | * @param string $itemType |
||
370 | */ |
||
371 | 2 | protected function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) { |
|
387 | |||
388 | /** |
||
389 | * Add notifications for the user that shares a file/folder |
||
390 | * |
||
391 | * @param string $owner |
||
392 | * @param string $subject |
||
393 | * @param string $shareWith |
||
394 | * @param int $fileSource |
||
395 | * @param string $itemType |
||
396 | */ |
||
397 | 2 | protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType) { |
|
413 | |||
414 | /** |
||
415 | * Add notifications for the owners whose files have been reshared |
||
416 | * |
||
417 | * @param string $currentOwner |
||
418 | * @param string $subject |
||
419 | * @param string $shareWith |
||
420 | * @param int $fileSource |
||
421 | * @param string $itemType |
||
422 | */ |
||
423 | 10 | protected function shareNotificationForOriginalOwners($currentOwner, $subject, $shareWith, $fileSource, $itemType) { |
|
463 | |||
464 | /** |
||
465 | * Adds the activity and email for a user when the settings require it |
||
466 | * |
||
467 | * @param string $user |
||
468 | * @param string $subject |
||
469 | * @param array $subjectParams |
||
470 | * @param int $fileId |
||
471 | * @param string $path |
||
472 | * @param bool $isFile If the item is a file, we link to the parent directory |
||
473 | * @param bool $streamSetting |
||
474 | * @param int $emailSetting |
||
475 | * @param string $type |
||
476 | */ |
||
477 | 9 | protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) { |
|
511 | } |
||
512 |