Completed
Push — master ( 5049eb...794713 )
by Gareth
03:08
created

API::getCalendar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace garethp\ews;
4
5
use garethp\ews\API\ExchangeWebServices;
6
use garethp\ews\API\Message\EmptyFolderResponseType;
7
use garethp\ews\API\Message\GetServerTimeZonesType;
8
use garethp\ews\API\Message\SyncFolderItemsResponseMessageType;
9
use garethp\ews\API\Message\UpdateItemResponseMessageType;
10
use garethp\ews\API\Type;
11
12
/**
13
 * A base class for APIs
14
 *
15
 * Class BaseAPI
16
 * @package garethp\ews
17
 */
18
class API
19
{
20
    protected static $defaultClientOptions = array(
21
        'version' => ExchangeWebServices::VERSION_2010
22
    );
23
24 34
    public function __construct(ExchangeWebServices $client = null)
25
    {
26 34
        if ($client) {
27 34
            $this->setClient($client);
28 34
        }
29 34
    }
30
31
    /**
32
     * @return Type\EmailAddressType
33
     */
34 26
    public function getPrimarySmtpMailbox()
35
    {
36 26
        return $this->getClient()->getPrimarySmtpMailbox();
37
    }
38
39
    /**
40
     * Storing the API client
41
     * @var ExchangeWebServices
42
     */
43
    private $client;
44
45
    /**
46
     * Get a calendar item
47
     *
48
     * @param string $name
49
     * @return CalendarAPI
50
     */
51 6
    public function getCalendar($name = null)
52
    {
53 6
        $calendar = new CalendarAPI();
54 6
        $calendar->setClient($this->getClient());
55 6
        $calendar->pickCalendar($name);
56
57 6
        return $calendar;
58
    }
59
60
    /**
61
     * @param string $folderName
62
     * @return MailAPI
63
     */
64 6
    public function getMailbox($folderName = null)
65
    {
66 6
        $mailApi = new MailAPI();
67 6
        $mailApi->setClient($this->getClient());
68 6
        $mailApi->pickMailFolder($folderName);
69
70 6
        return $mailApi;
71
    }
72
73
    /**
74
     * Set the API client
75
     *
76
     * @param ExchangeWebServices $client
77
     * @return $this
78
     */
79 34
    public function setClient($client)
80
    {
81 34
        $this->client = $client;
82
83 34
        return $this;
84
    }
85
86
    /**
87
     * Get the API client
88
     *
89
     * @return ExchangeWebServices
90
     */
91 33
    public function getClient()
92
    {
93 33
        return $this->client;
94
    }
95
96 33
    public static function withUsernameAndPassword($server, $username, $password, $options = [])
97
    {
98 33
        return new static(ExchangeWebServices::fromUsernameAndPassword(
99 33
            $server,
100 33
            $username,
101 33
            $password,
102 33
            array_replace_recursive(self::$defaultClientOptions, $options)
103 33
        ));
104
    }
105
106 1
    public static function withCallbackToken($server, $token, $options = [])
107
    {
108 1
        return new static(ExchangeWebServices::fromCallbackToken(
109 1
            $server,
110 1
            $token,
111 1
            array_replace_recursive(self::$defaultClientOptions, $options)
112 1
        ));
113
    }
114
115 1
    public function getPrimarySmptEmailAddress()
116
    {
117 1
        if ($this->getPrimarySmtpMailbox() == null) {
118 1
            return null;
119
        }
120
121 1
        return $this->getPrimarySmtpMailbox()->getEmailAddress();
122
    }
123
124 1
    public function setPrimarySmtpEmailAddress($emailAddress)
125
    {
126 1
        $this->getClient()->setPrimarySmtpEmailAddress($emailAddress);
127
128 1
        return $this;
129
    }
130
131
    /**
132
     * Create items through the API client
133
     *
134
     * @param $items
135
     * @param array $options
136
     * @return Type
137
     */
138 16
    public function createItems($items, $options = array())
139
    {
140 16
        $items = Utilities\ensureIsArray($items);
141
        $request = array(
142
            'Items' => $items
143 16
        );
144
145 16
        $request = array_replace_recursive($request, $options);
146 16
        $request = Type::buildFromArray($request);
147
148 16
        $response = $this->getClient()->CreateItem($request);
149
150 16
        return $response;
151
    }
152
153 4
    public function updateItems($items, $options = array())
154
    {
155
        $request = array(
156 4
            'ItemChanges' => $items,
157 4
            'MessageDisposition' => 'SaveOnly',
158
            'ConflictResolution' => 'AlwaysOverwrite'
159 4
        );
160
161 4
        $request = array_replace_recursive($request, $options);
162
163 4
        $request = Type::buildFromArray($request);
164
165 4
        $response = $this->getClient()->UpdateItem($request);
166 4
        if ($response instanceof UpdateItemResponseMessageType) {
167 4
            return $response->getItems();
168
        }
169
170
        return Utilities\ensureIsArray($response);
171
    }
172
173 1
    public function createCalendars($names, Type\FolderIdType $parentFolder = null, $options = array())
174
    {
175 1
        if ($parentFolder == null) {
176 1
            $parentFolder = $this->getFolderByDistinguishedId('calendar')->getFolderId();
177 1
        }
178
179 1
        return $this->createFolders($names, $parentFolder, $options, 'IPF.Appointment');
180
    }
181
    
182
    public function createContactsFolder($names, Type\FolderIdType $parentFolder = null, $options = array())
183
    {
184
        if ($parentFolder == null) {
185
            $parentFolder = $this->getFolderByDistinguishedId('contacts')->getFolderId();
186
        }
187
188
        return $this->createFolders($names, $parentFolder, $options, 'IPF.Contact');
189
    }
190
191 4
    public function createFolders($names, Type\FolderIdType $parentFolder, $options = array(), $folderClass = null)
192
    {
193 4
        $names = Utilities\ensureIsArray($names);
194
        $names = array_map(function ($name) use ($folderClass) {
195 4
            return ['DisplayName' => $name, 'FolderClass' => $folderClass];
196 4
        }, $names);
197
198
        $request = [
199 4
            'Folders' => ['Folder' => $names]
200 4
        ];
201
202 4
        if (!empty($parentFolder)) {
203 4
            $request['ParentFolderId'] = array('FolderId' => $parentFolder->toArray());
204 4
        }
205
206 4
        $request = array_merge_recursive($request, $options);
207
208 4
        $this->client->CreateFolder($request);
209 4
        return true;
210
    }
211
212
    /**
213
     * @deprecated Please use API::deleteFolders() instead
214
     *
215
     * @param Type\FolderIdType $folderId
216
     * @param array $options
217
     * @return Type
218
     */
219 3
    public function deleteFolder(Type\FolderIdType $folderId, $options = array())
220
    {
221 3
        return $this->deleteFolders($folderId, $options);
222
    }
223
224 4
    public function deleteFolders($folders, $options = array())
225
    {
226 4
        $folders = Utilities\ensureIsArray($folders);
227
228
        $folderIds = array_map(function ($folderId) {
229 4
            return $folderId->toArray();
230 4
        }, $folders);
231
232
        $request = [
233 4
            'DeleteType' => 'HardDelete',
234
            'FolderIds' => array(
235
                'FolderId' => $folderIds
236 4
            )
237 4
        ];
238
239 4
        $request = array_merge_recursive($request, $options);
240 4
        return $this->client->DeleteFolder($request);
241
    }
242
243
    public function moveItem(Type\ItemIdType $itemId, Type\FolderIdType $folderId, $options = array())
244
    {
245
        $request = array(
246
            'ToFolderId' => array('FolderId' => $folderId->toArray()),
247
            'ItemIds' => array('ItemId' => $itemId->toArray())
248
        );
249
250
        $request = array_merge_recursive($request, $options);
251
252
        return $this->client->MoveItem($request);
253
    }
254
255
    /**
256
     * @param $items Type\ItemIdType|Type\ItemIdType[]
257
     * @param array $options
258
     * @return bool
259
     */
260 16
    public function deleteItems($items, $options = array())
261
    {
262 16
        $items = Utilities\ensureIsArray($items);
263
264 16
        $items = array_map(function ($item) {
265 16
            $item = Type\ItemIdType::buildFromArray($item);
266
267 16
            return $item->toArray();
268 16
        }, $items);
269
270
        $request = array(
271 16
            'ItemIds' => array('ItemId' => $items),
272
            'DeleteType' => 'MoveToDeletedItems'
273 16
        );
274
275 16
        $request = array_replace_recursive($request, $options);
276 16
        $request = Type::buildFromArray($request);
277 16
        $this->getClient()->DeleteItem($request);
278
279
        //If the delete fails, an Exception will be thrown in processResponse before it gets here
280 16
        return true;
281
    }
282
283
    /**
284
     * @param $identifier
285
     * @return Type\BaseFolderType
286
     */
287 25
    public function getFolder($identifier)
288
    {
289
        $request = array(
290
            'FolderShape' => array(
291 25
                'BaseShape' => array('_' => 'Default')
292 25
            ),
293
            'FolderIds' => $identifier
294 25
        );
295 25
        $request = Type::buildFromArray($request);
296
297 25
        $response = $this->getClient()->GetFolder($request);
298
299 25
        return $response;
300
    }
301
302
    /**
303
     * Get a folder by it's distinguishedId
304
     *
305
     * @param string $distinguishedId
306
     * @return Type\BaseFolderType
307
     */
308 25
    public function getFolderByDistinguishedId($distinguishedId)
309
    {
310 25
        return $this->getFolder(array(
311
            'DistinguishedFolderId' => array(
312 25
                'Id' => $distinguishedId,
313 25
                'Mailbox' => $this->getPrimarySmtpMailbox()
314 25
            )
315 25
        ));
316
    }
317
318
    /**
319
     * @param $folderId
320
     * @return Type\BaseFolderType
321
     */
322 4
    public function getFolderByFolderId($folderId)
323
    {
324 4
        return $this->getFolder(array(
325 4
            'FolderId' => array('Id' => $folderId, 'Mailbox' => $this->getPrimarySmtpMailbox())
326 4
        ));
327
    }
328
329
    /**
330
     * @param string|Type\FolderIdType $parentFolderId
331
     * @param array $options
332
     * @return Type\BaseFolderType[]
333
     */
334 24 View Code Duplication
    public function getChildrenFolders($parentFolderId = 'root', $options = array())
335
    {
336 24
        if (is_string($parentFolderId)) {
337 16
            $parentFolderId = $this->getFolderByDistinguishedId($parentFolderId)->getFolderId();
338 16
        }
339
340
        $request = array(
341 24
            'Traversal' => 'Shallow',
342
            'FolderShape' => array(
343
                'BaseShape' => 'AllProperties'
344 24
            ),
345
            'ParentFolderIds' => array(
346 24
                'FolderId' => $parentFolderId->toArray()
347 24
            )
348 24
        );
349
350 24
        $request = array_replace_recursive($request, $options);
351
352 24
        $request = Type::buildFromArray($request);
353
354
        /** @var \garethp\ews\API\Message\FindFolderResponseMessageType $folders */
355 24
        return $this->getClient()->FindFolder($request);
356
    }
357
358
    /**
359
     * @param string $folderName
360
     * @param string|Type\FolderIdType $parentFolderId
361
     * @param array $options
362
     * @return bool|Type\BaseFolderType
363
     */
364 24
    public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array())
