Passed
Push — master ( 00f43d...a0d391 )
by Malte
02:56
created

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

312
            $this->connected = !imap_close(/** @scrutinizer ignore-type */ $this->connection, IMAP::CL_EXPUNGE);
Loading history...
313
        }
314
315
        return $this;
316
    }
317
318
    /**
319
     * Get a folder instance by a folder name
320
     * ---------------------------------------------
321
     * PLEASE NOTE: This is an experimental function
322
     * ---------------------------------------------
323
     * @param string        $folder_name
324
     * @param int           $attributes
325
     * @param null|string   $delimiter
326
     *
327
     * @return Folder
328
     */
329
    public function getFolder($folder_name, $attributes = 32, $delimiter = null) {
330
331
        $delimiter = $delimiter === null ? config('imap.options.delimiter', '/') : $delimiter;
332
333
        $oFolder = new Folder($this, (object) [
334
            'name'       => $this->getAddress().$folder_name,
335
            'attributes' => $attributes,
336
            'delimiter'  => $delimiter
337
        ]);
338
339
        return $oFolder;
340
    }
341
342
    /**
343
     * Get folders list.
344
     * If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array.
345
     *
346
     * @param boolean     $hierarchical
347
     * @param string|null $parent_folder
348
     *
349
     * @return FolderCollection
350
     * @throws ConnectionFailedException
351
     */
352
    public function getFolders($hierarchical = true, $parent_folder = null) {
353
        $this->checkConnection();
354
        $folders = FolderCollection::make([]);
355
356
        $pattern = $parent_folder.($hierarchical ? '%' : '*');
357
358
        $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

358
        $items = imap_getmailboxes(/** @scrutinizer ignore-type */ $this->connection, $this->getAddress(), $pattern);
Loading history...
359
        foreach ($items as $item) {
360
            $folder = new Folder($this, $item);
361
362
            if ($hierarchical && $folder->hasChildren()) {
363
                $pattern = $folder->full_name.$folder->delimiter.'%';
364
365
                $children = $this->getFolders(true, $pattern);
366
                $folder->setChildren($children);
367
            }
368
369
            $folders->push($folder);
370
        }
371
372
        return $folders;
373
    }
374
375
    /**
376
     * Open folder.
377
     *
378
     * @param Folder $folder
379
     * @param int    $attempts
380
     *
381
     * @throws ConnectionFailedException
382
     */
383
    public function openFolder(Folder $folder, $attempts = 3) {
384
        $this->checkConnection();
385
386
        if ($this->active_folder !== $folder) {
387
            $this->active_folder = $folder;
388
389
            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

389
            imap_reopen(/** @scrutinizer ignore-type */ $this->getConnection(), $folder->path, $this->getOptions(), $attempts);
Loading history...
390
        }
391
    }
392
393
    /**
394
     * Create a new Folder
395
     * @param string $name
396
     * @param boolean $expunge
397
     *
398
     * @return bool
399
     * @throws ConnectionFailedException
400
     */
401
    public function createFolder($name, $expunge = true) {
402
        $this->checkConnection();
403
        $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

403
        $status = imap_createmailbox(/** @scrutinizer ignore-type */ $this->getConnection(), $this->getAddress() . imap_utf7_encode($name));
Loading history...
404
        if($expunge) $this->expunge();
405
406
        return $status;
407
    }
408
    
409
    /**
410
     * Rename Folder
411
     * @param string  $old_name
412
     * @param string  $new_name
413
     * @param boolean $expunge
414
     *
415
     * @return bool
416
     * @throws ConnectionFailedException
417
     */
418
    public function renameFolder($old_name, $new_name, $expunge = true) {
419
        $this->checkConnection();
420
        $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

420
        $status = imap_renamemailbox(/** @scrutinizer ignore-type */ $this->getConnection(), $this->getAddress() . imap_utf7_encode($old_name), $this->getAddress() . imap_utf7_encode($new_name));
Loading history...
421
        if($expunge) $this->expunge();
422
423
        return $status;
424
    }
425
    
426
     /**
427
     * Delete Folder
428
     * @param string $name
429
      * @param boolean $expunge
430
     *
431
     * @return bool
432
     * @throws ConnectionFailedException
433
     */
434
    public function deleteFolder($name, $expunge = true) {
435
        $this->checkConnection();
436
        $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

436
        $status = imap_deletemailbox(/** @scrutinizer ignore-type */ $this->getConnection(), $this->getAddress() . imap_utf7_encode($name));
Loading history...
437
        if($expunge) $this->expunge();
438
439
        return $status;
440
    }
