Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
97 / 97
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
Office365
100.00% covered (success)
100.00%
97 / 97
100.00% covered (success)
100.00%
6 / 6
30
100.00% covered (success)
100.00%
1 / 1
 getMessages
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
1 / 1
20
 getMessage
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 getAttachments
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 getAttachment
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 markAsRead
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 markAsUnread
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Mail\Client;
15
16use Pop\Http;
17use Pop\Mail\Api\AbstractOffice365;
18
19/**
20 * Office 365 mail client class
21 *
22 * @category   Pop
23 * @package    Pop\Mail
24 * @author     Nick Sagona, III <dev@nolainteractive.com>
25 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
26 * @license    http://www.popphp.org/license     New BSD License
27 * @version    4.0.0
28 */
29class Office365 extends AbstractOffice365 implements HttpClientInterface
30{
31
32    /**
33     * Get messages
34     *
35     * @param  string $folder
36     * @param  array  $search
37     * @param  int    $limit
38     * @throws Exception|Http\Exception|Http\Client\Exception|Http\Client\Handler\Exception
39     * @return mixed
40     */
41    public function getMessages(string $folder = 'Inbox', array $search = [], int $limit = 10): mixed
42    {
43        if ($this->client === null) {
44            throw new Exception('Error: The client object has not been instantiated yet.');
45        }
46
47        $this->verifyToken();
48
49        $data = [];
50
51        if (!empty($limit)) {
52            $data['$top'] = $limit;
53        }
54
55        if (!empty($search)) {
56            $filterStrings = [];
57            foreach ($search as $key => $value) {
58                $op         = 'eq';
59                $startsWith = false;
60                $endsWith   = false;
61                if (str_ends_with($key, '%')) {
62                    $startsWith = true;
63                    $key        = substr($key, 0, -1);
64                } else if (str_starts_with($key, '%')) {
65                    $endsWith = true;
66                    $key      = substr($key, 1);
67                } else {
68                    if (str_ends_with($key, '!=')) {
69                        $op  = 'ne';
70                        $key = substr($key, 0, -2);
71                    } else if (str_ends_with($key, '>')) {
72                        $op  = 'gt';
73                        $key = substr($key, 0, -1);
74                    } else if (str_ends_with($key, '>=')) {
75                        $op  = 'ge';
76                        $key = substr($key, 0, -2);
77                    } else if (str_ends_with($key, '<')) {
78                        $op  = 'lt';
79                        $key = substr($key, 0, -1);
80                    } else if (str_ends_with($key, '<=')) {
81                        $op  = 'le';
82                        $key = substr($key, 0, -2);
83                    }
84                }
85
86                switch (strtolower($key)) {
87                    case 'unread':
88                        $filterStrings[] = "isRead " . $op . " " . ($value) ? "false" : "true";
89                        break;
90                    case 'sent':
91                        $filterStrings[] = "sentDateTime " . $op . " " . date('c', strtotime($value));
92                        break;
93                    default:
94                        if ($startsWith) {
95                            $filterStrings[] = "startsWith(" . $key . ", '" . $value . "')";
96                        } else if ($endsWith) {
97                            $filterStrings[] = "endsWith(" . $key . ", '" . $value . "')";
98                        } else {
99                            $filterStrings[] = $key . " " . $op . " '" . $value . "'";
100                        }
101                }
102            }
103
104            if (!empty($filterStrings)) {
105                $data['filter'] = implode(' and ', $filterStrings);
106            }
107        }
108
109        $this->client->setAuth(Http\Auth::createBearer($this->token));
110        $this->client->addOption('method', 'GET');
111        $this->client->addOption('type', Http\Client\Request::URLFORM);
112        $this->client->addOption('auto', true);
113
114        $uri = "/" . $this->accountId . "/mailfolders('" . $folder . "')/messages";
115        if (!empty($data)){
116            $uri .= '?' . rawurldecode(http_build_query($data, "\n"));
117        }
118
119        return $this->client->send($uri);
120    }
121
122    /**
123     * Get messages
124     *
125     * @param  string $messageId
126     * @param  bool   $raw
127     * @throws Exception|Http\Exception|Http\Client\Exception|Http\Client\Handler\Exception
128     * @return mixed
129     */
130    public function getMessage(string $messageId, bool $raw = false): mixed
131    {
132        if ($this->client === null) {
133            throw new Exception('Error: The client object has not been instantiated yet.');
134        }
135
136        $this->verifyToken();
137
138        $this->client->setAuth(Http\Auth::createBearer($this->token));
139        $this->client->addOption('method', 'GET');
140        $this->client->addOption('type', Http\Client\Request::URLFORM);
141        $this->client->addOption('auto', true);
142
143        $uri = "/" . $this->accountId . "/messages/" . $messageId;
144        if ($raw) {
145            $uri .= '/$value';
146        }
147
148        return $this->client->send($uri);
149    }
150
151    /**
152     * Get message attachments
153     *
154     * @param  string $messageId
155     * @param  string $folder
156     * @throws Exception|Http\Exception|Http\Client\Exception|Http\Client\Handler\Exception
157     * @return mixed
158     */
159    public function getAttachments(string $messageId, string $folder = 'Inbox'): mixed
160    {
161        if ($this->client === null) {
162            throw new Exception('Error: The client object has not been instantiated yet.');
163        }
164
165        $this->verifyToken();
166
167        $this->client->setAuth(Http\Auth::createBearer($this->token));
168        $this->client->addOption('method', 'GET');
169        $this->client->addOption('type', Http\Client\Request::URLFORM);
170        $this->client->addOption('auto', true);
171
172        return $this->client->send(
173            "/" . $this->accountId . "/mailfolders('" . $folder . "')/messages/" . $messageId . "/attachments"
174        );
175    }
176
177    /**
178     * Get message attachment
179     *
180     * @param  string $messageId
181     * @param  string $attachmentId
182     * @param  string $folder
183     * @throws Exception|Http\Exception|Http\Client\Exception|Http\Client\Handler\Exception
184     * @return mixed
185     */
186    public function getAttachment(string $messageId, string $attachmentId, string $folder = 'Inbox'): mixed
187    {
188        if ($this->client === null) {
189            throw new Exception('Error: The client object has not been instantiated yet.');
190        }
191
192        $this->verifyToken();
193
194        $this->client->setAuth(Http\Auth::createBearer($this->token));
195        $this->client->addOption('method', 'GET');
196        $this->client->addOption('type', Http\Client\Request::URLFORM);
197        $this->client->addOption('auto', true);
198
199        return $this->client->send(
200            "/" . $this->accountId . "/mailfolders('" . $folder . "')/messages/" . $messageId . "/attachments/" . $attachmentId
201        );
202    }
203
204    /**
205     * Mark message as read
206     *
207     * @param  string $messageId
208     * @param  bool   $isRead
209     * @throws Exception|Http\Exception|Http\Client\Exception|Http\Client\Handler\Exception
210     * @return Office365
211     */
212    public function markAsRead(string $messageId, bool $isRead = true): Office365
213    {
214        if ($this->client === null) {
215            throw new Exception('Error: The client object has not been instantiated yet.');
216        }
217
218        $this->verifyToken();
219
220        $this->client->setAuth(Http\Auth::createBearer($this->token));
221        $this->client->addOption('type', Http\Client\Request::JSON);
222        $this->client->addOption('auto', true);
223        $this->client->addOption('method', 'PATCH');
224        $this->client->setData(['isRead' => $isRead]);
225
226        $this->client->send("/" . $this->accountId . "/messages/" . $messageId);
227
228        return $this;
229    }
230
231    /**
232     * Mark message as unread
233     *
234     * @param  string $messageId
235     * @throws Exception|Http\Exception|Http\Client\Exception|Http\Client\Handler\Exception
236     * @return Office365
237     */
238    public function markAsUnread(string $messageId): Office365
239    {
240        return $this->markAsRead($messageId, false);
241    }
242
243}