Conditions | 20 |
Paths | 165 |
Total Lines | 169 |
Code Lines | 96 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
249 | public function import(Log $log) |
||
250 | { |
||
251 | $db = static::getDatabase(); |
||
252 | $api = static::getApi(); |
||
253 | |||
254 | /* |
||
255 | * This will throw an exception if it's not found |
||
256 | */ |
||
257 | $canvasObject = $api->get($this->getContext()->getVerificationUrl()); |
||
258 | |||
259 | if (!DataUtilities::URLexists($this>getFeedUrl())) { |
||
260 | throw new Exception( |
||
261 | "Cannot sync calendars with a valid calendar feed" |
||
262 | ); |
||
263 | } |
||
264 | |||
265 | if ($log) { |
||
266 | $log->log(static::getSyncTimestamp() . ' sync started', PEAR_LOG_INFO); |
||
267 | } |
||
268 | $this->save(); |
||
269 | |||
270 | $ics = new vcalendar([ |
||
271 | 'unique_id' => __FILE__, |
||
272 | 'url' => $this->getFeedUrl() |
||
273 | ]); |
||
274 | $ics->parse(); |
||
275 | |||
276 | /* |
||
277 | * TODO: would it be worth the performance improvement to just process |
||
278 | * things from today's date forward? (i.e. ignore old items, even |
||
279 | * if they've changed...) |
||
280 | */ |
||
281 | /* |
||
282 | * TODO:0 the best window for syncing would be the term of the course |
||
283 | * in question, right? issue:12 |
||
284 | */ |
||
285 | /* |
||
286 | * TODO:0 Arbitrarily selecting events in for a year on either side of |
||
287 | * today's date, probably a better system? issue:12 |
||
288 | */ |
||
289 | foreach ($ics->selectComponents( |
||
290 | date('Y')-1, // startYear |
||
291 | date('m'), // startMonth |
||
292 | date('d'), // startDay |
||
293 | date('Y')+1, // endYEar |
||
294 | date('m'), // endMonth |
||
295 | date('d'), // endDay |
||
296 | 'vevent', // cType |
||
297 | false, // flat |
||
298 | true, // any |
||
299 | true // split |
||
300 | ) as $year) { |
||
301 | foreach ($year as $month => $days) { |
||
302 | foreach ($days as $day => $events) { |
||
303 | foreach ($events as $i => $_event) { |
||
304 | $event = new Event($_event, $this->getPairingHash()); |
||
305 | if ($this->getFilter()->filterEvent($event)) { |
||
306 | $eventCache = Event::load($this->getPairingHash(), $eventHash); |
||
307 | if (empty($eventCache)) { |
||
308 | /* multi-day event instance start times need to be changed to _this_ date */ |
||
309 | $start = new DateTime( |
||
310 | iCalUtilityFunctions::_date2strdate( |
||
311 | $event->getProperty('DTSTART') |
||
312 | ) |
||
313 | ); |
||
314 | $end = new DateTime( |
||
315 | iCalUtilityFunctions::_date2strdate( |
||
316 | $event->getProperty('DTEND') |
||
317 | ) |
||
318 | ); |
||
319 | if ($event->getProperty('X-RECURRENCE')) { |
||
320 | $start = new DateTime($event->getProperty('X-CURRENT-DTSTART')[1]); |
||
321 | $end = new DateTime($event->getProperty('X-CURRENT-DTEND')[1]); |
||
322 | } |
||
323 | $start->setTimeZone(new DateTimeZone(Constants::LOCAL_TIMEZONE)); |
||
324 | $end->setTimeZone(new DateTimeZone(Constants::LOCAL_TIMEZONE)); |
||
325 | |||
326 | try { |
||
327 | $calendarEvent = $api->post( |
||
328 | "/calendar_events", |
||
329 | [ |
||
330 | 'calendar_event' => [ |
||
331 | 'context_code' => $this->getContext() . "_{$canvasObject['id']}", |
||
332 | 'title' => preg_replace( |
||
333 | '%^([^\]]+)(\s*\[[^\]]+\]\s*)+$%', |
||
334 | '\\1', |
||
335 | strip_tags($event->getProperty('SUMMARY')) |
||
336 | ), |
||
337 | 'description' => Markdown::defaultTransform(str_replace( |
||
338 | '\n', |
||
339 | "\n\n", |
||
340 | $event->getProperty('DESCRIPTION', 1) |
||
341 | )), |
||
342 | 'start_at' => $start->format(Constants::CANVAS_TIMESTAMP_FORMAT), |
||
343 | 'end_at' => $end->format(Constants::CANVAS_TIMESTAMP_FORMAT), |
||
344 | 'location_name' => $event->getProperty('LOCATION') |
||
345 | ] |
||
346 | ] |
||
347 | ); |
||
348 | } catch (Exception $e) { |
||
349 | if ($log) { |
||
350 | $log->log($e->getMessage(), PEAR_LOG_ERR); |
||
351 | } else { |
||
352 | throw $e; |
||
353 | } |
||
354 | } |
||
355 | |||
356 | $eventCache = new Event($this->getPairingHash(), $canvasObject['id'], $eventHash); |
||
357 | } |
||
358 | $eventCache->save(); |
||
359 | } |
||
360 | } |
||
361 | } |
||
362 | } |
||
363 | } |
||
364 | |||
365 | $findDeletedEvents = $db->prepare( |
||
366 | "SELECT * |
||
367 | FROM `events` |
||
368 | WHERE |
||
369 | `calendar` = :calendar AND |
||
370 | `synced` != :synced |
||
371 | " |
||
372 | ); |
||
373 | $deleteCachedEvent = $db->prepare( |
||
374 | "DELETE FROM `events` WHERE `id` = :id" |
||
375 | ); |
||
376 | $findDeletedEvents->execute([ |
||
377 | 'calendar' => $this->getPairingHash(), |
||
378 | 'synced' => static::getSyncTimestamp() |
||
379 | ]); |
||
380 | if (($deletedEvents = $findDeletedEvents->fetchAll()) !== false) { |
||
381 | foreach ($deletedEvents as $eventCache) { |
||
382 | try { |
||
383 | $api->delete( |
||
384 | "/calendar_events/{$eventCache['calendar_event[id]']}", |
||
385 | [ |
||
386 | 'cancel_reason' => getSyncTimestamp(), |
||
387 | /* |
||
388 | * TODO: this feels skeevy -- like the empty string |
||
389 | * will break |
||
390 | */ |
||
391 | 'as_user_id' => ($this->getContext()->getContext() == 'user' ? $canvasObject['id'] : '') |
||
392 | ] |
||
393 | ); |
||
394 | $deleteCachedEvent->execute($eventCache['id']); |
||
395 | } catch (Pest_Unauthorized $e) { |
||
396 | if ($log) { |
||
397 | $log->log( |
||
398 | "calendar_event[{$eventCache['calendar_event[id]']}] no longer exists and will be purged from cache.", |
||
399 | PEAR_LOG_WARN |
||
400 | ); |
||
401 | } else { |
||
402 | throw $e; |
||
403 | } |
||
404 | } catch (Exception $e) { |
||
405 | if ($log) { |
||
406 | $log->log($e->getMessage(), PEAR_LOG_ERR); |
||
407 | } else { |
||
408 | throw $e; |
||
409 | } |
||
410 | } |
||
411 | } |
||
412 | } |
||
413 | |||
414 | if ($log) { |
||
415 | $log->log(static::getSyncTimestamp() . ' sync finished', PEAR_LOG_INFO); |
||
416 | } |
||
417 | } |
||
418 | } |
||
419 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.