| Total Complexity | 65 |
| Total Lines | 434 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like BirthdayService 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.
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 BirthdayService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class BirthdayService { |
||
| 53 | public const BIRTHDAY_CALENDAR_URI = 'contact_birthdays'; |
||
| 54 | |||
| 55 | /** @var GroupPrincipalBackend */ |
||
| 56 | private $principalBackend; |
||
| 57 | |||
| 58 | /** @var CalDavBackend */ |
||
| 59 | private $calDavBackEnd; |
||
| 60 | |||
| 61 | /** @var CardDavBackend */ |
||
| 62 | private $cardDavBackEnd; |
||
| 63 | |||
| 64 | /** @var IConfig */ |
||
| 65 | private $config; |
||
| 66 | |||
| 67 | /** @var IDBConnection */ |
||
| 68 | private $dbConnection; |
||
| 69 | |||
| 70 | /** @var IL10N */ |
||
| 71 | private $l10n; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * BirthdayService constructor. |
||
| 75 | * |
||
| 76 | * @param CalDavBackend $calDavBackEnd |
||
| 77 | * @param CardDavBackend $cardDavBackEnd |
||
| 78 | * @param GroupPrincipalBackend $principalBackend |
||
| 79 | * @param IConfig $config |
||
| 80 | * @param IDBConnection $dbConnection |
||
| 81 | * @param IL10N $l10n |
||
| 82 | */ |
||
| 83 | public function __construct(CalDavBackend $calDavBackEnd, |
||
| 84 | CardDavBackend $cardDavBackEnd, |
||
| 85 | GroupPrincipalBackend $principalBackend, |
||
| 86 | IConfig $config, |
||
| 87 | IDBConnection $dbConnection, |
||
| 88 | IL10N $l10n) { |
||
| 89 | $this->calDavBackEnd = $calDavBackEnd; |
||
| 90 | $this->cardDavBackEnd = $cardDavBackEnd; |
||
| 91 | $this->principalBackend = $principalBackend; |
||
| 92 | $this->config = $config; |
||
| 93 | $this->dbConnection = $dbConnection; |
||
| 94 | $this->l10n = $l10n; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param int $addressBookId |
||
| 99 | * @param string $cardUri |
||
| 100 | * @param string $cardData |
||
| 101 | */ |
||
| 102 | public function onCardChanged(int $addressBookId, |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param int $addressBookId |
||
| 132 | * @param string $cardUri |
||
| 133 | */ |
||
| 134 | public function onCardDeleted(int $addressBookId, |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param string $principal |
||
| 158 | * @return array|null |
||
| 159 | * @throws \Sabre\DAV\Exception\BadRequest |
||
| 160 | */ |
||
| 161 | public function ensureCalendarExists(string $principal):?array { |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param $cardData |
||
| 177 | * @param $dateField |
||
| 178 | * @param $postfix |
||
| 179 | * @return VCalendar|null |
||
| 180 | * @throws InvalidDataException |
||
| 181 | */ |
||
| 182 | public function buildDateFromContact(string $cardData, |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $user |
||
| 300 | */ |
||
| 301 | public function resetForUser(string $user):void { |
||
| 302 | $principal = 'principals/users/'.$user; |
||
| 303 | $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); |
||
| 304 | $calendarObjects = $this->calDavBackEnd->getCalendarObjects($calendar['id'], CalDavBackend::CALENDAR_TYPE_CALENDAR); |
||
| 305 | |||
| 306 | foreach ($calendarObjects as $calendarObject) { |
||
| 307 | $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $calendarObject['uri'], CalDavBackend::CALENDAR_TYPE_CALENDAR); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param string $user |
||
| 313 | * @throws \Sabre\DAV\Exception\BadRequest |
||
| 314 | */ |
||
| 315 | public function syncUser(string $user):void { |
||
| 316 | $principal = 'principals/users/'.$user; |
||
| 317 | $this->ensureCalendarExists($principal); |
||
| 318 | $books = $this->cardDavBackEnd->getAddressBooksForUser($principal); |
||
| 319 | foreach ($books as $book) { |
||
| 320 | $cards = $this->cardDavBackEnd->getCards($book['id']); |
||
| 321 | foreach ($cards as $card) { |
||
| 322 | $this->onCardChanged((int) $book['id'], $card['uri'], $card['carddata']); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $existingCalendarData |
||
| 329 | * @param VCalendar $newCalendarData |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | public function birthdayEvenChanged(string $existingCalendarData, |
||
| 333 | VCalendar $newCalendarData):bool { |
||
| 334 | try { |
||
| 335 | $existingBirthday = Reader::read($existingCalendarData); |
||
| 336 | } catch (Exception $ex) { |
||
| 337 | return true; |
||
| 338 | } |
||
| 339 | |||
| 340 | return ( |
||
| 341 | $newCalendarData->VEVENT->DTSTART->getValue() !== $existingBirthday->VEVENT->DTSTART->getValue() || |
||
| 342 | $newCalendarData->VEVENT->SUMMARY->getValue() !== $existingBirthday->VEVENT->SUMMARY->getValue() |
||
| 343 | ); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param integer $addressBookId |
||
| 348 | * @return mixed |
||
| 349 | */ |
||
| 350 | protected function getAllAffectedPrincipals(int $addressBookId) { |
||
| 351 | $targetPrincipals = []; |
||
| 352 | $shares = $this->cardDavBackEnd->getShares($addressBookId); |
||
| 353 | foreach ($shares as $share) { |
||
| 354 | if ($share['{http://owncloud.org/ns}group-share']) { |
||
| 355 | $users = $this->principalBackend->getGroupMemberSet($share['{http://owncloud.org/ns}principal']); |
||
| 356 | foreach ($users as $user) { |
||
| 357 | $targetPrincipals[] = $user['uri']; |
||
| 358 | } |
||
| 359 | } else { |
||
| 360 | $targetPrincipals[] = $share['{http://owncloud.org/ns}principal']; |
||
| 361 | } |
||
| 362 | } |
||
| 363 | return array_values(array_unique($targetPrincipals, SORT_STRING)); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $cardUri |
||
| 368 | * @param string $cardData |
||
| 369 | * @param array $book |
||
| 370 | * @param int $calendarId |
||
| 371 | * @param array $type |
||
| 372 | * @throws InvalidDataException |
||
| 373 | * @throws \Sabre\DAV\Exception\BadRequest |
||
| 374 | */ |
||
| 375 | private function updateCalendar(string $cardUri, |
||
| 376 | string $cardData, |
||
| 377 | array $book, |
||
| 378 | int $calendarId, |
||
| 379 | array $type):void { |
||
| 380 | $objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics'; |
||
| 381 | $calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix']); |
||
| 382 | $existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri); |
||
| 383 | if (is_null($calendarData)) { |
||
| 384 | if (!is_null($existing)) { |
||
| 385 | $this->calDavBackEnd->deleteCalendarObject($calendarId, $objectUri); |
||
| 386 | } |
||
| 387 | } else { |
||
| 388 | if (is_null($existing)) { |
||
| 389 | $this->calDavBackEnd->createCalendarObject($calendarId, $objectUri, $calendarData->serialize()); |
||
| 390 | } else { |
||
| 391 | if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) { |
||
| 392 | $this->calDavBackEnd->updateCalendarObject($calendarId, $objectUri, $calendarData->serialize()); |
||
| 393 | } |
||
| 394 | } |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * checks if the admin opted-out of birthday calendars |
||
| 400 | * |
||
| 401 | * @return bool |
||
| 402 | */ |
||
| 403 | private function isGloballyEnabled():bool { |
||
| 404 | return $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes') === 'yes'; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Checks if the user opted-out of birthday calendars |
||
| 409 | * |
||
| 410 | * @param string $userPrincipal The user principal to check for |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | private function isUserEnabled(string $userPrincipal):bool { |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Formats title of Birthday event |
||
| 426 | * |
||
| 427 | * @param string $field Field name like BDAY, ANNIVERSARY, ... |
||
| 428 | * @param string $name Name of contact |
||
| 429 | * @param int|null $year Year of birth, anniversary, ... |
||
| 430 | * @param bool $supports4Byte Whether or not the database supports 4 byte chars |
||
| 431 | * @return string The formatted title |
||
| 432 | */ |
||
| 433 | private function formatTitle(string $field, |
||
| 486 | } |
||
| 487 | } |
||
| 488 | } |
||
| 490 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.