Complex classes like Data 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 Data, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Data { |
||
| 40 | /** @var IManager */ |
||
| 41 | protected $activityManager; |
||
| 42 | |||
| 43 | /** @var IDBConnection */ |
||
| 44 | protected $connection; |
||
| 45 | |||
| 46 | /** @var IUserSession */ |
||
| 47 | protected $userSession; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param IManager $activityManager |
||
| 51 | * @param IDBConnection $connection |
||
| 52 | * @param IUserSession $userSession |
||
| 53 | */ |
||
| 54 | 67 | public function __construct(IManager $activityManager, IDBConnection $connection, IUserSession $userSession) { |
|
| 59 | |||
| 60 | protected $notificationTypes = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param IL10N $l |
||
| 64 | * @return array Array "stringID of the type" => "translated string description for the setting" |
||
| 65 | * or Array "stringID of the type" => [ |
||
| 66 | * 'desc' => "translated string description for the setting" |
||
| 67 | * 'methods' => [\OCP\Activity\IExtension::METHOD_*], |
||
| 68 | * ] |
||
| 69 | */ |
||
| 70 | 8 | public function getNotificationTypes(IL10N $l) { |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Send an event into the activity stream |
||
| 83 | * |
||
| 84 | * @param IEvent $event |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | 4 | public function send(IEvent $event) { |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Send an event as email |
||
| 134 | * |
||
| 135 | * @param IEvent $event |
||
| 136 | * @param int $latestSendTime Activity $timestamp + batch setting of $affectedUser |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | 4 | public function storeMail(IEvent $event, $latestSendTime) { |
|
| 140 | 4 | if ($event->getAffectedUser() === '' || $event->getAffectedUser() === null) { |
|
| 141 | 2 | return false; |
|
| 142 | } |
||
| 143 | |||
| 144 | // store in DB |
||
| 145 | 2 | $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 146 | 2 | $queryBuilder->insert('activity_mq') |
|
| 147 | 2 | ->values([ |
|
| 148 | 2 | 'amq_appid' => $queryBuilder->createParameter('app'), |
|
| 149 | 2 | 'amq_subject' => $queryBuilder->createParameter('subject'), |
|
| 150 | 2 | 'amq_subjectparams' => $queryBuilder->createParameter('subjectparams'), |
|
| 151 | 2 | 'amq_affecteduser' => $queryBuilder->createParameter('affecteduser'), |
|
| 152 | 2 | 'amq_timestamp' => $queryBuilder->createParameter('timestamp'), |
|
| 153 | 2 | 'amq_type' => $queryBuilder->createParameter('type'), |
|
| 154 | 2 | 'amq_latest_send' => $queryBuilder->createParameter('latest_send'), |
|
| 155 | ]) |
||
| 156 | 2 | ->setParameters([ |
|
| 157 | 2 | 'app' => $event->getApp(), |
|
| 158 | 2 | 'subject' => $event->getSubject(), |
|
| 159 | 2 | 'subjectparams' => json_encode($event->getSubjectParameters()), |
|
| 160 | 2 | 'affecteduser' => $event->getAffectedUser(), |
|
| 161 | 2 | 'timestamp' => (int) $event->getTimestamp(), |
|
| 162 | 2 | 'type' => $event->getType(), |
|
| 163 | 2 | 'latest_send' => $latestSendTime, |
|
| 164 | ]) |
||
| 165 | 2 | ->execute(); |
|
| 166 | |||
| 167 | 2 | return true; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Read a list of events from the activity stream |
||
| 172 | * |
||
| 173 | * @param GroupHelper $groupHelper Allows activities to be grouped |
||
| 174 | * @param UserSettings $userSettings Gets the settings of the user |
||
| 175 | * @param string $user User for whom we display the stream |
||
| 176 | * |
||
| 177 | * @param int $since The integer ID of the last activity that has been seen. |
||
| 178 | * @param int $limit How many activities should be returned |
||
| 179 | * @param string $sort Should activities be given ascending or descending |
||
| 180 | * |
||
| 181 | * @param string $filter Filter the activities |
||
| 182 | * @param string $objectType Allows to filter the activities to a given object. May only appear together with $objectId |
||
| 183 | * @param int $objectId Allows to filter the activities to a given object. May only appear together with $objectType |
||
| 184 | * |
||
| 185 | * @return array |
||
| 186 | * |
||
| 187 | * @throws \OutOfBoundsException if the user (Code: 1) or the since (Code: 2) is invalid |
||
| 188 | * @throws \BadMethodCallException if the user has selected to display no types for this filter (Code: 3) |
||
| 189 | */ |
||
| 190 | 15 | public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user, $since, $limit, $sort, $filter, $objectType = '', $objectId = 0) { |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param IQueryBuilder $query |
||
| 273 | * @param string $user |
||
| 274 | * @param int $since |
||
| 275 | * @param string $sort |
||
| 276 | * |
||
| 277 | * @return array Headers that should be set on the response |
||
| 278 | * |
||
| 279 | * @throws \OutOfBoundsException If $since is not owned by $user |
||
| 280 | */ |
||
| 281 | 18 | protected function setOffsetFromSince(IQueryBuilder $query, $user, $since, $sort) { |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Verify that the filter is valid |
||
| 332 | * |
||
| 333 | * @param string $filterValue |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | 6 | public function validateFilter($filterValue) { |
|
| 337 | 6 | if (!isset($filterValue)) { |
|
| 338 | 1 | return 'all'; |
|
| 339 | } |
||
| 340 | |||
| 341 | switch ($filterValue) { |
||
| 342 | 5 | case 'by': |
|
| 343 | 4 | case 'self': |
|
| 344 | 3 | case 'all': |
|
| 345 | 2 | case 'filter': |
|
| 346 | 3 | return $filterValue; |
|
| 347 | default: |
||
| 348 | 2 | if ($this->activityManager->isFilterValid($filterValue)) { |
|
| 349 | 1 | return $filterValue; |
|
| 350 | } |
||
| 351 | 1 | return 'all'; |
|
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Delete old events |
||
| 357 | * |
||
| 358 | * @param int $expireDays Minimum 1 day |
||
| 359 | * @return null |
||
| 360 | */ |
||
| 361 | 2 | public function expire($expireDays = 365) { |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Delete activities that match certain conditions |
||
| 372 | * |
||
| 373 | * @param array $conditions Array with conditions that have to be met |
||
| 374 | * 'field' => 'value' => `field` = 'value' |
||
| 375 | * 'field' => array('value', 'operator') => `field` operator 'value' |
||
| 376 | * @return null |
||
| 377 | */ |
||
| 378 | 12 | public function deleteActivities($conditions) { |
|
| 394 | } |
||
| 395 |