Passed
Push — master ( 6990ab...b50988 )
by Malte
03:01
created

Client::getFolderPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
* File:     Client.php
4
* Category: -
5
* Author:   M. Goldenbaum
6
* Created:  19.01.17 22:21
7
* Updated:  -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\IMAP;
14
15
use Webklex\IMAP\Exceptions\ConnectionFailedException;
16
use Webklex\IMAP\Exceptions\GetMessagesFailedException;
17
use Webklex\IMAP\Exceptions\InvalidImapTimeoutTypeException;
18
use Webklex\IMAP\Exceptions\MailboxFetchingException;
19
use Webklex\IMAP\Exceptions\MaskNotFoundException;
20
use Webklex\IMAP\Exceptions\MessageSearchValidationException;
21
use Webklex\IMAP\Support\FolderCollection;
22
use Webklex\IMAP\Support\Masks\AttachmentMask;
23
use Webklex\IMAP\Support\Masks\MessageMask;
24
use Webklex\IMAP\Support\MessageCollection;
25
26
/**
27
 * Class Client
28
 *
29
 * @package Webklex\IMAP
30
 */
31
class Client {
32
33
    /**
34
     * @var boolean|resource
35
     */
36
    public $connection = false;
37
38
    /**
39
     * Server hostname.
40
     *
41
     * @var string
42
     */
43
    public $host;
44
45
    /**
46
     * Server port.
47
     *
48
     * @var int
49
     */
50
    public $port;
51
52
    /**
53
     * Service protocol.
54
     *
55
     * @var int
56
     */
57
    public $protocol;
58
59
    /**
60
     * Server encryption.
61
     * Supported: none, ssl or tls.
62
     *
63
     * @var string
64
     */
65
    public $encryption;
66
67
    /**
68
     * If server has to validate cert.
69
     *
70
     * @var mixed
71
     */
72
    public $validate_cert;
73
74
    /**
75
     * Account username/
76
     *
77
     * @var mixed
78
     */
79
    public $username;
80
81
    /**
82
     * Account password.
83
     *
84
     * @var string
85
     */
86
    public $password;
87
88
    /**
89
     * Read only parameter.
90
     *
91
     * @var bool
92
     */
93
    protected $read_only = false;
94
95
    /**
96
     * Active folder.
97
     *
98
     * @var Folder
99
     */
100
    protected $active_folder = false;
101
102
    /**
103
     * Connected parameter
104
     *
105
     * @var bool
106
     */
107
    protected $connected = false;
108
109
    /**
110
     * IMAP errors that might have ben occurred
111
     *
112
     * @var array $errors
113
     */
114
    protected $errors = [];
115
116
    /**
117
     * All valid and available account config parameters
118
     *
119
     * @var array $validConfigKeys
120
     */
121
    protected $valid_config_keys = ['host', 'port', 'encryption', 'validate_cert', 'username', 'password', 'protocol'];
122
123
    /**
124
     * @var string $default_message_mask
125
     */
126
    protected $default_message_mask = MessageMask::class;
127
128
    /**
129
     * @var string $default_attachment_mask
130
     */
131
    protected $default_attachment_mask = AttachmentMask::class;
132
133
    /**
134
     * Client constructor.
135
     * @param array $config
136
     *
137
     * @throws MaskNotFoundException
138
     */
139
    public function __construct($config = []) {
140
        $this->setConfig($config);
141
        $this->setMaskFromConfig($config);
142
    }
143
144
    /**
145
     * Client destructor
146
     */
147
    public function __destruct() {
148
        $this->disconnect();
149
    }
150
151
    /**
152
     * Set the Client configuration
153
     *
154
     * @param array $config
155
     *
156
     * @return self
157
     */
158
    public function setConfig(array $config) {
159
        $default_account = config('imap.default');
160
        $default_config  = config("imap.accounts.$default_account");
161
162
        foreach ($this->valid_config_keys as $key) {
163
            $this->$key = isset($config[$key]) ? $config[$key] : $default_config[$key];
164
        }
165
166
        return $this;
167
    }
168
169
    /**
170
     * Look for a possible mask in any available config
171
     * @param $config
172
     *
173
     * @throws MaskNotFoundException
174
     */
175
    protected function setMaskFromConfig($config) {
176
        $default_config  = config("imap.masks");
177
178
        if(isset($config['masks'])){
179
            if(isset($config['masks']['message'])) {
180
                if(class_exists($config['masks']['message'])) {
181
                    $this->default_message_mask = $config['masks']['message'];
182
                }else{
183
                    throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['message']);
184
                }
185
            }else{
186
                if(class_exists($default_config['message'])) {
187
                    $this->default_message_mask = $default_config['message'];
188
                }else{
189
                    throw new MaskNotFoundException("Unknown mask provided: ".$default_config['message']);
190
                }
191
            }
192
            if(isset($config['masks']['attachment'])) {
193
                if(class_exists($config['masks']['attachment'])) {
194
                    $this->default_message_mask = $config['masks']['attachment'];
195
                }else{
196
                    throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['attachment']);
197
                }
198
            }else{
199
                if(class_exists($default_config['attachment'])) {
200
                    $this->default_message_mask = $default_config['attachment'];
201
                }else{
202
                    throw new MaskNotFoundException("Unknown mask provided: ".$default_config['attachment']);
203
                }
204
            }
205
        }else{
206
            if(class_exists($default_config['message'])) {
207
                $this->default_message_mask = $default_config['message'];
208
            }else{
209
                throw new MaskNotFoundException("Unknown mask provided: ".$default_config['message']);
210
            }
211
212
            if(class_exists($default_config['attachment'])) {
213
                $this->default_message_mask = $default_config['attachment'];
214
            }else{
215
                throw new MaskNotFoundException("Unknown mask provided: ".$default_config['attachment']);
216
            }
217
        }
