Completed
Push — master ( 14422e...17eb68 )
by Gareth
02:59
created

MailAPI::updateMailItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 18
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
namespace garethp\ews;
4
5
use garethp\ews\API;
6
use garethp\ews\API\Enumeration\DistinguishedFolderIdNameType;
7
use garethp\ews\API\Type;
8
use garethp\ews\API\Type\MessageType;
9
10
class MailAPI extends API
11
{
12
    /**
13
     * @var Type\FolderIdType
14
     */
15
    protected $folderId;
16
17
    /**
18
     * @return Type\FolderIdType
19
     */
20 6
    public function getFolderId()
21
    {
22 6
        if (!$this->folderId) {
23 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...
24 1
        }
25
26 6
        return $this->folderId;
27
    }
28
29
    /**
30
     * @param Type\FolderIdType $folderId
31
     */
32 1
    public function setFolderId($folderId)
33
    {
34 1
        $this->folderId = $folderId;
35 1
    }
36
37
    /**
38
     * @param string $displayName
39
     * @param string|Type\FolderIdType $parentFolder
40
     */
41 6
    public function pickMailFolder($displayName = null, $parentFolder = 'inbox')
42
    {
43 6
        if ($displayName === null) {
44 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...
45 1
            return;
46
        }
47
48 6
        $folder = $this->getFolderByDisplayName($displayName, $parentFolder);
49 6
        $this->folderId = $folder->getFolderId();
50 6
    }
51
52 1
    protected function formatRestrictions($restrictions)
53
    {
54 1
        foreach ($restrictions as $restrictionType => $query) {
55 1
            $formattedRestrictionType = array();
56 1
            foreach ($query as $key => $value) {
57 1
                if ($value === false) {
58 1
                    $value = 'false';
59 1
                }
60 1
                $formattedRestrictionType[] = array(
61 1
                    'FieldURI' => array('FieldURI' => API\FieldURIManager::getFieldUriByName($key, 'message')),
62 1
                    'FieldURIOrConstant' => array('Constant' => array('Value' => (string) $value))
63 1
                );
64 1
            }
65
66 1
            $restrictions[$restrictionType] = $formattedRestrictionType;
67 1
        }
68
69 1
        return $restrictions;
70
    }
71
72
    /**
73
     * Get all mail items in the inbox
74
     *
75
     * @param Type\FolderIdType
76
     * @param array $options
77
     * @return Type\MessageType[]
78
     */
79 6
    public function getMailItems($folderId = null, $options = array())
80
    {
81 6
        if (!$folderId) {
82 6
            $folderId = $this->getFolderId();
83 6
        }
84
85
        $request = array(
86 6
            'Traversal' => 'Shallow',
87
            'ItemShape' => array(
88
                'BaseShape' => 'AllProperties'
89 6
            ),
90
            'ParentFolderIds' => array(
91 6
                'FolderId' => $folderId->toXmlObject()
92 6
            )
93 6
        );
94
95 6
        if (!empty($options['Restriction'])) {
96 1
            $options['Restriction'] = $this->formatRestrictions($options['Restriction']);
97 1
        }
98
99 6
        $request = array_replace_recursive($request, $options);
100
101 6
        $request = Type::buildFromArray($request);
102 6
        return $this->getClient()->FindItem($request);
103
    }
104
105
    /**
106
     * @param Type\FolderIdType $folderId
107
     * @param array $options
108
     * @return Type\MessageType[]
109
     */
110 1
    public function getUnreadMailItems($folderId = null, $options = array())
111
    {
112
        $unReadOption = array(
113
            'Restriction' => array(
114
                'IsEqualTo' => array(
115
                    'IsRead' => false
116 1
                )
117 1
            )
118 1
        );
119
120 1
        $options = array_replace_recursive($unReadOption, $options);
121
122 1
        return $this->getMailItems($folderId, $options);
123
    }
124
125
    /**
126
     * Updates a calendar item with changes
127
     *
128
     * @param $itemId Type\ItemIdType|Type
129
     * @param $changes
130
     * @return Type\MessageType[]
131
     */
132 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...
133
    {
134
        //Create the request
135
        $request = array(
136
            'ItemChange' => array(
137 1
                'ItemId' => $itemId->toArray(),
138 1
                'Updates' => API\ItemUpdateBuilder::buildUpdateItemChanges('Message', 'message', $changes)
139 1
            )
140 1
        );
141
142 1
        $items = $this->updateItems($request);
143
144 1
        if (!is_array($items)) {
145 1
            $items = array($items);
146 1
        }
147
148 1
        return $items;
149
    }
150
151
    /**
152
     * @param $mailItem Type\MessageType|Type\ItemIdType
153
     * @param $isRead boolean
154
     */
155 1
    public function markMailAsRead($mailItem, $isRead = true)
156
    {
157 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...
158 1
            $mailItem = $mailItem->getItemId();
159 1
        }
160
161 1
        $this->updateMailItem($mailItem, array(
162
            'IsRead' => $isRead
163 1
        ));
164 1
    }
165
166 3
    public function sendMail(MessageType $message, $options = array())
167
    {
168 3
        $items = array('Message' => $message->toXmlObject());
169
        $defaultOptions = array(
170 3
            'MessageDisposition' => 'SendAndSaveCopy',
171 3
        );
172
173 3
        if ($this->getPrimarySmtpMailbox() != null) {
174
            $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...
175
            $defaultOptions['SavedItemFolderId'] =
176
                array('FolderId' => $sentItems->toXmlObject());
177
        }
178
179 3
        $options = array_replace_recursive($defaultOptions, $options);
180
181 3
        return $this->createItems($items, $options);
182
    }
183
184
    public function getAttachment(Type\AttachmentIdType $attachmentId)
185
    {
186
        $request = array(
187
            'AttachmentIds' => array(
188
                $attachmentId->toXmlObject()
189
            ),
190
            'AttachmentShape' => array(
191
                'IncludeMimeContent' => true
192
            )
193
        );
194
195
        $attachment = $this->getClient()->GetAttachment($request);
196
        return $attachment;
197
    }
198
199
    /**
200
     * @param array $options
201
     * @return API\Message\EmptyFolderResponseType
202
     */
203
    public function emptyTrash(array $options = [])
204
    {
205
        return $this->emptyFolder(
206
            $this->getFolderByDistinguishedId(DistinguishedFolderIdNameType::DELETEDITEMS)->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...
207
            'SoftDelete',
208
            false,
209
            $options
210
        );
211
    }
212
}
213