ContactsAPI   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 96%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 41
dl 0
loc 127
ccs 48
cts 50
cp 0.96
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 createContacts() 0 17 2
A getFolderId() 0 8 2
A pickContactsFolder() 0 13 4
A updateContactItem() 0 17 2
A getContacts() 0 19 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 8
    public function pickContactsFolder($displayName = null)
23
    {
24 8
        if ($displayName === 'default.contacts' || $displayName === null) {
25 8
            $this->folderId = $this->getFolderByDistinguishedId('contacts')->getFolderId();
26
        } else {
27
            $this->folderId = $this->getFolderByDisplayName($displayName, 'contacts')->getFolderId();
28
        }
29
30 8
        if ($this->folderId === null) {
31
            throw new Exception('Folder does not exist');
32
        }
33
34 8
        return $this;
35
    }
36
37
    /**
38
     * @return Type\BaseFolderIdType
39
     */
40 8
    public function getFolderId()
41
    {
42 8
        if (!$this->folderId) {
43 8
            $this->pickContactsFolder();
44
            //            $this->folderId = $this->getFolderByDistinguishedId('contacts')->getFolderId();
45
        }
46
47 8
        return $this->folderId;
48
    }
49
50
    /**
51
     * @param Type\BaseFolderIdType $folderId
52
     */
53 8
    public function setFolderId($folderId)
54
    {
55 8
        $this->folderId = $folderId;
56
    }
57
58
    /**
59
     * @param Type\BaseFolderIdType $folderId
60
     * @param array $options
61
     * @return Type\ContactItemType[]
62
     */
63 3
    public function getContacts($folderId = null, $options = array())
64
    {
65 3
        if (!$folderId) {
66 3
            $folderId = $this->getFolderId();
67
        }
68
69 3
        $request = array(
70 3
            'Traversal' => 'Shallow',
71 3
            'ItemShape' => array(
72 3
                'BaseShape' => 'AllProperties'
73 3
            ),
74 3
            'ParentFolderIds' => $folderId->toArray(true)
75 3
        );
76
77 3
        $request = array_replace_recursive($request, $options);
78
79 3
        $request = Type::buildFromArray($request);
80
81 3
        return $this->getClient()->FindItem($request);
82
    }
83
84
    /**
85
     * @param Type\ItemIdType $itemId
86
     * @param array $options
87
     * @return Type\ContactItemType
88
     */
89 5
    public function getContact($itemId, $options = array())
90
    {
91 5
        return $this->getItem($itemId, $options);
92
    }
93
94
    /**
95
     * @param $contacts
96
     * @param array $options
97
     * @return Type\ItemIdType[]
98
     */
99 8
    public function createContacts($contacts, $options = array())
100
    {
101 8
        $request = array('Contact' => $contacts);
102
103 8
        $defaultOptions = array(
104 8
            'MessageDisposition' => 'SaveOnly',
105 8
            'SavedItemFolderId' => $this->getFolderId()->toArray(true)
106 8
        );
107 8
        $options = array_replace_recursive($defaultOptions, $options);
108
109 8
        $result = $this->createItems($request, $options);
110
111 8
        if (!is_array($result)) {
112 6
            $result = array($result);
113
        }
114
115 8
        return $result;
116
    }
117
118 2
    public function updateContactItem(Type\ItemIdType $itemId, $changes, $options = [])
119
    {
120
        //Create the request
121 2
        $request = array(
122 2
            'ItemChange' => array(
123 2
                'ItemId' => $itemId->toArray(),
124 2
                'Updates' => API\ItemUpdateBuilder::buildUpdateItemChanges('Contact', 'contacts', $changes)
125 2
            )
126 2
        );
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