Completed
Pull Request — master (#10)
by claudio
07:04
created

Sync   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 20
c 3
b 0
f 0
lcom 1
cbo 7
dl 0
loc 122
ccs 0
cts 72
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCalendar() 0 4 1
A sync() 0 4 1
A getEvents() 0 12 1
B syncToTimeSlots() 0 25 5
B parseEvent() 0 19 6
B parseDate() 0 14 5
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
    public function __construct(Caldav $calendar)
46
    {
47
        $this->calendar = $calendar;
48
    }
49
50
    /**
51
     * @return Caldav
52
     */
53
    public function getCalendar()
54
    {
55
        return $this->calendar;
56
    }
57
58
    /**
59
     * perform the sync
60
     */
61
    public function sync()
62
    {
63
        $this->syncToTimeSlots();
64
    }
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
    private function getEvents()
72
    {
73
        $caldavClient = new SimpleCaldavAdapter();
74
        $caldavClient->connect($this->calendar->url, $this->calendar->username, \Crypt::decrypt($this->calendar->password));
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<plunner\Caldav>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property username does not exist on object<plunner\Caldav>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property password does not exist on object<plunner\Caldav>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
        $calendars = $caldavClient->findCalendars();
76
        $caldavClient->setCalendar($calendars[$this->calendar->calendar_name]);
0 ignored issues
show
Documentation introduced by
The property calendar_name does not exist on object<plunner\Caldav>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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
    private function syncToTimeSlots()
88
    {
89
        try
90
        {
91
            $events = $this->getEvents();
92
        }catch (\it\thecsea\caldav_client_adapter\CaldavException $e)
93
        {
94
            \Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage()));
95
        }catch(\Illuminate\Contracts\Encryption\DecryptException $e){
96
            \Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage()));
97
        }
98
99
        /**
100
         * @var $calendarMain \plunner\Calendar
101
         */
102
        $calendarMain = $this->calendar->calendar;
0 ignored issues
show
Documentation introduced by
The property calendar does not exist on object<plunner\Caldav>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
104
        //remove old timeslots
105
        $calendarMain->timeslots()->delete();
106
        foreach($events as $event){
107
            if(!($event = $this->parseEvent($event)))
108
                \Event::fire(new CaldavErrorEvent($this->calendar, 'problem during the parsing an event'));
109
            $calendarMain->timeslots()->create($event);
0 ignored issues
show
Bug introduced by
It seems like $event defined by $this->parseEvent($event) on line 107 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...
110
        }
111
    }
112
113
    /**
114
     * @param EventInterface $event
115
     * @return \DateTime[]|null
116
     */
117
    private function parseEvent(EventInterface $event)
118
    {
119
        $pattern = "/^((DTSTART;)|(DTEND;))(.*)\$/m";
120
        if(preg_match_all($pattern, $event->getData(), $matches)){
121
            if(!isset($matches[4]) || count($matches[4]) != 2)
122
                return null;
123
            $ret = [];
124
            if($tmp = $this->parseDate($matches[4][0]))
125
                $ret['time_start'] = $tmp;
126
            else
127
                return null;
128
            if($tmp = $this->parseDate($matches[4][1]))
129
                $ret['time_end'] = $tmp;
130
            else
131
                return null;
132
            return $ret;
133
        }
134
        return null;
135
    }
136
137
    /**
138
     * @param String $date
139
     * @return \DateTime|null|false
140
     */
141
    private function parseDate($date)
142
    {
143
        $pattern = "/^((TZID=)|(VALUE=))(.*):(.*)\$/m";
144
        if(preg_match_all($pattern, $date, $matches)){
145
            if($matches[1][0] == 'TZID=')
146
            {
147
                return \DateTime::createFromFormat('Ymd\THis', $matches[5][0], new \DateTimeZone($matches[4][0]));
148
            }else if($matches[1][0] == 'VALUE=' && $matches[4][0] == 'DATE')
149
            {
150
                return \DateTime::createFromFormat('Ymd\THis', $matches[5][0].'T000000');
151
            }
152
        }
153
        return null;
154
    }
155
}