| Total Complexity | 61 |
| Total Lines | 370 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like RefreshWebcalService 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 RefreshWebcalService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class RefreshWebcalService { |
||
| 54 | |||
| 55 | /** @var CalDavBackend */ |
||
| 56 | private $calDavBackend; |
||
| 57 | |||
| 58 | /** @var IClientService */ |
||
| 59 | private $clientService; |
||
| 60 | |||
| 61 | /** @var IConfig */ |
||
| 62 | private $config; |
||
| 63 | |||
| 64 | /** @var ILogger */ |
||
| 65 | private $logger; |
||
| 66 | |||
| 67 | public const REFRESH_RATE = '{http://apple.com/ns/ical/}refreshrate'; |
||
| 68 | public const STRIP_ALARMS = '{http://calendarserver.org/ns/}subscribed-strip-alarms'; |
||
| 69 | public const STRIP_ATTACHMENTS = '{http://calendarserver.org/ns/}subscribed-strip-attachments'; |
||
| 70 | public const STRIP_TODOS = '{http://calendarserver.org/ns/}subscribed-strip-todos'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * RefreshWebcalJob constructor. |
||
| 74 | * |
||
| 75 | * @param CalDavBackend $calDavBackend |
||
| 76 | * @param IClientService $clientService |
||
| 77 | * @param IConfig $config |
||
| 78 | * @param ILogger $logger |
||
| 79 | */ |
||
| 80 | public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, IConfig $config, ILogger $logger) { |
||
| 81 | $this->calDavBackend = $calDavBackend; |
||
| 82 | $this->clientService = $clientService; |
||
| 83 | $this->config = $config; |
||
| 84 | $this->logger = $logger; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $principalUri |
||
| 89 | * @param string $uri |
||
| 90 | */ |
||
| 91 | public function refreshSubscription(string $principalUri, string $uri) { |
||
| 92 | $subscription = $this->getSubscription($principalUri, $uri); |
||
| 93 | $mutations = []; |
||
| 94 | if (!$subscription) { |
||
| 95 | return; |
||
| 96 | } |
||
| 97 | |||
| 98 | $webcalData = $this->queryWebcalFeed($subscription, $mutations); |
||
| 99 | if (!$webcalData) { |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | $stripTodos = ($subscription[self::STRIP_TODOS] ?? 1) === 1; |
||
| 104 | $stripAlarms = ($subscription[self::STRIP_ALARMS] ?? 1) === 1; |
||
| 105 | $stripAttachments = ($subscription[self::STRIP_ATTACHMENTS] ?? 1) === 1; |
||
| 106 | |||
| 107 | try { |
||
| 108 | $splitter = new ICalendar($webcalData, Reader::OPTION_FORGIVING); |
||
|
|
|||
| 109 | |||
| 110 | // we wait with deleting all outdated events till we parsed the new ones |
||
| 111 | // in case the new calendar is broken and `new ICalendar` throws a ParseException |
||
| 112 | // the user will still see the old data |
||
| 113 | $this->calDavBackend->purgeAllCachedEventsForSubscription($subscription['id']); |
||
| 114 | |||
| 115 | while ($vObject = $splitter->getNext()) { |
||
| 116 | /** @var Component $vObject */ |
||
| 117 | $compName = null; |
||
| 118 | |||
| 119 | foreach ($vObject->getComponents() as $component) { |
||
| 120 | if ($component->name === 'VTIMEZONE') { |
||
| 121 | continue; |
||
| 122 | } |
||
| 123 | |||
| 124 | $compName = $component->name; |
||
| 125 | |||
| 126 | if ($stripAlarms) { |
||
| 127 | unset($component->{'VALARM'}); |
||
| 128 | } |
||
| 129 | if ($stripAttachments) { |
||
| 130 | unset($component->{'ATTACH'}); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | if ($stripTodos && $compName === 'VTODO') { |
||
| 135 | continue; |
||
| 136 | } |
||
| 137 | |||
| 138 | $uri = $this->getRandomCalendarObjectUri(); |
||
| 139 | $calendarData = $vObject->serialize(); |
||
| 140 | try { |
||
| 141 | $this->calDavBackend->createCalendarObject($subscription['id'], $uri, $calendarData, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION); |
||
| 142 | } catch(BadRequest $ex) { |
||
| 143 | $this->logger->logException($ex); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | $newRefreshRate = $this->checkWebcalDataForRefreshRate($subscription, $webcalData); |
||
| 148 | if ($newRefreshRate) { |
||
| 149 | $mutations[self::REFRESH_RATE] = $newRefreshRate; |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->updateSubscription($subscription, $mutations); |
||
| 153 | } catch(ParseException $ex) { |
||
| 154 | $subscriptionId = $subscription['id']; |
||
| 155 | |||
| 156 | $this->logger->logException($ex); |
||
| 157 | $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error"); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * loads subscription from backend |
||
| 163 | * |
||
| 164 | * @param string $principalUri |
||
| 165 | * @param string $uri |
||
| 166 | * @return array|null |
||
| 167 | */ |
||
| 168 | public function getSubscription(string $principalUri, string $uri) { |
||
| 169 | $subscriptions = array_values(array_filter( |
||
| 170 | $this->calDavBackend->getSubscriptionsForUser($principalUri), |
||
| 171 | function($sub) use ($uri) { |
||
| 172 | return $sub['uri'] === $uri; |
||
| 173 | } |
||
| 174 | )); |
||
| 175 | |||
| 176 | if (count($subscriptions) === 0) { |
||
| 177 | return null; |
||
| 178 | } |
||
| 179 | |||
| 180 | return $subscriptions[0]; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * gets webcal feed from remote server |
||
| 185 | * |
||
| 186 | * @param array $subscription |
||
| 187 | * @param array &$mutations |
||
| 188 | * @return null|string |
||
| 189 | */ |
||
| 190 | private function queryWebcalFeed(array $subscription, array &$mutations) { |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * check if: |
||
| 321 | * - current subscription stores a refreshrate |
||
| 322 | * - the webcal feed suggests a refreshrate |
||
| 323 | * - return suggested refreshrate if user didn't set a custom one |
||
| 324 | * |
||
| 325 | * @param array $subscription |
||
| 326 | * @param string $webcalData |
||
| 327 | * @return string|null |
||
| 328 | */ |
||
| 329 | private function checkWebcalDataForRefreshRate($subscription, $webcalData) { |
||
| 330 | // if there is no refreshrate stored in the database, check the webcal feed |
||
| 331 | // whether it suggests any refresh rate and store that in the database |
||
| 332 | if (isset($subscription[self::REFRESH_RATE]) && $subscription[self::REFRESH_RATE] !== null) { |
||
| 333 | return null; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** @var Component\VCalendar $vCalendar */ |
||
| 337 | $vCalendar = Reader::read($webcalData); |
||
| 338 | |||
| 339 | $newRefreshRate = null; |
||
| 340 | if (isset($vCalendar->{'X-PUBLISHED-TTL'})) { |
||
| 341 | $newRefreshRate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue(); |
||
| 342 | } |
||
| 343 | if (isset($vCalendar->{'REFRESH-INTERVAL'})) { |
||
| 344 | $newRefreshRate = $vCalendar->{'REFRESH-INTERVAL'}->getValue(); |
||
| 345 | } |
||
| 346 | |||
| 347 | if (!$newRefreshRate) { |
||
| 348 | return null; |
||
| 349 | } |
||
| 350 | |||
| 351 | // check if new refresh rate is even valid |
||
| 352 | try { |
||
| 353 | DateTimeParser::parseDuration($newRefreshRate); |
||
| 354 | } catch(InvalidDataException $ex) { |
||
| 355 | return null; |
||
| 356 | } |
||
| 357 | |||
| 358 | return $newRefreshRate; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * update subscription stored in database |
||
| 363 | * used to set: |
||
| 364 | * - refreshrate |
||
| 365 | * - source |
||
| 366 | * |
||
| 367 | * @param array $subscription |
||
| 368 | * @param array $mutations |
||
| 369 | */ |
||
| 370 | private function updateSubscription(array $subscription, array $mutations) { |
||
| 371 | if (empty($mutations)) { |
||
| 372 | return; |
||
| 373 | } |
||
| 374 | |||
| 375 | $propPatch = new PropPatch($mutations); |
||
| 376 | $this->calDavBackend->updateSubscription($subscription['id'], $propPatch); |
||
| 377 | $propPatch->commit(); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * This method will strip authentication information and replace the |
||
| 382 | * 'webcal' or 'webcals' protocol scheme |
||
| 383 | * |
||
| 384 | * @param string $url |
||
| 385 | * @return string|null |
||
| 386 | */ |
||
| 387 | private function cleanURL(string $url) { |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Returns a random uri for a calendar-object |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | public function getRandomCalendarObjectUri():string { |
||
| 423 | } |
||
| 424 | } |
||
| 425 |