Passed
Push — master ( 3c4313...0524cd )
by Gareth
08:27
created

ContactsAPI   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 41
dl 0
loc 127
ccs 38
cts 40
cp 0.95
rs 10
c 4
b 0
f 1
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setFolderId() 0 3 1
A getContact() 0 3 1
A updateContactItem() 0 17 2
A getContacts() 0 19 2
A getFolderId() 0 8 2
A pickContactsFolder() 0 13 4
A createContacts() 0 17 2
1
<?php
2
3
namespace garethp\ews;
4
5
use garethp\ews\API\Exception;
6
use garethp\ews\API\Type;
7
8
class ContactsAPI extends API
9
{
10
    /**
11
     * @var Type\BaseFolderIdType
12
     */
13
    protected $folderId;
14
15
    /**
16
     * Pick a Contacts based on it's folder name
17
     *
18
     * @param string|null $displayName
19
     * @return $this
20
     * @throws Exception
21
     */
22 5
    public function pickContactsFolder($displayName = null)
23
    {
24 5
        if ($displayName === 'default.contacts' || $displayName === null) {
25 5
            $this->folderId = $this->getFolderByDistinguishedId('contacts')->getFolderId();
26
        } else {
27
            $this->folderId = $this->getFolderByDisplayName($displayName, 'contacts')->getFolderId();
28
        }
29
30 5
        if ($this->folderId === null) {
31
            throw new Exception('Folder does not exist');
32
        }
33
34 5
        return $this;
35
    }
36
37
    /**
38
     * @return Type\BaseFolderIdType
39
     */
40 5
    public function getFolderId()
41
    {
42 5
        if (!$this->folderId) {
43 5
            $this->pickContactsFolder();
44
            //            $this->folderId = $this->getFolderByDistinguishedId('contacts')->getFolderId();
45
        }
46
47 5
        return $this->folderId;
48
    }
49
50
    /**
51
     * @param Type\BaseFolderIdType $folderId
52
     */
53 5
    public function setFolderId($folderId)
54
    {
55 5
        $this->folderId = $folderId;
56 5
    }
57
58
    /**
59
     * @param Type\BaseFolderIdType $folderId
60
     * @param array $options
61
     * @return Type\ContactItemType[]
62
     */
63 2
    public function getContacts($folderId = null, $options = array())
64
    {
65 2
        if (!$folderId) {
66 2
            $folderId = $this->getFolderId();
67
        }
68
69
        $request = array(
70 2
            'Traversal' => 'Shallow',
71
            'ItemShape' => array(
72
                'BaseShape' => 'AllProperties'
73
            ),
74 2
            'ParentFolderIds' => $folderId->toArray(true)
75
        );
76
77 2
        $request = array_replace_recursive($request, $options);
78
79 2
        $request = Type::buildFromArray($request);
80
81 2
        return $this->getClient()->FindItem($request);
82
    }
83
84
    /**
85
     * @param Type\ItemIdType $itemId
86
     * @param array $options
87
     * @return Type\ContactItemType
88
     */
89 3
    public function getContact($itemId, $options = array())
90
    {
91 3
        return $this->getItem($itemId, $options);
92
    }
93
94
    /**
95
     * @param $contacts
96
     * @param array $options
97
     * @return Type\ItemIdType[]
98
     */
99 5
    public function createContacts($contacts, $options = array())
100
    {
101 5
        $request = array('Contact' => $contacts);
102
103
        $defaultOptions = array(
104 5
            'MessageDisposition' => 'SaveOnly',
105 5
            'SavedItemFolderId' => $this->getFolderId()->toArray(true)
106
        );
107 5
        $options = array_replace_recursive($defaultOptions, $options);
108
109 5
        $result = $this->createItems($request, $options);
110
111 5
        if (!is_array($result)) {
112 4
            $result = array($result);
113
        }
114
115 5
        return $result;
116
    }
117
118 2
    public function updateContactItem(Type\ItemIdType $itemId, $changes, $options = [])
119
    {
120
        //Create the request
121
        $request = array(
122
            'ItemChange' => array(
123 2
                'ItemId' => $itemId->toArray(),
124 2
                'Updates' => API\ItemUpdateBuilder::buildUpdateItemChanges('Contact', 'contacts', $changes)
125
            )
126
        );
127
128 2
        $items = $this->updateItems($request, $options);
129
130 2
        if (!is_array($items)) {
131 2
            $items = array($items);
132
        }
133
134 2
        return $items;
135
    }
136
}
137