Completed
Push — master ( 7f67d6...f80470 )
by Gareth
03:46
created

ContactsAPI::getContact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace garethp\ews\Contacts;
4
5
use garethp\ews\API;
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
     * @return Type\FolderIdType
17
     */
18 2
    public function getFolderId()
19
    {
20 2
        if (!$this->folderId) {
21 2
            $this->folderId = $this->getFolderByDistinguishedId('contacts')->getFolderId();
0 ignored issues
show
Documentation Bug introduced by
The method getFolderId does not exist on object<garethp\ews\API\Type>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
22 2
        }
23
24 2
        return $this->folderId;
25
    }
26
27
    /**
28
     * @param Type\FolderIdType $folderId
29
     */
30 2
    public function setFolderId($folderId)
31
    {
32 2
        $this->folderId = $folderId;
33 2
    }
34
35
    /**
36
     * @param Type\FolderIdType $folderId
37
     * @param array $options
38
     * @return Type\ContactItemType[]
39
     */
40
    public function getContacts($folderId = null, $options = array())
41
    {
42
        if (!$folderId) {
43
            $folderId = $this->getFolderId();
44
        }
45
46
        $request = array(
47
            'Traversal' => 'Shallow',
48
            'ItemShape' => array(
49
                'BaseShape' => 'AllProperties'
50
            ),
51
            'ParentFolderIds' => array(
52
                'FolderId' => $folderId->toXmlObject()
53
            )
54
        );
55
56
        $request = array_replace_recursive($request, $options);
57
58
        $request = Type::buildFromArray($request);
59
60
        return $this->getClient()->FindItem($request);
61
    }
62
63
    /**
64
     * @param Type\ItemIdType $itemId
65
     * @return Type\ContactItemType
66
     */
67 2
    public function getContact($itemId)
68
    {
69 2
        return $this->getItem($itemId);
70
    }
71
72
    /**
73
     * @param $contacts
74
     * @param array $options
75
     * @return Type\ItemIdType[]
76
     */
77 2 View Code Duplication
    public function createContacts($contacts, $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79 2
        $request = array('Contact' => $contacts);
80
81
        $defaultOptions = array(
82 2
            'MessageDisposition' => 'SaveOnly',
83 2
            'SavedItemFolderId' => array('FolderId' => $this->getFolderId()->toArray())
84 2
        );
85 2
        $options = array_replace_recursive($defaultOptions, $options);
86
87 2
        $result = $this->createItems($request, $options);
88
89 2
        if (!is_array($result)) {
90 2
            $result = array($result);
91 2
        }
92
93 2
        return $result;
94
    }
95
96 1 View Code Duplication
    public function updateContactItem(Type\ItemIdType $itemId, $changes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        //Create the request
99
        $request = array(
100
            'ItemChange' => array(
101 1
                'ItemId' => $itemId->toArray(),
102 1
                'Updates' => $this->buildUpdateItemChanges('Contact', 'contacts', $changes)
103 1
            )
104 1
        );
105
106 1
        $options = array();
107
108 1
        $items = $this->updateItems($request, $options);
109
110 1
        if (!is_array($items)) {
111 1
            $items = array($items);
112 1
        }
113
114 1
        return $items;
115
    }
116
}
117