Completed
Push — master ( bf74ca...ea24b8 )
by Gareth
10:36 queued 07:33
created

ContactsAPI::pickContactsFolder()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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