Completed
Pull Request — master (#10)
by claudio
03:57
created

Sync::getEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 3
cts 6
cp 0.5
rs 9.4286
cc 1
eloc 6
nc 1
nop 0
crap 1.125
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Claudio Cardinale <[email protected]>
5
 * Date: 03/12/15
6
 * Time: 2.33
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
 */
19
20
namespace plunner\Console\Commands\SyncCaldav;
21
22
use it\thecsea\caldav_client_adapter\simple_caldav_client\SimpleCaldavAdapter;
23
use \it\thecsea\caldav_client_adapter\EventInterface;
24
use plunner\Caldav;
25
use plunner\Events\CaldavErrorEvent;
26
27
/**
28
 * Class Sync
29
 * @package plunner\Console\Commands
30
 * @author Claudio Cardinale <[email protected]>
31
 * @copyright 2015 Claudio Cardinale
32
 * @version 1.0.0
33
 */
34
class Sync
35
{
36
    /**
37
     * @var Caldav
38
     */
39
    private $calendar;
40
41
    /**
42
     * Sync constructor.
43
     * @param Caldav $calendar
44
     */
45 4
    public function __construct(Caldav $calendar)
46
    {
47 4
        $this->calendar = $calendar;
48 4
    }
49
50
    /**
51
     * @return Caldav
52
     */
53 2
    public function getCalendar()
54
    {
55 2
        return $this->calendar;
56
    }
57
58
    /**
59
     * perform the sync
60
     */
61 4
    public function sync()
62
    {
63 4
        $this->syncToTimeSlots();
64 4
    }
65
66
    /**
67
     * @return array|\it\thecsea\caldav_client_adapter\EventInterface[]
68
     * @throws \it\thecsea\caldav_client_adapter\CaldavException
69
     * @thorws \Illuminate\Contracts\Encryption\DecryptException
70
     */
71 4
    private function getEvents()
72
    {
73 4
        $caldavClient = new SimpleCaldavAdapter();
74 4
        $caldavClient->connect($this->calendar->url, $this->calendar->username, \Crypt::decrypt($this->calendar->password));
75
        $calendars = $caldavClient->findCalendars();
76
        $caldavClient->setCalendar($calendars[$this->calendar->calendar_name]);
77
        /**
78
         * 26 hours before to avoid tiemezone problems and dst problems
79
         * 30 days after
80
         */
81
        return $caldavClient->getEvents(date('Ymd\THis\Z', time()-93600), date('Ymd\THis\Z', time()+2592000));
82
    }
83
84
    /**
85
     *
86
     */
87 4
    private function syncToTimeSlots()
88
    {
89
        try
90
        {
91 4
            $events = $this->getEvents();
92 4
        }catch (\it\thecsea\caldav_client_adapter\CaldavException $e)
93
        {
94 4
            \Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage()));
95 4
            return ;
96
        }catch(\Illuminate\Contracts\Encryption\DecryptException $e){
97
            \Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage()));
98
            return ;
99
        }
100
101
        /**
102
         * @var $calendarMain \plunner\Calendar
103
         */
104
        $calendarMain = $this->calendar->Calendar;
105
106
        //remove old timeslots
107
        $calendarMain->timeslots()->delete();
108
        foreach($events as $event){
109
            if(!($event = $this->parseEvent($event)))
110
                \Event::fire(new CaldavErrorEvent($this->calendar, 'problem during the parsing an event'));
111
            $calendarMain->timeslots()->create($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->parseEvent($event) on line 109 can also be of type null; however, Illuminate\Database\Eloq...\HasOneOrMany::create() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
112
        }
113
    }
114
115
    /**
116
     * @param EventInterface $event
117
     * @return \DateTime[]|null
118
     */
119
    private function parseEvent(EventInterface $event)
120
    {
121
        $pattern = "/^((DTSTART;)|(DTEND;))(.*)\$/m";
122
        if(preg_match_all($pattern, $event->getData(), $matches)){
123
            if(!isset($matches[4]) || count($matches[4]) != 2)
124
                return null;
125
            $ret = [];
126
            if($tmp = $this->parseDate($matches[4][0]))
127
                $ret['time_start'] = $tmp;
128
            else
129
                return null;
130
            if($tmp = $this->parseDate($matches[4][1]))
131
                $ret['time_end'] = $tmp;
132
            else
133
                return null;
134
            return $ret;
135
        }
136
        return null;
137
    }
138
139
    /**
140
     * @param String $date
141
     * @return \DateTime|null|false
142
     */
143
    private function parseDate($date)
144
    {
145
        $pattern = "/^((TZID=)|(VALUE=))(.*):(.*)\$/m";
146
        if(preg_match_all($pattern, $date, $matches)){
147
            if($matches[1][0] == 'TZID=')
148
            {
149
                return \DateTime::createFromFormat('Ymd\THis', $matches[5][0], new \DateTimeZone($matches[4][0]));
150
            }else if($matches[1][0] == 'VALUE=' && $matches[4][0] == 'DATE')
151
            {
152
                return \DateTime::createFromFormat('Ymd\THis', $matches[5][0].'T000000');
153
            }
154
        }
155
        return null;
156
    }
157
}