MailAPI::setFolderId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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');
23
        }
24
25 7
        return $this->folderId;
26
    }
27
28
    /**
29
     * @param Type\BaseFolderIdType $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\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');
44 1
            return;
45
        }
46
47 7
        $folder = $this->getFolderByDisplayName($displayName, $parentFolder);
48 7
        $this->folderId = $folder->getFolderId();
49
    }
50
51 1
    protected function formatRestrictions($restrictions)
52
    {
53 1
        return RestrictionFormatter::format('message', $restrictions);
54
    }
55
56
    /**
57
     * Get all mail items in the inbox
58
     *
59
     * @param Type\BaseFolderIdType $folderId
60
     * @param array $options
61
     * @return Type\MessageType[]
62
     */
63 7
    public function getMailItems($folderId = null, $options = array())
64
    {
65 7
        if (!$folderId) {
66 7
            $folderId = $this->getFolderId();
67
        }
68
69 7
        $request = array(
70 7
            'Traversal' => 'Shallow',
71 7
            'ItemShape' => array(
72 7
                'BaseShape' => 'AllProperties'
73 7
            ),
74 7
            'ParentFolderIds' => $folderId->toArray(true)
75 7
        );
76
77 7
        if (!empty($options['Restriction'])) {
78 1
            $options['Restriction'] = $this->formatRestrictions($options['Restriction']);
79
        }
80
81 7
        $request = array_replace_recursive($request, $options);
82
83 7
        $request = Type::buildFromArray($request);
84 7
        return $this->getClient()->FindItem($request);
85
    }
86
87
    /**
88
     * @param Type\BaseFolderIdType $folderId
89
     * @param array $options
90
     * @return Type\MessageType[]
91
     */
92 1
    public function getUnreadMailItems($folderId = null, $options = array())
93
    {
94 1
        $unReadOption = array(
95 1
            'Restriction' => array(
96 1
                'IsEqualTo' => array(
97 1
                    'IsRead' => false
98 1
                )
99 1
            )
100 1
        );
101
102 1
        $options = array_replace_recursive($unReadOption, $options);
103
104 1
        return $this->getMailItems($folderId, $options);
105
    }
106
107
    /**
108
     * Updates a calendar item with changes
109
     *
110
     * @param $itemId Type\ItemIdType|Type
111
     * @param $changes
112
     * @param array $options
113
     * @return Type\MessageType[]
114
     */
115 1
    public function updateMailItem($itemId, $changes, $options = [])
116
    {
117
        //Create the request
118 1
        $request = array(
119 1
            'ItemChange' => array(
120 1
                'ItemId' => $itemId->toArray(),
121 1
                'Updates' => API\ItemUpdateBuilder::buildUpdateItemChanges('Message', 'message', $changes)
122 1
            )
123 1
        );
124
125 1
        $items = $this->updateItems($request, $options);
126
127 1
        if (!is_array($items)) {
128 1
            $items = array($items);
129
        }
130
131 1
        return $items;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $items returns an array which contains values of type garethp\ews\API\Type\ArrayOfRealItemsType which are incompatible with the documented value type garethp\ews\API\Type\MessageType.
Loading history...
132
    }
133
134
    /**
135
     * @param $mailItem Type\MessageType|Type\ItemIdType
136
     * @param $isRead boolean
137
     */
138 1
    public function markMailAsRead($mailItem, $isRead = true)
139
    {
140 1
        if ($mailItem instanceof Type\MessageType) {
141 1
            $mailItem = $mailItem->getItemId();
142
        }
143
144 1
        $this->updateMailItem($mailItem, array(
145 1
            'IsRead' => $isRead
146 1
        ));
147
    }
148
149 3
    public function sendMail(MessageType $message, $options = array())
150
    {
151 3
        $items = array('Message' => $message->toXmlObject());
152 3
        $defaultOptions = array(
153 3
            'MessageDisposition' => 'SendAndSaveCopy',
154 3
        );
155
156 3
        if ($this->getPrimarySmtpMailbox() != null) {
157
            $sentItems = $this->getDistinguishedFolderId('sentitems');
158
            $defaultOptions['SavedItemFolderId'] = $sentItems->toArray(true);
159
        }
160
161 3
        $options = array_replace_recursive($defaultOptions, $options);
162
163 3
        return $this->createItems($items, $options);
164
    }
165
166
    public function getAttachment(Type\AttachmentIdType $attachmentId, $options = [])
167
    {
168
        $request = array(
169
            'AttachmentIds' => array(
170
                $attachmentId->toXmlObject()
171
            ),
172
            'AttachmentShape' => array(
173
                'IncludeMimeContent' => true
174
            )
175
        );
176
177
        $request = array_replace_recursive($request, $options);
178
179
        return $this->getClient()->GetAttachment($request);
180
    }
181
182
    /**
183
     * @param array $options
184
     * @return API\Message\EmptyFolderResponseType
185
     */
186
    public function emptyTrash(array $options = [])
187
    {
188
        return $this->emptyFolder(
189
            $this->getDistinguishedFolderId(DistinguishedFolderIdNameType::DELETEDITEMS),
190
            'SoftDelete',
191
            false,
192
            $options
193
        );
194
    }
195
}
196