Completed
Push — master ( d328d0...51fcf8 )
by Gareth
03:07
created

MailAPI::formatRestrictions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 19
ccs 15
cts 15
cp 1
crap 4
rs 9.6333
c 0
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
     * @param array $options
128
     * @return Type\MessageType[]
129
     */
130 1 View Code Duplication
    public function updateMailItem($itemId, $changes, $options = [])
131
    {
132
        //Create the request
133
        $request = array(
134
            'ItemChange' => array(
135 1
                'ItemId' => $itemId->toArray(),
136 1
                'Updates' => API\ItemUpdateBuilder::buildUpdateItemChanges('Message', 'message', $changes)
137 1
            )
138 1
        );
139
140 1
        $items = $this->updateItems($request, $options);
141
142 1
        if (!is_array($items)) {
143 1
            $items = array($items);
144 1
        }
145
146 1
        return $items;
147
    }
148
149
    /**
150
     * @param $mailItem Type\MessageType|Type\ItemIdType
151
     * @param $isRead boolean
152
     */
153 1
    public function markMailAsRead($mailItem, $isRead = true)
154
    {
155 1
        if ($mailItem instanceof Type\MessageType) {
156 1
            $mailItem = $mailItem->getItemId();
157 1
        }
158
159 1
        $this->updateMailItem($mailItem, array(
160
            'IsRead' => $isRead
161 1
        ));
162 1
    }
163
164 3
    public function sendMail(MessageType $message, $options = array())
165
    {
166 3
        $items = array('Message' => $message->toXmlObject());
167
        $defaultOptions = array(
168 3
            'MessageDisposition' => 'SendAndSaveCopy',
169 3
        );
170
171 3
        if ($this->getPrimarySmtpMailbox() != null) {
172
            $sentItems = $this->getDistinguishedFolderId('sentitems');
173
            $defaultOptions['SavedItemFolderId'] = $sentItems->toArray(true);
174
        }
175
176 3
        $options = array_replace_recursive($defaultOptions, $options);
177
178 3
        return $this->createItems($items, $options);
179
    }
180
181 View Code Duplication
    public function getAttachment(Type\AttachmentIdType $attachmentId, $options = [])
182
    {
183
        $request = array(
184
            'AttachmentIds' => array(
185
                $attachmentId->toXmlObject()
186
            ),
187
            'AttachmentShape' => array(
188
                'IncludeMimeContent' => true
189
            )
190
        );
191
192
        $request = array_replace_recursive($request, $options);
193
194
        $attachment = $this->getClient()->GetAttachment($request);
195
        return $attachment;
196
    }
197
198
    /**
199
     * @param array $options
200
     * @return API\Message\EmptyFolderResponseType
201
     */
202
    public function emptyTrash(array $options = [])
203
    {
204
        return $this->emptyFolder(
205
            $this->getDistinguishedFolderId(DistinguishedFolderIdNameType::DELETEDITEMS),
206
            'SoftDelete',
207
            false,
208
            $options
209
        );
210
    }
211
}
212