| Total Complexity | 63 |
| Total Lines | 422 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 51 | class BirthdayService { |
||
| 52 | |||
| 53 | 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, |
||
| 135 | string $cardUri) { |
||
| 136 | if (!$this->isGloballyEnabled()) { |
||
| 137 | return; |
||
| 138 | } |
||
| 139 | |||
| 140 | $targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); |
||
| 141 | $book = $this->cardDavBackEnd->getAddressBookById($addressBookId); |
||
| 142 | $targetPrincipals[] = $book['principaluri']; |
||
| 143 | foreach ($targetPrincipals as $principalUri) { |
||
| 144 | if (!$this->isUserEnabled($principalUri)) { |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | |||
| 148 | $calendar = $this->ensureCalendarExists($principalUri); |
||
| 149 | foreach (['', '-death', '-anniversary'] as $tag) { |
||
| 150 | $objectUri = $book['uri'] . '-' . $cardUri . $tag .'.ics'; |
||
| 151 | $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); |
||
| 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 { |
||
| 162 | $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); |
||
| 163 | if (!is_null($calendar)) { |
||
| 164 | return $calendar; |
||
| 165 | } |
||
| 166 | $this->calDavBackEnd->createCalendar($principal, self::BIRTHDAY_CALENDAR_URI, [ |
||
| 167 | '{DAV:}displayname' => 'Contact birthdays', |
||
| 168 | '{http://apple.com/ns/ical/}calendar-color' => '#FFFFCA', |
||
| 169 | 'components' => 'VEVENT', |
||
| 170 | ]); |
||
| 171 | |||
| 172 | return $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); |
||
| 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, |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $user |
||
| 287 | */ |
||
| 288 | public function resetForUser(string $user):void { |
||
| 289 | $principal = 'principals/users/'.$user; |
||
| 290 | $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); |
||
| 291 | $calendarObjects = $this->calDavBackEnd->getCalendarObjects($calendar['id'], CalDavBackend::CALENDAR_TYPE_CALENDAR); |
||
| 292 | |||
| 293 | foreach($calendarObjects as $calendarObject) { |
||
| 294 | $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $calendarObject['uri'], CalDavBackend::CALENDAR_TYPE_CALENDAR); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $user |
||
| 300 | * @throws \Sabre\DAV\Exception\BadRequest |
||
| 301 | */ |
||
| 302 | public function syncUser(string $user):void { |
||
| 303 | $principal = 'principals/users/'.$user; |
||
| 304 | $this->ensureCalendarExists($principal); |
||
| 305 | $books = $this->cardDavBackEnd->getAddressBooksForUser($principal); |
||
| 306 | foreach($books as $book) { |
||
| 307 | $cards = $this->cardDavBackEnd->getCards($book['id']); |
||
| 308 | foreach($cards as $card) { |
||
| 309 | $this->onCardChanged($book['id'], $card['uri'], $card['carddata']); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $existingCalendarData |
||
| 316 | * @param VCalendar $newCalendarData |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function birthdayEvenChanged(string $existingCalendarData, |
||
| 320 | VCalendar $newCalendarData):bool { |
||
| 321 | try { |
||
| 322 | $existingBirthday = Reader::read($existingCalendarData); |
||
| 323 | } catch (Exception $ex) { |
||
| 324 | return true; |
||
| 325 | } |
||
| 326 | |||
| 327 | return ( |
||
| 328 | $newCalendarData->VEVENT->DTSTART->getValue() !== $existingBirthday->VEVENT->DTSTART->getValue() || |
||
| 329 | $newCalendarData->VEVENT->SUMMARY->getValue() !== $existingBirthday->VEVENT->SUMMARY->getValue() |
||
| 330 | ); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param integer $addressBookId |
||
| 335 | * @return mixed |
||
| 336 | */ |
||
| 337 | protected function getAllAffectedPrincipals(int $addressBookId) { |
||
| 338 | $targetPrincipals = []; |
||
| 339 | $shares = $this->cardDavBackEnd->getShares($addressBookId); |
||
| 340 | foreach ($shares as $share) { |
||
| 341 | if ($share['{http://owncloud.org/ns}group-share']) { |
||
| 342 | $users = $this->principalBackend->getGroupMemberSet($share['{http://owncloud.org/ns}principal']); |
||
| 343 | foreach ($users as $user) { |
||
| 344 | $targetPrincipals[] = $user['uri']; |
||
| 345 | } |
||
| 346 | } else { |
||
| 347 | $targetPrincipals[] = $share['{http://owncloud.org/ns}principal']; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | return array_values(array_unique($targetPrincipals, SORT_STRING)); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $cardUri |
||
| 355 | * @param string $cardData |
||
| 356 | * @param array $book |
||
| 357 | * @param int $calendarId |
||
| 358 | * @param array $type |
||
| 359 | * @throws InvalidDataException |
||
| 360 | * @throws \Sabre\DAV\Exception\BadRequest |
||
| 361 | */ |
||
| 362 | private function updateCalendar(string $cardUri, |
||
| 363 | string $cardData, |
||
| 364 | array $book, |
||
| 365 | int $calendarId, |
||
| 366 | array $type):void { |
||
| 367 | $objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics'; |
||
| 368 | $calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix']); |
||
| 369 | $existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri); |
||
| 370 | if (is_null($calendarData)) { |
||
| 371 | if (!is_null($existing)) { |
||
| 372 | $this->calDavBackEnd->deleteCalendarObject($calendarId, $objectUri); |
||
| 373 | } |
||
| 374 | } else { |
||
| 375 | if (is_null($existing)) { |
||
| 376 | $this->calDavBackEnd->createCalendarObject($calendarId, $objectUri, $calendarData->serialize()); |
||
| 377 | } else { |
||
| 378 | if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) { |
||
| 379 | $this->calDavBackEnd->updateCalendarObject($calendarId, $objectUri, $calendarData->serialize()); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * checks if the admin opted-out of birthday calendars |
||
| 387 | * |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | private function isGloballyEnabled():bool { |
||
| 391 | return $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes') === 'yes'; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Checks if the user opted-out of birthday calendars |
||
| 396 | * |
||
| 397 | * @param string $userPrincipal The user principal to check for |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | private function isUserEnabled(string $userPrincipal):bool { |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Formats title of Birthday event |
||
| 413 | * |
||
| 414 | * @param string $field Field name like BDAY, ANNIVERSARY, ... |
||
| 415 | * @param string $name Name of contact |
||
| 416 | * @param int|null $year Year of birth, anniversary, ... |
||
| 417 | * @param bool $supports4Byte Whether or not the database supports 4 byte chars |
||
| 418 | * @return string The formatted title |
||
| 419 | */ |
||
| 420 | private function formatTitle(string $field, |
||
| 473 | } |
||
| 474 | } |
||
| 475 | } |
||
| 476 | } |
||
| 477 |
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.