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 |
||
39 | class FilesHooks { |
||
40 | |||
41 | /** @var \OCP\Activity\IManager */ |
||
42 | protected $manager; |
||
43 | |||
44 | /** @var \OCA\Activity\Data */ |
||
45 | protected $activityData; |
||
46 | |||
47 | /** @var \OCA\Activity\UserSettings */ |
||
48 | protected $userSettings; |
||
49 | |||
50 | /** @var \OCP\IGroupManager */ |
||
51 | protected $groupManager; |
||
52 | |||
53 | /** @var \OCP\IDBConnection */ |
||
54 | protected $connection; |
||
55 | |||
56 | /** @var \OC\Files\View */ |
||
57 | protected $view; |
||
58 | |||
59 | /** @var string|false */ |
||
60 | protected $currentUser; |
||
61 | |||
62 | /** |
||
63 | * Constructor |
||
64 | * |
||
65 | * @param IManager $manager |
||
66 | * @param Data $activityData |
||
67 | * @param UserSettings $userSettings |
||
68 | * @param IGroupManager $groupManager |
||
69 | * @param View $view |
||
70 | * @param IDBConnection $connection |
||
71 | * @param string|false $currentUser |
||
72 | */ |
||
73 | 46 | public function __construct(IManager $manager, Data $activityData, UserSettings $userSettings, IGroupManager $groupManager, View $view, IDBConnection $connection, $currentUser) { |
|
74 | 46 | $this->manager = $manager; |
|
75 | 46 | $this->activityData = $activityData; |
|
76 | 46 | $this->userSettings = $userSettings; |
|
77 | 46 | $this->groupManager = $groupManager; |
|
78 | 46 | $this->view = $view; |
|
79 | 46 | $this->connection = $connection; |
|
80 | 46 | $this->currentUser = $currentUser; |
|
81 | 46 | } |
|
82 | |||
83 | /** |
||
84 | * @return string|false Current UserID if logged in, false otherwise |
||
85 | */ |
||
86 | 2 | protected function getCurrentUser() { |
|
89 | |||
90 | /** |
||
91 | * Store the create hook events |
||
92 | * @param string $path Path of the file that has been created |
||
93 | */ |
||
94 | 2 | public function fileCreate($path) { |
|
95 | 2 | if ($this->getCurrentUser() !== false) { |
|
96 | 1 | $this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, 'created_self', 'created_by'); |
|
97 | 1 | } else { |
|
98 | 1 | $this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, '', 'created_public'); |
|
99 | } |
||
100 | 2 | } |
|
101 | |||
102 | /** |
||
103 | * Store the update hook events |
||
104 | * @param string $path Path of the file that has been modified |
||
105 | */ |
||
106 | 1 | public function fileUpdate($path) { |
|
109 | |||
110 | /** |
||
111 | * Store the delete hook events |
||
112 | * @param string $path Path of the file that has been deleted |
||
113 | */ |
||
114 | 1 | public function fileDelete($path) { |
|
117 | |||
118 | /** |
||
119 | * Store the restore hook events |
||
120 | * @param string $path Path of the file that has been restored |
||
121 | */ |
||
122 | 1 | public function fileRestore($path) { |
|
125 | |||
126 | /** |
||
127 | * Creates the entries for file actions on $file_path |
||
128 | * |
||
129 | * @param string $filePath The file that is being changed |
||
130 | * @param int $activityType The activity type |
||
131 | * @param string $subject The subject for the actor |
||
132 | * @param string $subjectBy The subject for other users (with "by $actor") |
||
133 | */ |
||
134 | 3 | protected function addNotificationsForFileAction($filePath, $activityType, $subject, $subjectBy) { |
|
135 | // Do not add activities for .part-files |
||
136 | 3 | if (substr($filePath, -5) === '.part') { |
|
137 | 1 | return; |
|
138 | } |
||
139 | |||
140 | 2 | list($filePath, $uidOwner, $fileId) = $this->getSourcePathAndOwner($filePath); |
|
141 | 2 | $affectedUsers = $this->getUserPathsFromPath($filePath, $uidOwner); |
|
142 | 2 | $filteredStreamUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'stream', $activityType); |
|
143 | 2 | $filteredEmailUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'email', $activityType); |
|
144 | |||
145 | 2 | foreach ($affectedUsers as $user => $path) { |
|
146 | 2 | if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) { |
|
147 | 2 | continue; |
|
148 | } |
||
149 | |||
150 | 2 | if ($user === $this->currentUser) { |
|
151 | 1 | $userSubject = $subject; |
|
152 | 1 | $userParams = array($path); |
|
153 | 1 | } else { |
|
154 | 1 | $userSubject = $subjectBy; |
|
155 | 1 | $userParams = array($path, $this->currentUser); |
|
156 | } |
||
157 | |||
158 | 2 | $this->addNotificationsForUser( |
|
159 | 2 | $user, $userSubject, $userParams, |
|
160 | 2 | $fileId, $path, true, |
|
161 | 2 | !empty($filteredStreamUsers[$user]), |
|
162 | 2 | !empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0, |
|
163 | $activityType |
||
164 | 2 | ); |
|
165 | 2 | } |
|
166 | 2 | } |
|
167 | |||
168 | /** |
||
169 | * Returns a "username => path" map for all affected users |
||
170 | * |
||
171 | * @param string $path |
||
172 | * @param string $uidOwner |
||
173 | * @return array |
||
174 | */ |
||
175 | protected function getUserPathsFromPath($path, $uidOwner) { |
||
178 | |||
179 | /** |
||
180 | * Return the source |
||
181 | * |
||
182 | * @param string $path |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function getSourcePathAndOwner($path) { |
||
201 | |||
202 | /** |
||
203 | * Manage sharing events |
||
204 | * @param array $params The hook params |
||
205 | */ |
||
206 | 3 | public function share($params) { |
|
219 | |||
220 | /** |
||
221 | * Sharing a file or folder with a user |
||
222 | * |
||
223 | * @param string $shareWith |
||
224 | * @param int $fileSource File ID that is being shared |
||
225 | * @param string $itemType File type that is being shared (file or folder) |
||
226 | * @param string $fileTarget File path |
||
227 | */ |
||
228 | 2 | protected function shareFileOrFolderWithUser($shareWith, $fileSource, $itemType, $fileTarget) { |
|
241 | |||
242 | /** |
||
243 | * Sharing a file or folder with a group |
||
244 | * |
||
245 | * @param string $shareWith |
||
246 | * @param int $fileSource File ID that is being shared |
||
247 | * @param string $itemType File type that is being shared (file or folder) |
||
248 | * @param string $fileTarget File path |
||
249 | * @param int $shareId The Share ID of this share |
||
250 | */ |
||
251 | 5 | protected function shareFileOrFolderWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId) { |
|
293 | |||
294 | /** |
||
295 | * Check when there was a naming conflict and the target is different |
||
296 | * for some of the users |
||
297 | * |
||
298 | * @param array $affectedUsers |
||
299 | * @param int $shareId |
||
300 | * @return mixed |
||
301 | */ |
||
302 | protected function fixPathsForShareExceptions(array $affectedUsers, $shareId) { |
||
316 | |||
317 | /** |
||
318 | * Sharing a file or folder via link/public |
||
319 | * |
||
320 | * @param int $fileSource File ID that is being shared |
||
321 | * @param string $itemType File type that is being shared (file or folder) |
||
322 | */ |
||
323 | 2 | protected function shareFileOrFolder($fileSource, $itemType) { |
|
340 | |||
341 | /** |
||
342 | * Add notifications for the user that shares a file/folder |
||
343 | * |
||
344 | * @param string $subject |
||
345 | * @param string $shareWith |
||
346 | * @param int $fileSource |
||
347 | * @param string $itemType |
||
348 | */ |
||
349 | 2 | protected function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) { |
|
364 | |||
365 | /** |
||
366 | * Add notifications for the user that shares a file/folder |
||
367 | * |
||
368 | * @param string $owner |
||
369 | * @param string $subject |
||
370 | * @param string $shareWith |
||
371 | * @param int $fileSource |
||
372 | * @param string $itemType |
||
373 | */ |
||
374 | 2 | protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType) { |
|
389 | |||
390 | /** |
||
391 | * Add notifications for the owners whose files have been reshared |
||
392 | * |
||
393 | * @param string $currentOwner |
||
394 | * @param string $subject |
||
395 | * @param string $shareWith |
||
396 | * @param int $fileSource |
||
397 | * @param string $itemType |
||
398 | */ |
||
399 | 10 | protected function shareNotificationForOriginalOwners($currentOwner, $subject, $shareWith, $fileSource, $itemType) { |
|
437 | |||
438 | /** |
||
439 | * Adds the activity and email for a user when the settings require it |
||
440 | * |
||
441 | * @param string $user |
||
442 | * @param string $subject |
||
443 | * @param array $subjectParams |
||
444 | * @param int $fileId |
||
445 | * @param string $path |
||
446 | * @param bool $isFile If the item is a file, we link to the parent directory |
||
447 | * @param bool $streamSetting |
||
448 | * @param int $emailSetting |
||
449 | * @param string $type |
||
450 | */ |
||
451 | 9 | protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) { |
|
485 | } |
||
486 |