218
219
    }
220
221
    /**
222
     * Get the current imap resource
223
     *
224
     * @return bool|resource
225
     * @throws ConnectionFailedException
226
     */
227
    public function getConnection() {
228
        $this->checkConnection();
229
        return $this->connection;
230
    }
231
232
    /**
233
     * Set read only property and reconnect if it's necessary.
234
     *
235
     * @param bool $read_only
236
     *
237
     * @return self
238
     */
239
    public function setReadOnly($read_only = true) {
240
        $this->read_only = $read_only;
241
242
        return $this;
243
    }
244
245
    /**
246
     * Determine if connection was established.
247
     *
248
     * @return bool
249
     */
250
    public function isConnected() {
251
        return $this->connected;
252
    }
253
254
    /**
255
     * Determine if connection is in read only mode.
256
     *
257
     * @return bool
258
     */
259
    public function isReadOnly() {
260
        return $this->read_only;
261
    }
262
263
    /**
264
     * Determine if connection was established and connect if not.
265
     *
266
     * @throws ConnectionFailedException
267
     */
268
    public function checkConnection() {
269
        if (!$this->isConnected() || $this->connection === false) {
270
            $this->connect();
271
        }
272
    }
273
274
    /**
275
     * Connect to server.
276
     *
277
     * @param int $attempts
278
     *
279
     * @return $this
280
     * @throws ConnectionFailedException
281
     */
282
    public function connect($attempts = 3) {
283
        $this->disconnect();
284
285
        try {
286
            $this->connection = imap_open(
287
                $this->getAddress(),
288
                $this->username,
289
                $this->password,
290
                $this->getOptions(),
291
                $attempts,
292
                config('imap.options.open')
293
            );
294
            $this->connected = !!$this->connection;
295
        } catch (\ErrorException $e) {
296
            $errors = imap_errors();
297
            $message = $e->getMessage().'. '.implode("; ", (is_array($errors) ? $errors : array()));
0 ignored issues
show
introduced by
The condition is_array($errors) is always true.
Loading history...
298
299
            throw new ConnectionFailedException($message);
300
        }
301
302
        return $this;
303
    }
304
305
    /**
306
     * Disconnect from server.
307
     *
308
     * @return $this
309
     */
