Completed
Push — master ( d7c914...6c6572 )
by Gareth
11:08 queued 07:24
created

CalendarAPI::updateCalendarItem()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2
Metric Value
dl 24
loc 24
ccs 8
cts 8
cp 1
rs 8.9713
cc 2
eloc 12
nc 2
nop 2
crap 2
1
<?php
2
3
namespace garethp\ews\Calendar;
4
5
use garethp\ews\API\Type\CalendarItemType;
6
use garethp\ews\API\Type;
7
use garethp\ews\API;
8
use garethp\ews\API\Enumeration;
9
use DateTime;
10
11
/**
12
 * An API end point for Calendar items
13
 *
14
 * Class API
15
 * @package garethp\ews\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
        } 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<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...
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
     * @param Type\FolderIdType $folderId
57
     * @return $this
58
     */
59
    public function setFolderId($folderId)
60
    {
61
        $this->folderId = $folderId;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Create one or more calendar items
68
     *
69
     * @param $items CalendarItemType[]|CalendarItemType|array or more calendar items to create
70
     * @param $options array Options to merge in to the request
71
     * @return Type\ItemIdType[]
72
     */
73 3
    public function createCalendarItems($items, $options = array())
74
    {
75
        //If the item passed in is an object, or if it's an associative]
76
        // array waiting to be an object, let's put it in to an array
77 3
        if (!is_array($items) || Type::arrayIsAssoc($items)) {
78 2
            $items = array($items);
79
        }
80
81 3
        $item = array('CalendarItem' => $items);
82
        $defaultOptions = array(
83 3
            'SendMeetingInvitations' => Enumeration\CalendarItemCreateOrDeleteOperationType::SEND_TO_NONE,
84
            'SavedItemFolderId' => array(
85 3
                'FolderId' => $this->getFolderId()->toXmlObject()
86
            )
87
        );
88
89 3
        $options = array_replace_recursive($defaultOptions, $options);
90
91 3
        $items = $this->createItems($item, $options);
92
93 3
        if (!is_array($items)) {
94 2
            $items = array($items);
95
        }
96
97 3
        return $items;
98
    }
99
100
    /**
101
     * Get a list of calendar items between two dates/times
102
     *
103
     * @param string|DateTime $start
104
     * @param string|DateTime $end
105
     * @param array $options
106
     * @return CalendarItemType[]|Type\FindItemParentType
107
     */
108 6
    public function getCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array())
109
    {
110 6
        if (!($start instanceof DateTime)) {
111 6
            $start = new DateTime($start);
112
        }
113
114 6
        if (!($end instanceof DateTime)) {
115 6
            $end = new DateTime($end);
116
        }
117
118
        $request = array(
119 6
            'Traversal' => 'Shallow',
120
            'ItemShape' => array(
121
                'BaseShape' => 'AllProperties'
122
            ),
123
            'CalendarView' => array(
124 6
                'MaxEntriesReturned' => 100,
125 6
                'StartDate' => $start->format('c'),
126 6
                'EndDate' => $end->format('c')
127
            ),
128
            'ParentFolderIds' => array(
129 6
                'FolderId' => $this->getFolderId()->toXmlObject()
130
            )
131
        );
132
133 6
        $request = array_replace_recursive($request, $options);
134
135 6
        $request = Type::buildFromArray($request);
136 6
        $response = $this->getClient()->FindItem($request);
137 6
        $items = $response;
138
139 6
        return $items;
140
    }
141
142
    /**
143
     * @param $id
144
     * @param $changeKey
145
     * @return Type\CalendarItemType
146
     */
147 1
    public function getCalendarItem($id, $changeKey)
148
    {
149 1
        return $this->getItem(['Id' => $id, 'ChangeKey' => $changeKey]);
150
    }
151
152
    /**
153
     * Updates a calendar item with changes
154
     *
155
     * @param $itemId Type\ItemIdType
156
     * @param $changes
157
     * @return Type\CalendarItemType[]
158
     */
159 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...
160
    {
161
        //Create the request
162
        $request = array(
163
            'ItemChange' => array(
164 1
                'ItemId' => $itemId->toArray(),
165
                'Updates' => array(
166 1
                    'SetItemField' => $this->buildUpdateItemChanges('CalendarItem', 'calendar', $changes)
167
                )
168
            )
169
        );
170
171
        $options = array(
172
            'SendMeetingInvitationsOrCancellations' => 'SendToNone'
173 1
        );
174
175 1
        $items = $this->updateItems($request, $options)->getCalendarItem();
176
177 1
        if (!is_array($items)) {
178 1
            $items = array($items);
179
        }
180
181 1
        return $items;
182
    }
183
184
    /**
185
     * @param $itemId Type\ItemIdType
186
     * @return bool
187
     */
188 3
    public function deleteCalendarItem(Type\ItemIdType $itemId)
189
    {
190 3
        return $this->deleteItems($itemId, array(
191
            'SendMeetingCancellations' => 'SendToNone'
192 3
        ));
193
    }
194
195
    /**
196
     * @param string $start
197
     * @param string $end
198
     * @param array $options
199
     */
200 6
    public function deleteAllCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array())
201
    {
202 6
        $items = $this->getCalendarItems($start, $end, $options);
203 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...
204 6
            $this->deleteCalendarItem($item->getItemId());
205
        }
206
    }
207
208
    /**
209
     * Get a list of changes on the calendar items
210
     *
211
     * @param null $syncState
212
     * @param array $options
213
     * @return API\Message\SyncFolderItemsResponseMessageType
214
     */
215 1
    public function listChanges($syncState = null, $options = array())
216
    {
217 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...
218
    }
219
220
    /**
221
     * @param Type\ItemIdType $itemId
222
     * @param string $message
223
     * @param string $sensitivity
224
     * @param array $options
225
     *
226
     * @return Type\ItemIdType[]
227
     */
228 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...
229
    {
230
        $request = array(
231
            'AcceptItem' => array(
232
                'Sensitivity' => $sensitivity,
233
                'Body' => array('BodyType' => 'HTML', '_value' => $message),
234
                'ReferenceItemId' => $itemId->toArray()
235
            )
236
        );
237
238
        $defaultOptions = array('MessageDisposition' => 'SendOnly');
239
        $options = array_replace_recursive($defaultOptions, $options);
240
241
        $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...
242
        if (!is_array($request)) {
243
            $return = array($return);
244
        }
245
246
        return $return;
247
    }
248
249
    /**
250
     * @param $itemId
251
     * @param $message
252
     * @param string $sensitivity
253
     * @param array $options
254
     * @return Type\ItemIdType[]
255
     */
256 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...
257
    {
258
        $request = array(
259
            'DeclineItem' => array(
260
                'Sensitivity' => $sensitivity,
261
                'Body' => array('BodyType' => 'HTML', '_value' => $message),
262
                'ReferenceItemId' => $itemId->toArray()
263
            )
264
        );
265
266
        $defaultOptions = array('MessageDisposition' => 'SendOnly');
267
        $options = array_replace_recursive($defaultOptions, $options);
268
269
        $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...
270
        if (!is_array($request)) {
271
            $return = array($return);
272
        }
273
274
        return $return;
275
    }
276
}
277