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

MailAPI::getMailItems()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

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