1 | <?php |
||
36 | class Sync |
||
37 | { |
||
38 | /** |
||
39 | * @var Caldav |
||
40 | */ |
||
41 | private $calendar; |
||
42 | |||
43 | /** |
||
44 | * Sync constructor. |
||
45 | * @param Caldav $calendar |
||
46 | */ |
||
47 | 6 | public function __construct(Caldav $calendar) |
|
48 | { |
||
49 | 6 | $this->calendar = $calendar; |
|
50 | 6 | } |
|
51 | |||
52 | /** |
||
53 | * @return Caldav |
||
54 | */ |
||
55 | public function getCalendar() |
||
56 | { |
||
57 | return $this->calendar; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * perform the sync |
||
62 | */ |
||
63 | 6 | public function sync() |
|
64 | { |
||
65 | 6 | $this->syncToTimeSlots(); |
|
66 | 6 | } |
|
67 | |||
68 | /** |
||
69 | * |
||
70 | */ |
||
71 | 6 | private function syncToTimeSlots() |
|
72 | { |
||
73 | try { |
||
74 | 6 | $events = $this->getEvents(); |
|
75 | 6 | } catch (\it\thecsea\caldav_client_adapter\CaldavException $e) { |
|
76 | 6 | \Event::fire(new ErrorEvent($this->calendar, $e->getMessage())); |
|
77 | 6 | return; |
|
78 | } catch (\Illuminate\Contracts\Encryption\DecryptException $e) { |
||
79 | \Event::fire(new ErrorEvent($this->calendar, $e->getMessage())); |
||
80 | return; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @var $calendarMain \plunner\Calendar |
||
85 | */ |
||
86 | $calendarMain = $this->calendar->Calendar; |
||
87 | |||
88 | //remove old timeslots |
||
89 | $calendarMain->timeslots()->delete(); |
||
90 | |||
91 | //insert new timeslots |
||
92 | foreach ($events as $event) { |
||
93 | if (!($event = $this->parseEvent($event))) |
||
94 | \Event::fire(new ErrorEvent($this->calendar, 'problem during the parsing an event')); |
||
95 | else |
||
96 | $calendarMain->timeslots()->create($event); |
||
97 | } |
||
98 | \Event::fire(new OkEvent($this->calendar)); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return array|\it\thecsea\caldav_client_adapter\EventInterface[] |
||
103 | * @throws \it\thecsea\caldav_client_adapter\CaldavException |
||
104 | * @throws \Illuminate\Contracts\Encryption\DecryptException |
||
105 | * @throws CaldavException |
||
106 | */ |
||
107 | 6 | private function getEvents() |
|
122 | |||
123 | /** |
||
124 | * @param EventInterface $event |
||
125 | * @return \DateTime[]|null |
||
126 | */ |
||
127 | private function parseEvent(EventInterface $event) |
||
146 | |||
147 | /** |
||
148 | * @param String $date |
||
149 | * @return \DateTime|null|false |
||
150 | */ |
||
151 | private function parseDate($date) |
||
163 | } |