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 |
||
| 40 | class Data { |
||
| 41 | /** @var IManager */ |
||
| 42 | protected $activityManager; |
||
| 43 | |||
| 44 | /** @var IDBConnection */ |
||
| 45 | protected $connection; |
||
| 46 | |||
| 47 | /** @var IUserSession */ |
||
| 48 | protected $userSession; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param IManager $activityManager |
||
| 52 | * @param IDBConnection $connection |
||
| 53 | * @param IUserSession $userSession |
||
| 54 | */ |
||
| 55 | 68 | public function __construct(IManager $activityManager, IDBConnection $connection, IUserSession $userSession) { |
|
| 60 | |||
| 61 | protected $notificationTypes = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param IL10N $l |
||
| 65 | * @return array Array "stringID of the type" => "translated string description for the setting" |
||
| 66 | * or Array "stringID of the type" => [ |
||
| 67 | * 'desc' => "translated string description for the setting" |
||
| 68 | * 'methods' => [\OCP\Activity\IExtension::METHOD_*], |
||
| 69 | * ] |
||
| 70 | */ |
||
| 71 | 8 | public function getNotificationTypes(IL10N $l) { |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Send an event into the activity stream |
||
| 84 | * |
||
| 85 | * @param IEvent $event |
||
| 86 | * @return bool |
||
| 87 | */ |
||
| 88 | 4 | public function send(IEvent $event) { |
|
| 89 | 4 | if ($event->getAffectedUser() === '' || $event->getAffectedUser() === null) { |
|
| 90 | 2 | return false; |
|
| 91 | } |
||
| 92 | |||
| 93 | // store in DB |
||
| 94 | 2 | $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 95 | 2 | $queryBuilder->insert('activity') |
|
| 96 | 2 | ->values([ |
|
| 97 | 2 | 'app' => $queryBuilder->createParameter('app'), |
|
| 98 | 2 | 'subject' => $queryBuilder->createParameter('subject'), |
|
| 99 | 2 | 'subjectparams' => $queryBuilder->createParameter('subjectparams'), |
|
| 100 | 2 | 'message' => $queryBuilder->createParameter('message'), |
|
| 101 | 2 | 'messageparams' => $queryBuilder->createParameter('messageparams'), |
|
| 102 | 2 | 'file' => $queryBuilder->createParameter('object_name'), |
|
| 103 | 2 | 'link' => $queryBuilder->createParameter('link'), |
|
| 104 | 2 | 'user' => $queryBuilder->createParameter('user'), |
|
| 105 | 2 | 'affecteduser' => $queryBuilder->createParameter('affecteduser'), |
|
| 106 | 2 | 'timestamp' => $queryBuilder->createParameter('timestamp'), |
|
| 107 | 2 | 'priority' => $queryBuilder->createParameter('priority'), |
|
| 108 | 2 | 'type' => $queryBuilder->createParameter('type'), |
|
| 109 | 2 | 'object_type' => $queryBuilder->createParameter('object_type'), |
|
| 110 | 2 | 'object_id' => $queryBuilder->createParameter('object_id'), |
|
| 111 | ]) |
||
| 112 | 2 | ->setParameters([ |
|
| 113 | 2 | 'app' => $event->getApp(), |
|
| 114 | 2 | 'type' => $event->getType(), |
|
| 115 | 2 | 'affecteduser' => $event->getAffectedUser(), |
|
| 116 | 2 | 'user' => $event->getAuthor(), |
|
| 117 | 2 | 'timestamp' => (int) $event->getTimestamp(), |
|
| 118 | 2 | 'subject' => $event->getSubject(), |
|
| 119 | 2 | 'subjectparams' => json_encode($event->getSubjectParameters()), |
|
| 120 | 2 | 'message' => $event->getMessage(), |
|
| 121 | 2 | 'messageparams' => json_encode($event->getMessageParameters()), |
|
| 122 | 2 | 'priority' => IExtension::PRIORITY_MEDIUM, |
|
| 123 | 2 | 'object_type' => $event->getObjectType(), |
|
| 124 | 2 | 'object_id' => (int) $event->getObjectId(), |
|
| 125 | 2 | 'object_name' => $event->getObjectName(), |
|
| 126 | 2 | 'link' => $event->getLink(), |
|
| 127 | ]) |
||
| 128 | 2 | ->execute(); |
|
| 129 | |||
| 130 | 2 | return true; |
|
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Send an event as email |
||
| 135 | * |
||
| 136 | * @param IEvent $event |
||
| 137 | * @param int $latestSendTime Activity $timestamp + batch setting of $affectedUser |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | 4 | public function storeMail(IEvent $event, $latestSendTime) { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Read a list of events from the activity stream |
||
| 173 | * |
||
| 174 | * @param GroupHelper $groupHelper Allows activities to be grouped |
||
| 175 | * @param UserSettings $userSettings Gets the settings of the user |
||
| 176 | * @param string $user User for whom we display the stream |
||
| 177 | * |
||
| 178 | * @param int $since The integer ID of the last activity that has been seen. |
||
| 179 | * @param int $limit How many activities should be returned |
||
| 180 | * @param string $sort Should activities be given ascending or descending |
||
| 181 | * |
||
| 182 | * @param string $filter Filter the activities |
||
| 183 | * @param string $objectType Allows to filter the activities to a given object. May only appear together with $objectId |
||
| 184 | * @param int $objectId Allows to filter the activities to a given object. May only appear together with $objectType |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | * |
||
| 188 | * @throws \OutOfBoundsException if the user (Code: 1) or the since (Code: 2) is invalid |
||
| 189 | * @throws \BadMethodCallException if the user has selected to display no types for this filter (Code: 3) |
||
| 190 | */ |
||
| 191 | 15 | public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user, $since, $limit, $sort, $filter, $objectType = '', $objectId = 0) { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param IQueryBuilder $query |
||
| 274 | * @param string $user |
||
| 275 | * @param int $since |
||
| 276 | * @param string $sort |
||
| 277 | * |
||
| 278 | * @return array Headers that should be set on the response |
||
| 279 | * |
||
| 280 | * @throws \OutOfBoundsException If $since is not owned by $user |
||
| 281 | */ |
||
| 282 | 18 | protected function setOffsetFromSince(IQueryBuilder $query, $user, $since, $sort) { |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Verify that the filter is valid |
||
| 333 | * |
||
| 334 | * @param string $filterValue |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | 6 | public function validateFilter($filterValue) { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Delete old events |
||
| 358 | * |
||
| 359 | * @param int $expireDays Minimum 1 day |
||
| 360 | * @return null |
||
| 361 | */ |
||
| 362 | 2 | public function expire($expireDays = 365) { |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Delete activities that match certain conditions |
||
| 373 | * |
||
| 374 | * @param array $conditions Array with conditions that have to be met |
||
| 375 | * 'field' => 'value' => `field` = 'value' |
||
| 376 | * 'field' => array('value', 'operator') => `field` operator 'value' |
||
| 377 | * @return null |
||
| 378 | */ |
||
| 379 | 12 | public function deleteActivities($conditions) { |
|
| 395 | } |
||
| 396 |