Completed
Branch master (09022f)
by Gareth
05:56 queued 03:06
created

MailAPI   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 189
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 87.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 2
cbo 3
dl 18
loc 189
ccs 55
cts 63
cp 0.873
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getFolderId() 0 8 2
A setFolderId() 0 4 1
A pickMailFolder() 0 10 2
A formatRestrictions() 0 19 4
B getMailItems() 0 25 3
A getUnreadMailItems() 0 14 1
A updateMailItem() 18 18 2
A markMailAsRead() 0 10 2
A sendMail() 0 17 2
A getAttachment() 0 14 1

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\Mail;
4
5
use garethp\ews\API;
6
use garethp\ews\API\Type;
7
use garethp\ews\API\Type\MessageType;
8
9
class MailAPI extends API
10
{
11
    /**
12
     * @var Type\FolderIdType
13
     */
14
    protected $folderId;
15
16
    /**
17
     * @return Type\FolderIdType
18
     */
19 6
    public function getFolderId()
20
    {
21 6
        if (!$this->folderId) {
22 1
            $this->folderId = $this->getFolderByDistinguishedId('inbox')->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...
23
        }
24
25 6
        return $this->folderId;
26
    }
27
28
    /**
29
     * @param Type\FolderIdType $folderId
30
     */
31 1
    public function setFolderId($folderId)
32
    {
33 1
        $this->folderId = $folderId;
34
    }
35
36
    /**
37
     * @param string $displayName
38
     * @param string|Type\FolderIdType $parentFolder
39
     */
40 6
    public function pickMailFolder($displayName = null, $parentFolder = 'inbox')
41
    {
42 6
        if ($displayName === null) {
43 1
            $this->folderId = $this->getFolderByDistinguishedId('inbox')->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...
44 1
            return;
45
        }
46
47 6
        $folder = $this->getFolderByDisplayName($displayName, $parentFolder);
48 6
        $this->folderId = $folder->getFolderId();
49
    }
50
51 1
    protected function formatRestrictions($restrictions)
52
    {
53 1
        foreach ($restrictions as $restrictionType => $query) {
54 1
            $formattedRestrictionType = array();
55 1
            foreach ($query as $key => $value) {
56 1
                if ($value === false) {
57 1
                    $value = 'false';
58
                }
59 1
                $formattedRestrictionType[] = array(
60 1
                    'FieldURI' => array('FieldURI' => $this->getFieldUriByName($key, 'message')),
61 1
                    'FieldURIOrConstant' => array('Constant' => array('Value' => (string) $value))
62
                );
63
            }
64
65 1
            $restrictions[$restrictionType] = $formattedRestrictionType;
66
        }
67
68 1
        return $restrictions;
69
    }
70
71
    /**
72
     * Get all mail items in the inbox
73
     *
74
     * @param Type\FolderIdType
75
     * @param array $options
76
     * @return Type\MessageType[]
77
     */
78 6
    public function getMailItems($folderId = null, $options = array())
79
    {
80 6
        if (!$folderId) {
81 6
            $folderId = $this->getFolderId();
82
        }
83
84
        $request = array(
85 6
            'Traversal' => 'Shallow',
86
            'ItemShape' => array(
87
                'BaseShape' => 'AllProperties'
88
            ),
89
            'ParentFolderIds' => array(
90 6
                'FolderId' => $folderId->toXmlObject()
91
            )
92
        );
93
94 6
        if (!empty($options['Restriction'])) {
95 1
            $options['Restriction'] = $this->formatRestrictions($options['Restriction']);
96
        }
97
98 6
        $request = array_replace_recursive($request, $options);
99
100 6
        $request = Type::buildFromArray($request);
101 6
        return $this->getClient()->FindItem($request);
102
    }
103
104
    /**
105
     * @param Type\FolderIdType $folderId
106
     * @param array $options
107
     * @return Type\MessageType[]
108
     */
109 1
    public function getUnreadMailItems($folderId = null, $options = array())
110
    {
111
        $unReadOption = array(
112
            'Restriction' => array(
113
                'IsEqualTo' => array(
114
                    'IsRead' => false
115
                )
116
            )
117 1
        );
118
119 1
        $options = array_replace_recursive($unReadOption, $options);
120
121 1
        return $this->getMailItems($folderId, $options);
122
    }
123
124
    /**
125
     * Updates a calendar item with changes
126
     *
127
     * @param $itemId Type\ItemIdType|Type
128
     * @param $changes
129
     * @return Type\MessageType[]
130
     */
131 1 View Code Duplication
    public function updateMailItem($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...
132
    {
133
        //Create the request
134
        $request = array(
135
            'ItemChange' => array(
136 1
                'ItemId' => $itemId->toArray(),
137 1
                'Updates' => $this->buildUpdateItemChanges('Message', 'message', $changes)
138
            )
139
        );
140
141 1
        $items = $this->updateItems($request);
142
143 1
        if (!is_array($items)) {
144 1
            $items = array($items);
145
        }
146
147 1
        return $items;
148
    }
149
150
    /**
151
     * @param $mailItem Type\MessageType|Type\ItemIdType
152
     * @param $isRead boolean
153
     */
154 1
    public function markMailAsRead($mailItem, $isRead = true)
155
    {
156 1
        if ($mailItem instanceof Type\MessageType) {
0 ignored issues
show
Bug introduced by
The class garethp\ews\API\Type\MessageType does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
157 1
            $mailItem = $mailItem->getItemId();
158
        }
159
160 1
        $this->updateMailItem($mailItem, array(
161 1
            'IsRead' => $isRead
162
        ));
163
    }
164
165 3
    public function sendMail(MessageType $message, $options = array())
166
    {
167 3
        $items = array('Message' => $message->toXmlObject());
168
        $defaultOptions = array(
169 3
            'MessageDisposition' => 'SendAndSaveCopy',
170
        );
171
172 3
        if ($this->getPrimarySmtpMailbox() != null) {
173
            $sentItems = $this->getFolderByDistinguishedId('sentitems')->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...
174
            $defaultOptions['SavedItemFolderId'] =
175
                array('FolderId' => $sentItems->toXmlObject());
176
        }
177
178 3
        $options = array_replace_recursive($defaultOptions, $options);
179
180 3
        return $this->createItems($items, $options);
181
    }
182
183
    public function getAttachment(Type\AttachmentIdType $attachmentId)
184
    {
185
        $request = array (
186
            'AttachmentIds' => array(
187
                $attachmentId->toXmlObject()
188
            ),
189
            'AttachmentShape' => array(
190
                'IncludeMimeContent' => true
191
            )
192
        );
193
194
        $attachment = $this->getClient()->GetAttachment($request);
0 ignored issues
show
Documentation Bug introduced by
The method GetAttachment does not exist on object<garethp\ews\API\ExchangeWebServices>? 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...
195
        return $attachment;
196
    }
197
}
198