441
442
    /**
443
     * Get messages from folder.
444
     *
445
     * @param Folder   $folder
446
     * @param string   $criteria
447
     * @param int|null $fetch_options
448
     * @param boolean  $fetch_body
449
     * @param boolean  $fetch_attachment
450
     * @param boolean  $fetch_flags
451
     *
452
     * @return MessageCollection
453
     * @throws ConnectionFailedException
454
     * @throws Exceptions\InvalidWhereQueryCriteriaException
455
     * @throws GetMessagesFailedException
456
     *
457
     * @deprecated 1.0.5.2:2.0.0 No longer needed. Use Folder::getMessages() instead
458
     * @see Folder::getMessages()
459
     */
460
    public function getMessages(Folder $folder, $criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false) {
461
        return $folder->getMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags);
462
    }
463
464
    /**
465
     * Get all unseen messages from folder
466
     *
467
     * @param Folder   $folder
468
     * @param string   $criteria
469
     * @param int|null $fetch_options
470
     * @param boolean  $fetch_body
471
     * @param boolean  $fetch_attachment
472
     * @param boolean  $fetch_flags
473
     *
474
     * @return MessageCollection
475
     * @throws ConnectionFailedException
476
     * @throws Exceptions\InvalidWhereQueryCriteriaException
477
     * @throws GetMessagesFailedException
478
     * @throws MessageSearchValidationException
479
     *
480
     * @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::getMessages('UNSEEN') instead
481
     * @see Folder::getMessages()
482
     */
483
    public function getUnseenMessages(Folder $folder, $criteria = 'UNSEEN', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false) {
484
        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

484
        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...
485
    }
486
487
    /**
488
     * Search messages by a given search criteria
489
     *
490
     * @param array    $where
491
     * @param Folder   $folder
492
     * @param int|null $fetch_options
493
     * @param boolean  $fetch_body
494
     * @param string   $charset
495
     * @param boolean  $fetch_attachment
496
     * @param boolean  $fetch_flags
497
     *
498
     * @return MessageCollection
499
     * @throws ConnectionFailedException
500
     * @throws Exceptions\InvalidWhereQueryCriteriaException
501
     * @throws GetMessagesFailedException
502
     *
503
     * @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::searchMessages() instead
504
     * @see Folder::searchMessages()
505
     *
506
     */
507
    public function searchMessages(array $where, Folder $folder, $fetch_options = null, $fetch_body = true, $charset = "UTF-8", $fetch_attachment = true, $fetch_flags = false) {
508
        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

508
        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

508
        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

508
        return $folder->searchMessages($where, $fetch_options, $fetch_body, /** @scrutinizer ignore-type */ $charset, $fetch_attachment, $fetch_flags);
Loading history...
509
    }
510
511
    /**
512
     * Get option for imap_open and imap_reopen.
513
     * It supports only isReadOnly feature.
514
     *
515
     * @return int
516
     */
517
    protected function getOptions() {
518
        return ($this->isReadOnly()) ? IMAP::OP_READONLY : 0;
519
    }
520
521
    /**
522
     * Get full address of mailbox.
523
     *
524
     * @return string
525
     */
526
    protected function getAddress() {
527
        $address = "{".$this->host.":".$this->port."/".($this->protocol ? $this->protocol : 'imap');
528
        if (!$this->validate_cert) {
529
            $address .= '/novalidate-cert';
530
        }
531
        if (in_array($this->encryption,['tls','ssl'])) {
532
            $address .= '/'.$this->encryption;
533
        }
534
        $address .= '}';
535
536
        return $address;
537
    }
538
539
    /**
540
     * Retrieve the quota level settings, and usage statics per mailbox
541
     *
542
     * @return array
543
     * @throws ConnectionFailedException
544
     */
545
    public function getQuota() {
546
        $this->checkConnection();
547
        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

547
        return imap_get_quota(/** @scrutinizer ignore-type */ $this->getConnection(), 'user.'.$this->username);
Loading history...
548
    }
549
550
    /**
551
     * Retrieve the quota settings per user
552
     *
553
     * @param string $quota_root
554
     *
555
     * @return array
556
     * @throws ConnectionFailedException
557
     */
558
    public function getQuotaRoot($quota_root = 'INBOX') {
559
        $this->checkConnection();
560
        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

560
        return imap_get_quotaroot(/** @scrutinizer ignore-type */ $this->getConnection(), $quota_root);
Loading history...
561
    }