365
    {
366 24
        $folders = $this->getChildrenFolders($parentFolderId, $options);
367
368 24
        foreach ($folders as $folder) {
0 ignored issues
show
Bug introduced by
The expression $folders of type object<garethp\ews\API\Type> is not traversable.
Loading history...
369 24
            if ($folder->getDisplayName() == $folderName) {
370 23
                return $folder;
371
            }
372 17
        }
373
374 4
        return false;
375
    }
376
377
    /**
378
     * @param $itemId array|Type\ItemIdType
379
     * @param array $options
380
     * @return Type
381
     */
382 5 View Code Duplication
    public function getItem($itemId, $options = array())
383
    {
384 5
        if ($itemId instanceof Type\ItemIdType) {
385 4
            $itemId = $itemId->toArray();
386 4
        }
387
388
        $request = array(
389 5
            'ItemShape' => array('BaseShape' => 'AllProperties'),
390 5
            'ItemIds' => array('ItemId' => $itemId)
391 5
        );
392
393 5
        $request = array_replace_recursive($request, $options);
394
395 5
        return $this->getClient()->GetItem($request);
396
    }
397
398
    /**
399
     * Get a list of sync changes on a folder
400
     *
401
     * @param Type\FolderIdType $folderId
402
     * @param null $syncState
403
     * @param array $options
404
     * @return SyncFolderItemsResponseMessageType
405
     */
