Completed
Push — master ( 0a0629...7fa454 )
by Gareth
06:34
created

CalendarAPI::acceptMeeting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 4
crap 2
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
     * @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 2
        }
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 3
            )
87 3
        );
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 2
        }
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 6
        }
113
114 6
        if (!($end instanceof DateTime)) {
115 6
            $end = new DateTime($end);
116 6
        }
117
118
        $request = array(
119 6
            'Traversal' => 'Shallow',
120
            'ItemShape' => array(
121
                'BaseShape' => 'AllProperties'
122 6
            ),
123
            'CalendarView' => array(
124 6
                'MaxEntriesReturned' => 100,
125 6
                'StartDate' => $start->format('c'),
126 6
                'EndDate' => $end->format('c')
127 6
            ),
128
            'ParentFolderIds' => array(
129 6
                'FolderId' => $this->getFolderId()->toXmlObject()
130 6
            )
131 6
        );
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
    public function updateCalendarItem(Type\ItemIdType $itemId, $changes)
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 1
                )
168 1
            )
169 1
        );
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 1
        }
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<jamesiarmes\PEWS\API\Type> is not traversable.
Loading history...
204 2
            $this->deleteCalendarItem($item->getItemId());
205 6
        }
206 6
    }
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
    public function acceptMeeting($itemId, $message, $sensitivity = 'Private', $options = array())
229
    {
230
        $acceptItem = array(
231
            'Sensitivity' => $sensitivity,
232
            'Body' => $message,
233
            'ReferenceItemId' => $itemId->toArray()
234
        );
235
236
        $acceptItem = array('AcceptItem' => array(array_replace_recursive($acceptItem, $options)));
237
        return $this->createItems($acceptItem);
238
    }
239
}
240