Total Complexity | 53 |
Total Lines | 347 |
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 |
||
44 | class BirthdayService { |
||
45 | |||
46 | const BIRTHDAY_CALENDAR_URI = 'contact_birthdays'; |
||
47 | |||
48 | /** @var GroupPrincipalBackend */ |
||
49 | private $principalBackend; |
||
50 | |||
51 | /** @var CalDavBackend */ |
||
52 | private $calDavBackEnd; |
||
53 | |||
54 | /** @var CardDavBackend */ |
||
55 | private $cardDavBackEnd; |
||
56 | |||
57 | /** @var IConfig */ |
||
58 | private $config; |
||
59 | |||
60 | /** @var IDBConnection */ |
||
61 | private $dbConnection; |
||
62 | |||
63 | /** |
||
64 | * BirthdayService constructor. |
||
65 | * |
||
66 | * @param CalDavBackend $calDavBackEnd |
||
67 | * @param CardDavBackend $cardDavBackEnd |
||
68 | * @param GroupPrincipalBackend $principalBackend |
||
69 | * @param IConfig $config; |
||
70 | */ |
||
71 | public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend, IConfig $config, IDBConnection $dbConnection) { |
||
72 | $this->calDavBackEnd = $calDavBackEnd; |
||
73 | $this->cardDavBackEnd = $cardDavBackEnd; |
||
74 | $this->principalBackend = $principalBackend; |
||
75 | $this->config = $config; |
||
76 | $this->dbConnection = $dbConnection; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param int $addressBookId |
||
81 | * @param string $cardUri |
||
82 | * @param string $cardData |
||
83 | */ |
||
84 | public function onCardChanged($addressBookId, $cardUri, $cardData) { |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param int $addressBookId |
||
111 | * @param string $cardUri |
||
112 | */ |
||
113 | public function onCardDeleted($addressBookId, $cardUri) { |
||
114 | if (!$this->isGloballyEnabled()) { |
||
115 | return; |
||
116 | } |
||
117 | |||
118 | $targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); |
||
119 | $book = $this->cardDavBackEnd->getAddressBookById($addressBookId); |
||
120 | $targetPrincipals[] = $book['principaluri']; |
||
121 | foreach ($targetPrincipals as $principalUri) { |
||
122 | if (!$this->isUserEnabled($principalUri)) { |
||
123 | continue; |
||
124 | } |
||
125 | |||
126 | $calendar = $this->ensureCalendarExists($principalUri); |
||
127 | foreach (['', '-death', '-anniversary'] as $tag) { |
||
128 | $objectUri = $book['uri'] . '-' . $cardUri . $tag .'.ics'; |
||
129 | $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param string $principal |
||
136 | * @return array|null |
||
137 | * @throws \Sabre\DAV\Exception\BadRequest |
||
138 | */ |
||
139 | public function ensureCalendarExists($principal) { |
||
140 | $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); |
||
141 | if (!is_null($calendar)) { |
||
142 | return $calendar; |
||
143 | } |
||
144 | $this->calDavBackEnd->createCalendar($principal, self::BIRTHDAY_CALENDAR_URI, [ |
||
145 | '{DAV:}displayname' => 'Contact birthdays', |
||
146 | '{http://apple.com/ns/ical/}calendar-color' => '#FFFFCA', |
||
147 | 'components' => 'VEVENT', |
||
148 | ]); |
||
149 | |||
150 | return $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param string $cardData |
||
155 | * @param string $dateField |
||
156 | * @param string $postfix |
||
157 | * @param string $summarySymbol |
||
158 | * @param string $utfSummarySymbol |
||
159 | * @return null|VCalendar |
||
160 | */ |
||
161 | public function buildDateFromContact($cardData, $dateField, $postfix, $summarySymbol, $utfSummarySymbol) { |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param string $user |
||
275 | */ |
||
276 | public function resetForUser($user) { |
||
277 | $principal = 'principals/users/'.$user; |
||
278 | $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); |
||
279 | $calendarObjects = $this->calDavBackEnd->getCalendarObjects($calendar['id'], CalDavBackend::CALENDAR_TYPE_CALENDAR); |
||
280 | |||
281 | foreach($calendarObjects as $calendarObject) { |
||
282 | $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $calendarObject['uri'], CalDavBackend::CALENDAR_TYPE_CALENDAR); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @param string $user |
||
288 | */ |
||
289 | public function syncUser($user) { |
||
290 | $principal = 'principals/users/'.$user; |
||
291 | $this->ensureCalendarExists($principal); |
||
292 | $books = $this->cardDavBackEnd->getAddressBooksForUser($principal); |
||
293 | foreach($books as $book) { |
||
294 | $cards = $this->cardDavBackEnd->getCards($book['id']); |
||
295 | foreach($cards as $card) { |
||
296 | $this->onCardChanged($book['id'], $card['uri'], $card['carddata']); |
||
297 | } |
||
298 | } |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param string $existingCalendarData |
||
303 | * @param VCalendar $newCalendarData |
||
304 | * @return bool |
||
305 | */ |
||
306 | public function birthdayEvenChanged($existingCalendarData, $newCalendarData) { |
||
307 | try { |
||
308 | $existingBirthday = Reader::read($existingCalendarData); |
||
309 | } catch (Exception $ex) { |
||
310 | return true; |
||
311 | } |
||
312 | if ($newCalendarData->VEVENT->DTSTART->getValue() !== $existingBirthday->VEVENT->DTSTART->getValue() || |
||
313 | $newCalendarData->VEVENT->SUMMARY->getValue() !== $existingBirthday->VEVENT->SUMMARY->getValue() |
||
314 | ) { |
||
315 | return true; |
||
316 | } |
||
317 | return false; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * @param integer $addressBookId |
||
322 | * @return mixed |
||
323 | */ |
||
324 | protected function getAllAffectedPrincipals($addressBookId) { |
||
325 | $targetPrincipals = []; |
||
326 | $shares = $this->cardDavBackEnd->getShares($addressBookId); |
||
327 | foreach ($shares as $share) { |
||
328 | if ($share['{http://owncloud.org/ns}group-share']) { |
||
329 | $users = $this->principalBackend->getGroupMemberSet($share['{http://owncloud.org/ns}principal']); |
||
330 | foreach ($users as $user) { |
||
331 | $targetPrincipals[] = $user['uri']; |
||
332 | } |
||
333 | } else { |
||
334 | $targetPrincipals[] = $share['{http://owncloud.org/ns}principal']; |
||
335 | } |
||
336 | } |
||
337 | return array_values(array_unique($targetPrincipals, SORT_STRING)); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @param string $cardUri |
||
342 | * @param string $cardData |
||
343 | * @param array $book |
||
344 | * @param int $calendarId |
||
345 | * @param string[] $type |
||
346 | */ |
||
347 | private function updateCalendar($cardUri, $cardData, $book, $calendarId, $type) { |
||
348 | $objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics'; |
||
349 | $calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix'], $type['symbol'], $type['utfSymbol']); |
||
350 | $existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri); |
||
351 | if (is_null($calendarData)) { |
||
352 | if (!is_null($existing)) { |
||
353 | $this->calDavBackEnd->deleteCalendarObject($calendarId, $objectUri); |
||
354 | } |
||
355 | } else { |
||
356 | if (is_null($existing)) { |
||
357 | $this->calDavBackEnd->createCalendarObject($calendarId, $objectUri, $calendarData->serialize()); |
||
358 | } else { |
||
359 | if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) { |
||
360 | $this->calDavBackEnd->updateCalendarObject($calendarId, $objectUri, $calendarData->serialize()); |
||
361 | } |
||
362 | } |
||
363 | } |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * checks if the admin opted-out of birthday calendars |
||
368 | * |
||
369 | * @return bool |
||
370 | */ |
||
371 | private function isGloballyEnabled() { |
||
372 | $isGloballyEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes'); |
||
373 | return $isGloballyEnabled === 'yes'; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * checks if the user opted-out of birthday calendars |
||
378 | * |
||
379 | * @param $userPrincipal |
||
380 | * @return bool |
||
381 | */ |
||
382 | private function isUserEnabled($userPrincipal) { |
||
391 | } |
||
392 | |||
393 | } |
||
394 |
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.