406 2
    public function listItemChanges($folderId, $syncState = null, $options = array())
407
    {
408
        $request = array(
409 2
            'ItemShape' => array('BaseShape' => 'AllProperties'),
410 2
            'SyncFolderId' => array('FolderId' => $folderId->toXmlObject()),
411 2
            'SyncScope' => 'NormalItems',
412
            'MaxChangesReturned' => '100'
413 2
        );
414
415 2
        if ($syncState != null) {
416 1
            $request['SyncState'] = $syncState;
417 1
            $request['ItemShape']['BaseShape'] = 'AllProperties';
418 1
        }
419
420 2
        $request = array_replace_recursive($request, $options);
421
422 2
        $request = Type::buildFromArray($request);
423 2
        $response = $this->getClient()->SyncFolderItems($request);
424
425 2
        return $response;
426
    }
427
428
    public function getServerTimezones($timezoneIDs = array(), $fullTimezoneData = false)
429
    {
430
        $request = GetServerTimeZonesType::buildFromArray(array(
431
            'returnFullTimeZoneData' => $fullTimezoneData
432
        ));
433
434
        if (!empty($timezoneIDs)) {
435
            $request->setIds($timezoneIDs);
436
        }
437
438
        $timezones = $this->getClient()->GetServerTimeZones($request);
439
        $timezones = $timezones->TimeZoneDefinition;
0 ignored issues
show
Documentation introduced by
The property TimeZoneDefinition does not exist on object<garethp\ews\API\Type>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
440
441
        return Utilities\ensureIsArray($timezones);
442
    }