310
    public function disconnect() {
311
        if ($this->isConnected() && $this->connection !== false && is_integer($this->connection) === false) {
312
            $this->errors = array_merge($this->errors, imap_errors() ?: []);
313
            $this->connected = !imap_close($this->connection, IMAP::CL_EXPUNGE);
0 ignored issues
show
Bug introduced by
$this->connection of type integer is incompatible with the type resource expected by parameter $imap_stream of imap_close(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

313
            $this->connected = !imap_close(/** @scrutinizer ignore-type */ $this->connection, IMAP::CL_EXPUNGE);
Loading history...
314
        }
315
316
        return $this;
317
    }
318
319
    /**
320
     * Get a folder instance by a folder name
321
     * ---------------------------------------------
322
     * PLEASE NOTE: This is an experimental function
323
     * ---------------------------------------------
324
     * @param string        $folder_name
325
     * @param int           $attributes
326
     * @param null|string   $delimiter
327
     *
328
     * @return Folder
329
     */
330
    public function getFolder($folder_name, $attributes = 32, $delimiter = null) {
331
332
        $delimiter = $delimiter === null ? config('imap.options.delimiter', '/') : $delimiter;
333
334
        $oFolder = new Folder($this, (object) [
335
            'name'       => $this->getAddress().$folder_name,
336
            'attributes' => $attributes,
337
            'delimiter'  => $delimiter
338
        ]);
339
340
        return $oFolder;
341
    }
342
343
    /**
344
     * Get folders list.
345
     * If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array.
346
     *
347
     * @param boolean     $hierarchical
348
     * @param string|null $parent_folder
349
     *
350
     * @return FolderCollection
351
     * @throws ConnectionFailedException
352
     * @throws MailboxFetchingException
353
     */
354
    public function getFolders($hierarchical = true, $parent_folder = null) {
355
        $this->checkConnection();
356
        $folders = FolderCollection::make([]);
357
358
        $pattern = $parent_folder.($hierarchical ? '%' : '*');
359
360
        $items = imap_getmailboxes($this->connection, $this->getAddress(), $pattern);
0 ignored issues
show
Bug introduced by
It seems like $this->connection can also be of type true; however, parameter $imap_stream of imap_getmailboxes() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

360
        $items = imap_getmailboxes(/** @scrutinizer ignore-type */ $this->connection, $this->getAddress(), $pattern);
Loading history...
361
        if(is_array($items)){
0 ignored issues
show
introduced by
The condition is_array($items) is always true.
Loading history...
362
            foreach ($items as $item) {
363
                $folder = new Folder($this, $item);
364
365
                if ($hierarchical && $folder->hasChildren()) {
366
                    $pattern = $folder->full_name.$folder->delimiter.'%';
367
368
                    $children = $this->getFolders(true, $pattern);
369
                    $folder->setChildren($children);
370
                }
371
372
                $folders->push($folder);
373
            }
374
375
            return $folders;
376
        }else{
377
            throw new MailboxFetchingException($this->getLastError());
378
        }
379
    }
380
381
    /**
382
     * Open folder.
383
     *
384
     * @param string|Folder $folder_path
385
     * @param int           $attempts
386
     *
387
     * @throws ConnectionFailedException
388
     */
389
    public function openFolder($folder_path, $attempts = 3) {
390
        $this->checkConnection();
391
392
        if(property_exists($folder_path, 'path')) {
393
            $folder_path = $folder_path->path;
394
        }
395
396
        if ($this->active_folder !== $folder_path) {
397
            $this->active_folder = $folder_path;
0 ignored issues
show
Documentation Bug introduced by
It seems like $folder_path can also be of type string. However, the property $active_folder is declared as type Webklex\IMAP\Folder. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
398
399
            imap_reopen($this->getConnection(), $folder_path, $this->getOptions(), $attempts);
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type true; however, parameter $imap_stream of imap_reopen() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

399
            imap_reopen(/** @scrutinizer ignore-type */ $this->getConnection(), $folder_path, $this->getOptions(), $attempts);
Loading history...
Bug introduced by
It seems like $folder_path can also be of type Webklex\IMAP\Folder; however, parameter $mailbox of imap_reopen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

399
            imap_reopen($this->getConnection(), /** @scrutinizer ignore-type */ $folder_path, $this->getOptions(), $attempts);
Loading history...
400
        }
401
    }
402
403
    /**
404
     * Create a new Folder
405
     * @param string $name
406
     * @param boolean $expunge
407
     *
408
     * @return bool
409
     * @throws ConnectionFailedException
410
     */
411
    public function createFolder($name, $expunge = true) {
412
        $this->checkConnection();
413
        $status = imap_createmailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($name));
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type true; however, parameter $imap_stream of imap_createmailbox() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

413
        $status = imap_createmailbox(/** @scrutinizer ignore-type */ $this->getConnection(), $this->getAddress() . imap_utf7_encode($name));
Loading history...
414
        if($expunge) $this->expunge();
415
416
        return $status;
417
    }