562
563
    /**
564
     * Gets the number of messages in the current mailbox
565
     *
566
     * @return int
567
     * @throws ConnectionFailedException
568
     */
569
    public function countMessages() {
570
        $this->checkConnection();
571
        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

571
        return imap_num_msg(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
572
    }
573
574
    /**
575
     * Gets the number of recent messages in current mailbox
576
     *
577
     * @return int
578
     * @throws ConnectionFailedException
579
     */
580
    public function countRecentMessages() {
581
        $this->checkConnection();
582
        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

582
        return imap_num_recent(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
583
    }
584
585
    /**
586
     * Returns all IMAP alert messages that have occurred
587
     *
588
     * @return array
589
     */
590
    public function getAlerts() {
591
        return imap_alerts();
592
    }
593
594
    /**
595
     * Returns all of the IMAP errors that have occurred
596
     *
597
     * @return array
598
     */
599
    public function getErrors() {
600
        $this->errors = array_merge($this->errors, imap_errors() ?: []);
601
602
        return $this->errors;
603
    }
604
605
    /**
606
     * Gets the last IMAP error that occurred during this page request
607
     *
608
     * @return string
609
     */
610
    public function getLastError() {
611
        return imap_last_error();
612
    }
613
614
    /**
615
     * Delete all messages marked for deletion
616
     *
617
     * @return bool
618
     * @throws ConnectionFailedException
619
     */
620
    public function expunge() {
621
        $this->checkConnection();
622
        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

622
        return imap_expunge(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
623
    }
624
625
    /**
626
     * Check current mailbox
627
     *
628
     * @return object {
629
     *      Date    [string(37) "Wed, 8 Mar 2017 22:17:54 +0100 (CET)"]             current system time formatted according to » RFC2822
630
     *      Driver  [string(4) "imap"]                                              protocol used to access this mailbox: POP3, IMAP, NNTP
631
     *      Mailbox ["{[email protected]:993/imap/user="[email protected]"}INBOX"]    the mailbox name
632
     *      Nmsgs   [int(1)]                                                        number of messages in the mailbox
633
     *      Recent  [int(0)]                                                        number of recent messages in the mailbox
634
     * }
635
     * @throws ConnectionFailedException
636
     */
637
    public function checkCurrentMailbox() {
638
        $this->checkConnection();
639
        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

639
        return imap_check(/** @scrutinizer ignore-type */ $this->connection);
Loading history...
640
    }
641
642
    /**
643
     * Set the imap timeout for a given operation type
644
     * @param $type
645
     * @param $timeout
646
     *
647
     * @return mixed
648
     * @throws InvalidImapTimeoutTypeException
649
     */
650
    public function setTimeout($type, $timeout) {
651
        if(0 <= $type && $type <= 4) {
652
            return imap_timeout($type, $timeout);
653
        }
654
655
        throw new InvalidImapTimeoutTypeException("Invalid imap timeout type provided.");
656
    }
657
658
    /**
659
     * Get the timeout for a certain operation
660
     * @param $type
661
     *
662
     * @return mixed
663
     * @throws InvalidImapTimeoutTypeException
664
     */
665
    public function getTimeout($type){
666
        if(0 <= $type && $type <= 4) {
667
            return imap_timeout($type);
668
        }
669
670
        throw new InvalidImapTimeoutTypeException("Invalid imap timeout type provided.");
671
    }
672
673
    /**
674
     * @return string
675
     */
676
    public function getDefaultMessageMask(){
677
        return $this->default_message_mask;
678
    }
679
680
    /**
681
     * @param $mask
682
     *
683
     * @return $this
684
     * @throws MaskNotFoundException
685
     */
686
    public function setDefaultMessageMask($mask) {
687
        if(class_exists($mask)) {
688
            $this->default_message_mask = $mask;
689
690
            return $this;
691
        }
692
693
        throw new MaskNotFoundException("Unknown mask provided: ".$mask);
694
    }
695
696
    /**
697
     * @return string
698
     */
699
    public function getDefaultAttachmentMask(){
700
        return $this->default_attachment_mask;
701
    }
702
703
    /**
704
     * @param $mask
705
     *
706
     * @return $this
707
     * @throws MaskNotFoundException
708
     */
709
    public function setDefaultAttachmentMask($mask) {
710
        if(class_exists($mask)) {
711
            $this->default_attachment_mask = $mask;
712
713
            return $this;
714
        }
715
716
        throw new MaskNotFoundException("Unknown mask provided: ".$mask);
717
    }
718
}
719