Completed
Push — master ( 9a8ecb...d328d0 )
by Gareth
02:37
created

ContactsAPI::pickContactsFolder()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 5
Ratio 35.71 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 5
loc 14
ccs 6
cts 8
cp 0.75
crap 4.25
rs 9.2
c 0
b 0
f 0
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 View Code Duplication
        if ($displayName === 'default.contacts' || $displayName === null) {
25 5
            $this->folderId = $this->getDistinguishedFolderId('contacts');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getDistinguishedFolderId('contacts') of type object<garethp\ews\API\T...tinguishedFolderIdType> is incompatible with the declared type object<garethp\ews\API\Type\BaseFolderIdType> of property $folderId.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26 5
        } 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 5
        }
45
46 5
        return $this->folderId;
47
    }
48
49
    /**
50
     * @param Type\BaseFolderIdType $folderId
51
     */
52 5
    public function setFolderId($folderId)
53
    {
54 5
        $this->folderId = $folderId;
55 5
    }
56
57
    /**
58
     * @param Type\BaseFolderIdType $folderId
59
     * @param array $options
60
     * @return Type\ContactItemType[]
61
     */
62 2 View Code Duplication
    public function getContacts($folderId = null, $options = array())
63
    {
64 2
        if (!$folderId) {
65 2
            $folderId = $this->getFolderId();
66 2
        }
67
68
        $request = array(
69 2
            'Traversal' => 'Shallow',
70
            'ItemShape' => array(
71
                'BaseShape' => 'AllProperties'
72 2
            ),
73 2
            'ParentFolderIds' => $folderId->toArray(true)
74 2
        );
75
76 2
        $request = array_replace_recursive($request, $options);
77
78 2
        $request = Type::buildFromArray($request);
79
80 2
        return $this->getClient()->FindItem($request);
81
    }
82
83
    /**
84
     * @param Type\ItemIdType $itemId
85
     * @return Type\ContactItemType
86
     */
87 3
    public function getContact($itemId)
88
    {
89 3
        return $this->getItem($itemId);
90
    }
91
92
    /**
93
     * @param $contacts
94
     * @param array $options
95
     * @return Type\ItemIdType[]
96
     */
97 5
    public function createContacts($contacts, $options = array())
98
    {
99 5
        $request = array('Contact' => $contacts);
100
101
        $defaultOptions = array(
102 5
            'MessageDisposition' => 'SaveOnly',
103 5
            'SavedItemFolderId' => $this->getFolderId()->toArray(true)
104 5
        );
105 5
        $options = array_replace_recursive($defaultOptions, $options);
106
107 5
        $result = $this->createItems($request, $options);
108
109 5
        if (!is_array($result)) {
110 4
            $result = array($result);
111 4
        }
112
113 5
        return $result;
114
    }
115
116 2 View Code Duplication
    public function updateContactItem(Type\ItemIdType $itemId, $changes)
117
    {
118
        //Create the request
119
        $request = array(
120
            'ItemChange' => array(
121 2
                'ItemId' => $itemId->toArray(),
122 2
                'Updates' => API\ItemUpdateBuilder::buildUpdateItemChanges('Contact', 'contacts', $changes)
123 2
            )
124 2
        );
125
126 2
        $options = array();
127
128 2
        $items = $this->updateItems($request, $options);
129
130 2
        if (!is_array($items)) {
131 2
            $items = array($items);
132 2
        }
133
134 2
        return $items;
135
    }
136
}
137