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 | 66 | 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) { |
|
| 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) { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param IQueryBuilder $query |
||
| 292 | * @param string $user |
||
| 293 | * @param int $since |
||
| 294 | * @param string $sort |
||
| 295 | * |
||
| 296 | * @return array Headers that should be set on the response |
||
| 297 | * |
||
| 298 | * @throws \OutOfBoundsException If $since is not owned by $user |
||
| 299 | */ |
||
| 300 | 18 | protected function setOffsetFromSince(IQueryBuilder $query, $user, $since, $sort) { |
|
| 301 | 18 | if ($since) { |
|
| 302 | 5 | $queryBuilder = $this->connection->getQueryBuilder(); |
|
| 303 | 5 | $queryBuilder->select('*') |
|
| 304 | 5 | ->from('activity') |
|
| 305 | 5 | ->where($queryBuilder->expr()->eq('activity_id', $queryBuilder->createNamedParameter((int) $since))); |
|
| 306 | 5 | $result = $queryBuilder->execute(); |
|
| 307 | 5 | $activity = $result->fetch(); |
|
| 308 | 5 | $result->closeCursor(); |
|
| 309 | |||
| 310 | 5 | if ($activity) { |
|
| 311 | 4 | if ($activity['affecteduser'] !== $user) { |
|
| 312 | 1 | throw new \OutOfBoundsException('Invalid since', 2); |
|
| 313 | } |
||
| 314 | 3 | $timestamp = (int) $activity['timestamp']; |
|
| 315 | |||
| 316 | 3 | if ($sort === 'DESC') { |
|
| 317 | 2 | $query->andWhere($query->expr()->lte('timestamp', $query->createNamedParameter($timestamp))); |
|
| 318 | 1 | $query->andWhere($query->expr()->lt('activity_id', $query->createNamedParameter($since))); |
|
| 319 | 1 | } else { |
|
| 320 | 1 | $query->andWhere($query->expr()->gte('timestamp', $query->createNamedParameter($timestamp))); |
|
| 321 | $query->andWhere($query->expr()->gt('activity_id', $query->createNamedParameter($since))); |
||
| 322 | } |
||
| 323 | 1 | return []; |
|
| 324 | } |
||
| 325 | 1 | } |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Couldn't find the since, so find the oldest one and set the header |
||
| 329 | */ |
||
| 330 | 14 | $fetchQuery = $this->connection->getQueryBuilder(); |
|
| 331 | 14 | $fetchQuery->select('activity_id') |
|
| 332 | 14 | ->from('activity') |
|
| 333 | 14 | ->where($fetchQuery->expr()->eq('affecteduser', $fetchQuery->createNamedParameter($user))) |
|
| 334 | 14 | ->orderBy('timestamp', $sort) |
|
| 335 | 14 | ->setMaxResults(1); |
|
| 336 | 14 | $result = $fetchQuery->execute(); |
|
| 337 | 14 | $activity = $result->fetch(); |
|
| 338 | 14 | $result->closeCursor(); |
|
| 339 | |||
| 340 | 14 | if ($activity !== false) { |
|
| 341 | return [ |
||
| 342 | 12 | 'X-Activity-First-Known' => (int) $activity['activity_id'], |
|
| 343 | 12 | ]; |
|
| 344 | } |
||
| 345 | |||
| 346 | 2 | return []; |
|
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Verify that the filter is valid |
||
| 351 | * |
||
| 352 | * @param string $filterValue |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | 6 | public function validateFilter($filterValue) { |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Delete old events |
||
| 376 | * |
||
| 377 | * @param int $expireDays Minimum 1 day |
||
| 378 | * @return null |
||
| 379 | */ |
||
| 380 | 2 | public function expire($expireDays = 365) { |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Delete activities that match certain conditions |
||
| 391 | * |
||
| 392 | * @param array $conditions Array with conditions that have to be met |
||
| 393 | * 'field' => 'value' => `field` = 'value' |
||
| 394 | * 'field' => array('value', 'operator') => `field` operator 'value' |
||
| 395 | * @return null |
||
| 396 | */ |
||
| 397 | 12 | public function deleteActivities($conditions) { |
|
| 413 | } |
||
| 414 |