418
    
419
    /**
420
     * Rename Folder
421
     * @param string  $old_name
422
     * @param string  $new_name
423
     * @param boolean $expunge
424
     *
425
     * @return bool
426
     * @throws ConnectionFailedException
427
     */
428
    public function renameFolder($old_name, $new_name, $expunge = true) {
429
        $this->checkConnection();
430
        $status = imap_renamemailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($old_name), $this->getAddress() . imap_utf7_encode($new_name));
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type true; however, parameter $imap_stream of imap_renamemailbox() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

430
        $status = imap_renamemailbox(/** @scrutinizer ignore-type */ $this->getConnection(), $this->getAddress() . imap_utf7_encode($old_name), $this->getAddress() . imap_utf7_encode($new_name));
Loading history...
431
        if($expunge) $this->expunge();
432
433
        return $status;
434
    }
435
    
436
     /**
437
     * Delete Folder
438
     * @param string $name
439
      * @param boolean $expunge
440
     *
441
     * @return bool
442
     * @throws ConnectionFailedException
443
     */
444
    public function deleteFolder($name, $expunge = true) {
445
        $this->checkConnection();
446
        $status = imap_deletemailbox($this->getConnection(), $this->getAddress() . imap_utf7_encode($name));
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type true; however, parameter $imap_stream of imap_deletemailbox() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

446
        $status = imap_deletemailbox(/** @scrutinizer ignore-type */ $this->getConnection(), $this->getAddress() . imap_utf7_encode($name));
Loading history...
447
        if($expunge) $this->expunge();
448
449
        return $status;
450
    }
451
452
    /**
453
     * Get messages from folder.
454
     *
455
     * @param Folder   $folder
456
     * @param string   $criteria
457
     * @param int|null $fetch_options
458
     * @param boolean  $fetch_body
459
     * @param boolean  $fetch_attachment
460
     * @param boolean  $fetch_flags
461
     *
462
     * @return MessageCollection
463
     * @throws ConnectionFailedException
464
     * @throws Exceptions\InvalidWhereQueryCriteriaException
465
     * @throws GetMessagesFailedException
466
     *
467
     * @deprecated 1.0.5.2:2.0.0 No longer needed. Use Folder::getMessages() instead
468
     * @see Folder::getMessages()
469
     */
470
    public function getMessages(Folder $folder, $criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false) {
471
        return $folder->getMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags);
472
    }
473
474
    /**
475
     * Get all unseen messages from folder
476
     *
477
     * @param Folder   $folder
478
     * @param string   $criteria
479
     * @param int|null $fetch_options
480
     * @param boolean  $fetch_body
481
     * @param boolean  $fetch_attachment
482
     * @param boolean  $fetch_flags
483
     *
484
     * @return MessageCollection
485
     * @throws ConnectionFailedException
486
     * @throws Exceptions\InvalidWhereQueryCriteriaException
487
     * @throws GetMessagesFailedException
488
     * @throws MessageSearchValidationException
489
     *
490
     * @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::getMessages('UNSEEN') instead
491
     * @see Folder::getMessages()
492
     */
493
    public function getUnseenMessages(Folder $folder, $criteria = 'UNSEEN', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false) {
494
        return $folder->getUnseenMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags);
0 ignored issues
show
Deprecated Code introduced by
The function Webklex\IMAP\Folder::getUnseenMessages() has been deprecated: 1.0.5:2.0.0 No longer needed. Use Folder::getMessages('UNSEEN') instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

494
        return /** @scrutinizer ignore-deprecated */ $folder->getUnseenMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
495
    }
496
497
    /**
498
     * Search messages by a given search criteria
499
     *
500
     * @param array    $where
501
     * @param Folder   $folder
502
     * @param int|null $fetch_options
503
     * @param boolean  $fetch_body
504
     * @param string   $charset
505
     * @param boolean  $fetch_attachment
506
     * @param boolean  $fetch_flags
507
     *
508
     * @return MessageCollection
509
     * @throws ConnectionFailedException
510
     * @throws Exceptions\InvalidWhereQueryCriteriaException
511
     * @throws GetMessagesFailedException
512
     *
513
     * @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::searchMessages() instead
514
     * @see Folder::searchMessages()
515
     *
516
     */
