Completed
Push — master ( 649e1f...bdce94 )
by Gareth
04:10
created

CalendarAPI::deleteCalendarItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace garethp\ews;
4
5
use garethp\ews\API\Enumeration\DisposalType;
6
use garethp\ews\API\Type\CalendarItemType;
7
use garethp\ews\API\Type;
8
use garethp\ews\API;
9
use garethp\ews\API\Enumeration;
10
use DateTime;
11
12
/**
13
 * An API end point for Calendar items
14
 *
15
 * Class API
16
 * @package garethp\ews\Calendar
17
 */
18
class CalendarAPI extends API
19
{
20
    /**
21
     * @var Type\FolderIdType
22
     */
23
    protected $folderId;
24
25
    /**
26
     * Pick a Calendar based on it's name
27
     *
28
     * @param string|null $displayName
29
     * @return $this
30
     */
31 6
    public function pickCalendar($displayName = null)
32
    {
33 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...
34 1
            $folder = $this->getFolderByDistinguishedId('calendar');
35 1
        } else {
36 6
            $folder = $this->getFolderByDisplayName($displayName, 'calendar');
37
        }
38
39 6
        $this->folderId = $folder->getFolderId();
0 ignored issues
show
Documentation Bug introduced by
The method getFolderId does not exist on object<garethp\ews\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...
40
41 6
        return $this;
42
    }
43
44
    /**
45
     * @return Type\FolderIdType
46
     */
47 6
    public function getFolderId()
48
    {
49 6
        if ($this->folderId === null) {
50
            $this->pickCalendar();
51
        }
52
53 6
        return $this->folderId;
54
    }
55
56
    /**
57
     * @param Type\FolderIdType $folderId
58
     * @return $this
59
     */
60
    public function setFolderId($folderId)
61
    {
62
        $this->folderId = $folderId;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Create one or more calendar items
69
     *
70
     * @param $items CalendarItemType[]|CalendarItemType|array or more calendar items to create
71
     * @param $options array Options to merge in to the request
72
     * @return Type\ItemIdType[]
73
     */
74 3
    public function createCalendarItems($items, $options = array())
75
    {
76
        //If the item passed in is an object, or if it's an associative]
77
        // array waiting to be an object, let's put it in to an array
78 3
        if (!is_array($items) || Type::arrayIsAssoc($items)) {
79 2
            $items = array($items);
80 2
        }
81
82 3
        $item = array('CalendarItem' => $items);
83
        $defaultOptions = array(
84 3
            'SendMeetingInvitations' => Enumeration\CalendarItemCreateOrDeleteOperationType::SEND_TO_NONE,
85
            'SavedItemFolderId' => array(
86 3
                'FolderId' => $this->getFolderId()->toXmlObject()
87 3
            )
88 3
        );
89
90 3
        $options = array_replace_recursive($defaultOptions, $options);
91
92 3
        $items = $this->createItems($item, $options);
93
94 3
        if (!is_array($items)) {
95 2
            $items = array($items);
96 2
        }
97
98 3
        return $items;
99
    }
100
101
    /**
102
     * Get a list of calendar items between two dates/times
103
     *
104
     * @param string|DateTime $start
105
     * @param string|DateTime $end
106
     * @param array $options
107
     * @return CalendarItemType[]|Type\FindItemParentType
108
     */
109 6
    public function getCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array())
110
    {
111 6
        if (!($start instanceof DateTime)) {
112 6
            $start = new DateTime($start);
113 6
        }
114
115 6
        if (!($end instanceof DateTime)) {
116 6
            $end = new DateTime($end);
117 6
        }
118
119
        $request = array(
120 6
            'Traversal' => 'Shallow',
121
            'ItemShape' => array(
122
                'BaseShape' => 'AllProperties'
123 6
            ),
124
            'CalendarView' => array(
125 6
                'MaxEntriesReturned' => 100,
126 6
                'StartDate' => $start->format('c'),
127 6
                'EndDate' => $end->format('c')
128 6
            ),
129
            'ParentFolderIds' => array(
130 6
                'FolderId' => $this->getFolderId()->toXmlObject()
131 6
            )
132 6
        );
133
134 6
        $request = array_replace_recursive($request, $options);
135
136 6
        $request = Type::buildFromArray($request);
137 6
        $response = $this->getClient()->FindItem($request);
138 6
        $items = $response;
139
140 6
        return $items;
141
    }
142
143
    /**
144
     * @param $id
145
     * @param $changeKey
146
     * @return Type\CalendarItemType
147
     */
