Completed
Branch master (09022f)
by Gareth
05:56 queued 03:06
created

CalendarAPI   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 258
Duplicated Lines 24.03 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 69.23%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 1
cbo 3
dl 62
loc 258
ccs 54
cts 78
cp 0.6923
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setFolderId() 0 6 1
A getCalendarItem() 0 4 1
A pickCalendar() 0 12 3
A getFolderId() 0 8 2
B createCalendarItems() 0 26 4
B getCalendarItems() 0 33 3
A deleteCalendarItem() 0 6 1
A listChanges() 0 4 1
A updateCalendarItem() 22 22 2
A deleteAllCalendarItems() 0 7 2
A acceptMeeting() 20 20 2
A declineMeeting() 20 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 1
                'Updates' => $this->buildUpdateItemChanges('CalendarItem', 'calendar', $changes)
166
            )
167
        );
168
169
        $options = array(
170
            'SendMeetingInvitationsOrCancellations' => 'SendToNone'
171 1
        );
172
173 1
        $items = $this->updateItems($request, $options)->getCalendarItem();
174
175 1
        if (!is_array($items)) {
176 1
            $items = array($items);
177
        }
178
179 1
        return $items;
180
    }
181
182
    /**
183
     * @param $itemId Type\ItemIdType
184
     * @return bool
185
     */
186 3
    public function deleteCalendarItem(Type\ItemIdType $itemId)
187
    {
188 3
        return $this->deleteItems($itemId, array(
189
            'SendMeetingCancellations' => 'SendToNone'
190 3
        ));
191
    }
192
193
    /**
194
     * @param string $start
195
     * @param string $end
196
     * @param array $options
197
     */
198 6
    public function deleteAllCalendarItems($start = '12:00 AM', $end = '11:59 PM', $options = array())
199
    {
200 6
        $items = $this->getCalendarItems($start, $end, $options);
201 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...
202 6
            $this->deleteCalendarItem($item->getItemId());
203
        }
204
    }
205
206
    /**
207
     * Get a list of changes on the calendar items
208
     *
209
     * @param null $syncState
210
     * @param array $options
211
     * @return API\Message\SyncFolderItemsResponseMessageType
212
     */
213 1
    public function listChanges($syncState = null, $options = array())
214
    {
215 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...
216
    }
217
218
    /**
219
     * @param Type\ItemIdType $itemId
220
     * @param string $message
221
     * @param string $sensitivity
222
     * @param array $options
223
     *
224
     * @return Type\ItemIdType[]
225
     */
226 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...
227
    {
228
        $request = array(
229
            'AcceptItem' => array(
230
                'Sensitivity' => $sensitivity,
231
                'Body' => array('BodyType' => 'HTML', '_value' => $message),
232
                'ReferenceItemId' => $itemId->toArray()
233
            )
234
        );
235
236
        $defaultOptions = array('MessageDisposition' => 'SendOnly');
237
        $options = array_replace_recursive($defaultOptions, $options);
238
239
        $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...
240
        if (!is_array($request)) {
241
            $return = array($return);
242
        }
243
244
        return $return;
245
    }
246
247
    /**
248
     * @param $itemId
249
     * @param $message
250
     * @param string $sensitivity
251
     * @param array $options
252
     * @return Type\ItemIdType[]
253
     */
254 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...
255
    {
256
        $request = array(
257
            'DeclineItem' => array(
258
                'Sensitivity' => $sensitivity,
259
                'Body' => array('BodyType' => 'HTML', '_value' => $message),
260
                'ReferenceItemId' => $itemId->toArray()
261
            )
262
        );
263
264
        $defaultOptions = array('MessageDisposition' => 'SendOnly');
265
        $options = array_replace_recursive($defaultOptions, $options);
266
267
        $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...
268
        if (!is_array($request)) {
269
            $return = array($return);
270
        }
271
272
        return $return;
273
    }
274
}
275