443
444
    /**
445
     * @param Type\ItemIdType $itemId
446
     * @param $fromType
447
     * @param $destinationType
448
     * @param $mailbox
449
     *
450
     * @return Type\ItemIdType
451
     */
452
    public function convertIdFormat(Type\ItemIdType $itemId, $fromType, $destinationType, $mailbox)
453
    {
454
        $result = $this->getClient()->ConvertId(array(
455
            'DestinationFormat' => $destinationType,
456
            'SourceIds' => array(
457
                'AlternateId' => array(
458
                    'Format' => $fromType,
459
                    'Id' => $itemId->getId(),
460
                    'Mailbox' => $mailbox
461
                )
462
            )
463
        ));
464
465
        $itemId->setId($result->getId());
466
467
        return $itemId;
468
    }
469
470
    /**
471
     * @param Type\FindItemParentType|Type\FindFolderParentType $result
472
     *
473
     * @return Type\FindItemParentType|Type\FindFolderParentType
474
     */
475 2
    public function getNextPage($result)
476
    {
477 2
        if ($result->isIncludesLastItemInRange()) {
478
            return $result;
479
        }
480
481 2
        $currentPage = $result->getCurrentPage();
482 2
        $currentPage->setOffset($result->getIndexedPagingOffset());
483
484 2
        $lastRequest = $result->getLastRequest();
485 2
        $lastRequest->setIndexedPage($currentPage);
486
487 2
        if ($result instanceof Type\FindFolderParentType) {
488 1
            return $this->getClient()->FindFolder($lastRequest);
489
        }
490
491 1
        return $this->getClient()->FindItem($lastRequest);
492
    }
493
494
    /**
495
     * @param Type\FolderIdType $folderId
496
     * @param string $deleteType
497
     * @param bool $deleteSubFolders
498
     * @param array $options
499
     * @return EmptyFolderResponseType
500
     */
501
    public function emptyFolder(
502
        Type\FolderIdType $folderId,
503
        $deleteType = 'SoftDelete',
504
        $deleteSubFolders = false,
505
        array $options = []
506
    ) {
507
        $request = [
508
            'DeleteType' => $deleteType,
509
            'DeleteSubFolders' => $deleteSubFolders,
510
            'FolderIds' => ['FolderId' => $folderId->toArray()]
511
        ];
512
513
        $request = array_merge_recursive($request, $options);
514
515
        return $this->getClient()->EmptyFolder($request);
516
    }
517
}
518