148 1
    public function getCalendarItem($id, $changeKey)
149
    {
150 1
        return $this->getItem(['Id' => $id, 'ChangeKey' => $changeKey]);
151
    }
152
153
    /**
154
     * Updates a calendar item with changes
155
     *
156
     * @param $itemId Type\ItemIdType
157
     * @param $changes
158
     * @return Type\CalendarItemType[]
159
     */
160 1 View Code Duplication
    public function updateCalendarItem(Type\ItemIdType $itemId, $changes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        //Create the request
163
        $request = array(
164
            'ItemChange' => array(
165 1
                'ItemId' => $itemId->toArray(),
166 1
                'Updates' => $this->buildUpdateItemChanges('CalendarItem', 'calendar', $changes)
167 1
            )
168 1
        );
169
170
        $options = array(
171
            'SendMeetingInvitationsOrCancellations' => 'SendToNone'
172 1
        );
173
174 1
        $items = $this->updateItems($request, $options)->getCalendarItem();
175
176 1
        if (!is_array($items)) {
177 1
            $items = array($items);
178 1
        }
179
180 1
        return $items;
181
    }
182
183
    /**
184
     * @param Type\ItemIdType $itemId
185
     * @param array  $options
186
     * @return bool
187
     */
188 3
    public function deleteCalendarItem(Type\ItemIdType $itemId, $options = array())
189
    {
190
        $defaultOptions = array(
191
            'SendMeetingCancellations' => 'SendToNone'
192 3
        );
193
194 3
        $options = array_replace_recursive($defaultOptions, $options);
195 3
        return $this->deleteItems($itemId, $options);
196
    }
197
198
    /**
199
     * @param string $start
200
     * @param string $end
201
     * @param array $options
202
     */
203 6
    public function deleteAllCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array())
204
    {
205 6
        $items = $this->getCalendarItems($start, $end, $options);
206 6
        foreach ($items as $item) {
0 ignored issues
show
Bug introduced by
The expression $items of type object<garethp\ews\API\Type> is not traversable.
Loading history...
207 2
            $this->deleteCalendarItem($item->getItemId());
208 6
        }
209 6
    }
210
211
    /**
212
     * Get a list of changes on the calendar items
213
     *
214
     * @param null $syncState
215
     * @param array $options
216
     * @return API\Message\SyncFolderItemsResponseMessageType
217
     */
218 1
    public function listChanges($syncState = null, $options = array())
219
    {
220 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...
221
    }
222
223
    /**
224
     * @param Type\ItemIdType $itemId
225
     * @param string $message
226
     * @param string $sensitivity
227
     * @param array $options
228
     *
229
     * @return Type\ItemIdType[]
230
     */
231 View Code Duplication
    public function acceptMeeting($itemId, $message, $sensitivity = 'Private', $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
    {
233
        $request = array(
234
            'AcceptItem' => array(
235
                'Sensitivity' => $sensitivity,
236
                'Body' => array('BodyType' => 'HTML', '_value' => $message),
237
                'ReferenceItemId' => $itemId->toArray()
238
            )
239
        );
240
241
        $defaultOptions = array('MessageDisposition' => 'SendOnly');
242
        $options = array_replace_recursive($defaultOptions, $options);
243
244
        $return = $this->createItems($request, $options)->getCalendarItem();
0 ignored issues
show
Documentation Bug introduced by
The method getCalendarItem does not exist on object<garethp\ews\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...
245
        if (!is_array($request)) {
246
            $return = array($return);
247
        }
248
249
        return $return;
250
    }
251
252
    /**
253
     * @param $itemId
254
     * @param $message
255
     * @param string $sensitivity
256
     * @param array $options
257
     * @return Type\ItemIdType[]
258
     */
259 View Code Duplication
    public function declineMeeting($itemId, $message, $sensitivity = 'Private', $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
260
    {
261
        $request = array(
262
            'DeclineItem' => array(
263
                'Sensitivity' => $sensitivity,
264
                'Body' => array('BodyType' => 'HTML', '_value' => $message),
265
                'ReferenceItemId' => $itemId->toArray()
266
            )
267
        );
268
269
        $defaultOptions = array('MessageDisposition' => 'SendOnly');
270
        $options = array_replace_recursive($defaultOptions, $options);
271
272
        $return = $this->createItems($request, $options)->getCalendarItem();
0 ignored issues
show
Documentation Bug introduced by
The method getCalendarItem does not exist on object<garethp\ews\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...
273
        if (!is_array($request)) {
274
            $return = array($return);
275
        }
276
277
        return $return;
278
    }
279
}
280