Completed
Push — master ( 7d0c17...bf74ca )
by Gareth
10:56 queued 08:03
created

ContactsAPI   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 56.59 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 83.02%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 4
dl 73
loc 129
ccs 44
cts 53
cp 0.8302
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A pickContact() 13 13 4
A getFolderId() 0 8 2
A setFolderId() 0 4 1
A getContacts() 22 22 2
A getContact() 0 4 1
A createContacts() 18 18 2
A updateContactItem() 20 20 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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