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