517
    public function searchMessages(array $where, Folder $folder, $fetch_options = null, $fetch_body = true, $charset = "UTF-8", $fetch_attachment = true, $fetch_flags = false) {
518
        return $folder->searchMessages($where, $fetch_options, $fetch_body, $charset, $fetch_attachment, $fetch_flags);
0 ignored issues
show
Deprecated Code introduced by
The function Webklex\IMAP\Folder::searchMessages() has been deprecated: 1.2.1:2.0.0 No longer needed. Use Folder::query() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

518
        return /** @scrutinizer ignore-deprecated */ $folder->searchMessages($where, $fetch_options, $fetch_body, $charset, $fetch_attachment, $fetch_flags);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
$fetch_flags of type boolean is incompatible with the type integer|null expected by parameter $limit of Webklex\IMAP\Folder::searchMessages(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

518
        return $folder->searchMessages($where, $fetch_options, $fetch_body, $charset, $fetch_attachment, /** @scrutinizer ignore-type */ $fetch_flags);
Loading history...
Bug introduced by
$charset of type string is incompatible with the type boolean expected by parameter $fetch_attachment of Webklex\IMAP\Folder::searchMessages(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

518
        return $folder->searchMessages($where, $fetch_options, $fetch_body, /** @scrutinizer ignore-type */ $charset, $fetch_attachment, $fetch_flags);
Loading history...
519
    }
520
521
    /**
522
     * Get option for imap_open and imap_reopen.
523
     * It supports only isReadOnly feature.
524
     *
525
     * @return int
526
     */
527
    protected function getOptions() {
528
        return ($this->isReadOnly()) ? IMAP::OP_READONLY : 0;
529
    }
530
531
    /**
532
     * Get full address of mailbox.
533
     *
534
     * @return string
535
     */
536
    protected function getAddress() {
537
        $address = "{".$this->host.":".$this->port."/".($this->protocol ? $this->protocol : 'imap');
538
        if (!$this->validate_cert) {
539
            $address .= '/novalidate-cert';
540
        }
541
        if (in_array($this->encryption,['tls','ssl'])) {
542
            $address .= '/'.$this->encryption;
543
        }
544
545
        $address .= '}';
546
547
        return $address;
548
    }
549
550
    /**
551
     * Retrieve the quota level settings, and usage statics per mailbox
552
     *
553
     * @return array
554
     * @throws ConnectionFailedException
555
     */
556
    public function getQuota() {
557
        $this->checkConnection();
558
        return imap_get_quota($this->getConnection(), 'user.'.$this->username);
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type true; however, parameter $imap_stream of imap_get_quota() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

558
        return imap_get_quota(/** @scrutinizer ignore-type */ $this->getConnection(), 'user.'.$this->username);
Loading history...
559
    }
560
561
    /**
562
     * Retrieve the quota settings per user
563
     *
564
     * @param string $quota_root
565
     *
566
     * @return array
567
     * @throws ConnectionFailedException
568
     */
569
    public function getQuotaRoot($quota_root = 'INBOX') {
570
        $this->checkConnection();
571
        return imap_get_quotaroot($this->getConnection(), $quota_root);
0 ignored issues
show
Bug introduced by
It seems like $this->getConnection() can also be of type true; however, parameter $imap_stream of imap_get_quotaroot() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

571
        return imap_get_quotaroot(/** @scrutinizer ignore-type */ $this->getConnection(), $quota_root);
Loading history...
572
    }
573
574
    /**
575
     * Gets the number of messages in the current mailbox
576
     *
577
     * @return int
578
     * @throws ConnectionFailedException
579
     */
580
    public function countMessages() {
581
        $this->checkConnection();
582
        return imap_num_msg($this->connection);
0 ignored issues
show
Bug introduced by
It seems like $this->connection can also be of type true; however, parameter $imap_stream of imap_num_msg() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

582
        return imap_num_msg(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
583
    }
584
585
    /**
586
     * Gets the number of recent messages in current mailbox
587
     *
588
     * @return int
589
     * @throws ConnectionFailedException
590
     */
591
    public function countRecentMessages() {
592
        $this->checkConnection();
593
        return imap_num_recent($this->connection);
0 ignored issues
show
Bug introduced by
It seems like $this->connection can also be of type true; however, parameter $imap_stream of imap_num_recent() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

593
        return imap_num_recent(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
594
    }
595
596
    /**
597
     * Returns all IMAP alert messages that have occurred
598
     *
599
     * @return array
600
     */
601
    public function getAlerts() {
602
        return imap_alerts();
603
    }
604
605
    /**
606
     * Returns all of the IMAP errors that have occurred
607
     *
608
     * @return array
609
     */
610
    public function getErrors() {
611
        $this->errors = array_merge($this->errors, imap_errors() ?: []);
612
613
        return $this->errors;
614
    }
615
616
    /**
617
     * Gets the last IMAP error that occurred during this page request
618
     *
619
     * @return string
620
     */
621
    public function getLastError() {
622
        return imap_last_error();
623
    }
624
625
    /**
626
     * Delete all messages marked for deletion
627
     *
628
     * @return bool
629
     * @throws ConnectionFailedException
630
     */
631
    public function expunge() {
632
        $this->checkConnection();
633
        return imap_expunge($this->connection);
0 ignored issues
show
Bug introduced by
It seems like $this->connection can also be of type true; however, parameter $imap_stream of imap_expunge() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

633
        return imap_expunge(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
634
    }
635
636
    /**
637
     * Check current mailbox
638
     *
639
     * @return object {
640
     *      Date    [string(37) "Wed, 8 Mar 2017 22:17:54 +0100 (CET)"]             current system time formatted according to » RFC2822
641
     *      Driver  [string(4) "imap"]                                              protocol used to access this mailbox: POP3, IMAP, NNTP
642
     *      Mailbox ["{[email protected]:993/imap/user="[email protected]"}INBOX"]    the mailbox name
643
     *      Nmsgs   [int(1)]                                                        number of messages in the mailbox
644
     *      Recent  [int(0)]                                                        number of recent messages in the mailbox
645
     * }
646
     * @throws ConnectionFailedException
647
     */
648
    public function checkCurrentMailbox() {
649
        $this->checkConnection();
650
        return imap_check($this->connection);
0 ignored issues
show
Bug introduced by
It seems like $this->connection can also be of type true; however, parameter $imap_stream of imap_check() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

650
        return imap_check(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
651
    }
652
653
    /**
654
     * Set the imap timeout for a given operation type
655
     * @param $type
656
     * @param $timeout
657
     *
658
     * @return mixed
659
     * @throws InvalidImapTimeoutTypeException
660
     */
661
    public function setTimeout($type, $timeout) {
662
        if(0 <= $type && $type <= 4) {
663
            return imap_timeout($type, $timeout);
664
        }
665
666
        throw new InvalidImapTimeoutTypeException("Invalid imap timeout type provided.");
667
    }
668
669
    /**
670
     * Get the timeout for a certain operation
671
     * @param $type
672
     *
673
     * @return mixed
674
     * @throws InvalidImapTimeoutTypeException
675
     */
676
    public function getTimeout($type){
677
        if(0 <= $type && $type <= 4) {
678
            return imap_timeout($type);
679
        }
680
681
        throw new InvalidImapTimeoutTypeException("Invalid imap timeout type provided.");
682
    }
683
684
    /**
685
     * @return string
686
     */
687
    public function getDefaultMessageMask(){
688
        return $this->default_message_mask;
689
    }
690
691
    /**
692
     * @param $mask
693
     *
694
     * @return $this
695
     * @throws MaskNotFoundException
696
     */
697
    public function setDefaultMessageMask($mask) {
698
        if(class_exists($mask)) {
699
            $this->default_message_mask = $mask;
700
701
            return $this;
702
        }
703
704
        throw new MaskNotFoundException("Unknown mask provided: ".$mask);
705
    }
706
707
    /**
708
     * @return string
709
     */
710
    public function getDefaultAttachmentMask(){
711
        return $this->default_attachment_mask;
712
    }
713
714
    /**
715
     * @param $mask
716
     *
717
     * @return $this
718
     * @throws MaskNotFoundException
719
     */
720
    public function setDefaultAttachmentMask($mask) {
721
        if(class_exists($mask)) {
722
            $this->default_attachment_mask = $mask;
723
724
            return $this;
725
        }
726
727
        throw new MaskNotFoundException("Unknown mask provided: ".$mask);
728
    }
729
730
    /**
731
     * Get the current active folder
732
     *
733
     * @return Folder
734
     */
735
    public function getFolderPath(){
736
        return $this->active_folder;
737
    }
738
}
739