Completed
Push — master ( 8909a0...61f6fc )
by Gareth
03:29
created

CalendarAPI::deleteCalendarItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace jamesiarmes\PEWS\Calendar;
4
5
use jamesiarmes\PEWS\API\Type\CalendarItemType;
6
use jamesiarmes\PEWS\API\Type;
7
use jamesiarmes\PEWS\API;
8
use jamesiarmes\PEWS\API\Enumeration;
9
use DateTime;
10
11
/**
12
 * An API end point for Calendar items
13
 *
14
 * Class API
15
 * @package jamesiarmes\PEWS\Calendar
16
 */
17
class CalendarAPI extends API
18
{
19
    /**
20
     * @var Type\FolderIdType
21
     */
22
    protected $folderId;
23
24
    /**
25
     * Pick a Calendar based on it's name
26
     *
27
     * @param string|null $displayName
28
     * @return $this
29
     */
30 6
    public function pickCalendar($displayName = null)
31
    {
32 6
        if ($displayName == 'default.calendar' || $displayName == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $displayName of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
33 1
            $folder = $this->getFolderByDistinguishedId('calendar');
34 1
        } else {
35 6
            $folder = $this->getFolderByDisplayName($displayName, 'calendar');
36
        }
37
38 6
        $this->folderId = $folder->getFolderId();
0 ignored issues
show
Documentation Bug introduced by
The method getFolderId does not exist on object<jamesiarmes\PEWS\API\Type>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
39
40 6
        return $this;
41
    }
42
43
    /**
44
     * @return Type\FolderIdType
45
     */
46 6
    public function getFolderId()
47
    {
48 6
        if ($this->folderId === null) {
49
            $this->pickCalendar();
50
        }
51
52 6
        return $this->folderId;
53
    }
54
55
    /**
56
     * Create one or more calendar items
57
     *
58
     * @param $items CalendarItemType[]|CalendarItemType|Array or more calendar items to create
59
     * @param $options array Options to merge in to the request
60
     * @return Type\ItemIdType[]
61
     */
62 3
    public function createCalendarItems($items, $options = array())
63
    {
64
        //If the item passed in is an object, or if it's an associative]
65
        // array waiting to be an object, let's put it in to an array
66 3
        if (!is_array($items) || Type::arrayIsAssoc($items)) {
67 2
            $items = array($items);
68 2
        }
69
70 3
        $item = array('CalendarItem' => $items);
71
        $defaultOptions = array(
72 3
            'SendMeetingInvitations' => Enumeration\CalendarItemCreateOrDeleteOperationType::SEND_TO_NONE,
73
            'SavedItemFolderId' => array(
74 3
                'FolderId' => $this->getFolderId()->toXmlObject()
75 3
            )
76 3
        );
77
78 3
        $options = array_replace_recursive($defaultOptions, $options);
79
80 3
        $items = $this->createItems($item, $options);
81
82 3
        if (!is_array($items)) {
83 2
            $items = array($items);
84 2
        }
85
86 3
        return $items;
87
    }
88
89
    /**
90
     * Get a list of calendar items between two dates/times
91
     *
92
     * @param string|DateTime $start
93
     * @param string|DateTime $end
94
     * @param array $options
95
     * @return CalendarItemType[]|Type\FindItemParentType
96
     */
97 6
    public function getCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array())
98
    {
99 6
        if (!($start instanceof DateTime)) {
100 6
            $start = new DateTime($start);
101 6
        }
102
103 6
        if (!($end instanceof DateTime)) {
104 6
            $end = new DateTime($end);
105 6
        }
106
107
        $request = array(
108 6
            'Traversal' => 'Shallow',
109
            'ItemShape' => array(
110
                'BaseShape' => 'AllProperties'
111 6
            ),
112
            'CalendarView' => array(
113 6
                'MaxEntriesReturned' => 100,
114 6
                'StartDate' => $start->format('c'),
115 6
                'EndDate' => $end->format('c')
116 6
            ),
117 6
            'ParentFolderIds' => array(
118 6
                'FolderId' => $this->getFolderId()->toXmlObject()
119 6
            )
120 6
        );
121
122 6
        $request = array_replace_recursive($request, $options);
123
124 6
        $request = Type::buildFromArray($request);
125 6
        $response = $this->getClient()->FindItem($request);
126 6
        $items = $response;
127
128 6
        return $items;
129
    }
130
131
    /**
132
     * @param $id
133
     * @param $changeKey
134
     * @return Type\CalendarItemType
135
     */
136 1
    public function getCalendarItem($id, $changeKey)
137
    {
138 1
        return $this->getItem(['Id' => $id, 'ChangeKey' => $changeKey]);
139
    }
140
141
    /**
142
     * Updates a calendar item with changes
143
     *
144
     * @param $itemId Type\ItemIdType
145
     * @param $changes
146
     * @return Type\CalendarItemType[]
147
     */
148 1
    public function updateCalendarItem(Type\ItemIdType $itemId, $changes)
149
    {
150
        //Create the request
151
        $request = array(
152
            'ItemChange' => array(
153 1
                'ItemId' => $itemId->toArray(),
154
                'Updates' => array(
155 1
                    'SetItemField' => $this->buildUpdateItemChanges('CalendarItem', 'calendar', $changes)
156 1
                )
157 1
            )
158 1
        );
159
160
        $options = array(
161
            'SendMeetingInvitationsOrCancellations' => 'SendToNone'
162 1
        );
163
164 1
        $items = $this->updateItems($request, $options)->getCalendarItem();
165
166 1
        if (!is_array($items)) {
167 1
            $items = array($items);
168 1
        }
169
170 1
        return $items;
171
    }
172
173
    /**
174
     * @param $itemId Type\ItemIdType
175
     * @return bool
176
     */
177 3
    public function deleteCalendarItem(Type\ItemIdType $itemId)
178
    {
179 3
        return $this->deleteItems($itemId, array(
180
            'SendMeetingCancellations' => 'SendToNone'
181 3
        ));
182
    }
183
184
    /**
185
     * @param string $start
186
     * @param string $end
187
     * @param array $options
188
     */
189 6
    public function deleteAllCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array())
190
    {
191 6
        $items = $this->getCalendarItems($start, $end, $options);
192 6
        foreach ($items as $item) {
0 ignored issues
show
Bug introduced by
The expression $items of type object<jamesiarmes\PEWS\API\Type> is not traversable.
Loading history...
193 2
            $this->deleteCalendarItem($item->getItemId());
194 6
        }
195 6
    }
196
197
    /**
198
     * Get a list of changes on the calendar items
199
     *
200
     * @param null $syncState
201
     * @param array $options
202
     * @return mixed
203
     */
204 1
    public function listChanges($syncState = null, $options = array())
205
    {
206 1
        return parent::listItemChanges($this->getFolderId(), $syncState, $options);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (listItemChanges() instead of listChanges()). Are you sure this is correct? If so, you might want to change this to $this->listItemChanges().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
207
    }
208
}
209