Total Complexity | 50 |
Total Lines | 346 |
Duplicated Lines | 0 % |
Changes | 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 | $compName = null; |
||
117 | |||
118 | foreach ($vObject->getComponents() as $component) { |
||
119 | if ($component->name === 'VTIMEZONE') { |
||
120 | continue; |
||
121 | } |
||
122 | |||
123 | $compName = $component->name; |
||
124 | |||
125 | if ($stripAlarms) { |
||
126 | unset($component->{'VALARM'}); |
||
127 | } |
||
128 | if ($stripAttachments) { |
||
129 | unset($component->{'ATTACH'}); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | if ($stripTodos && $compName === 'VTODO') { |
||
134 | continue; |
||
135 | } |
||
136 | |||
137 | $uri = $this->getRandomCalendarObjectUri(); |
||
138 | $calendarData = $vObject->serialize(); |
||
139 | try { |
||
140 | $this->calDavBackend->createCalendarObject($subscription['id'], $uri, $calendarData, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION); |
||
141 | } catch (BadRequest $ex) { |
||
142 | $this->logger->logException($ex); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | $newRefreshRate = $this->checkWebcalDataForRefreshRate($subscription, $webcalData); |
||
147 | if ($newRefreshRate) { |
||
148 | $mutations[self::REFRESH_RATE] = $newRefreshRate; |
||
149 | } |
||
150 | |||
151 | $this->updateSubscription($subscription, $mutations); |
||
152 | } catch (ParseException $ex) { |
||
153 | $subscriptionId = $subscription['id']; |
||
154 | |||
155 | $this->logger->logException($ex); |
||
156 | $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error"); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * loads subscription from backend |
||
162 | * |
||
163 | * @param string $principalUri |
||
164 | * @param string $uri |
||
165 | * @return array|null |
||
166 | */ |
||
167 | public function getSubscription(string $principalUri, string $uri) { |
||
168 | $subscriptions = array_values(array_filter( |
||
169 | $this->calDavBackend->getSubscriptionsForUser($principalUri), |
||
170 | function ($sub) use ($uri) { |
||
171 | return $sub['uri'] === $uri; |
||
172 | } |
||
173 | )); |
||
174 | |||
175 | if (count($subscriptions) === 0) { |
||
176 | return null; |
||
177 | } |
||
178 | |||
179 | return $subscriptions[0]; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * gets webcal feed from remote server |
||
184 | * |
||
185 | * @param array $subscription |
||
186 | * @param array &$mutations |
||
187 | * @return null|string |
||
188 | */ |
||
189 | private function queryWebcalFeed(array $subscription, array &$mutations) { |
||
291 | } |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * check if: |
||
296 | * - current subscription stores a refreshrate |
||
297 | * - the webcal feed suggests a refreshrate |
||
298 | * - return suggested refreshrate if user didn't set a custom one |
||
299 | * |
||
300 | * @param array $subscription |
||
301 | * @param string $webcalData |
||
302 | * @return string|null |
||
303 | */ |
||
304 | private function checkWebcalDataForRefreshRate($subscription, $webcalData) { |
||
305 | // if there is no refreshrate stored in the database, check the webcal feed |
||
306 | // whether it suggests any refresh rate and store that in the database |
||
307 | if (isset($subscription[self::REFRESH_RATE]) && $subscription[self::REFRESH_RATE] !== null) { |
||
308 | return null; |
||
309 | } |
||
310 | |||
311 | /** @var Component\VCalendar $vCalendar */ |
||
312 | $vCalendar = Reader::read($webcalData); |
||
313 | |||
314 | $newRefreshRate = null; |
||
315 | if (isset($vCalendar->{'X-PUBLISHED-TTL'})) { |
||
316 | $newRefreshRate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue(); |
||
317 | } |
||
318 | if (isset($vCalendar->{'REFRESH-INTERVAL'})) { |
||
319 | $newRefreshRate = $vCalendar->{'REFRESH-INTERVAL'}->getValue(); |
||
320 | } |
||
321 | |||
322 | if (!$newRefreshRate) { |
||
323 | return null; |
||
324 | } |
||
325 | |||
326 | // check if new refresh rate is even valid |
||
327 | try { |
||
328 | DateTimeParser::parseDuration($newRefreshRate); |
||
329 | } catch (InvalidDataException $ex) { |
||
330 | return null; |
||
331 | } |
||
332 | |||
333 | return $newRefreshRate; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * update subscription stored in database |
||
338 | * used to set: |
||
339 | * - refreshrate |
||
340 | * - source |
||
341 | * |
||
342 | * @param array $subscription |
||
343 | * @param array $mutations |
||
344 | */ |
||
345 | private function updateSubscription(array $subscription, array $mutations) { |
||
346 | if (empty($mutations)) { |
||
347 | return; |
||
348 | } |
||
349 | |||
350 | $propPatch = new PropPatch($mutations); |
||
351 | $this->calDavBackend->updateSubscription($subscription['id'], $propPatch); |
||
352 | $propPatch->commit(); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * This method will strip authentication information and replace the |
||
357 | * 'webcal' or 'webcals' protocol scheme |
||
358 | * |
||
359 | * @param string $url |
||
360 | * @return string|null |
||
361 | */ |
||
362 | private function cleanURL(string $url) { |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Returns a random uri for a calendar-object |
||
393 | * |
||
394 | * @return string |
||
395 | */ |
||
396 | public function getRandomCalendarObjectUri():string { |
||
398 | } |
||
399 | } |
||
400 |