IMAPMailbox::createTransport()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace rdx\imap;
4
5
use RuntimeException;
6
7
class IMAPMailbox {
8
9
	protected $server = '';
10
	protected $username = '';
11
	protected $password = '';
12
	protected $mailbox = '';
13
	protected $flags = [];
14
15
	/**
16
	 * @var IMAPTransport
17
	 */
18
	protected $imap;
19
20
	public function __construct( $server, $username, $password, $mailbox = null, array $flags = [] ) {
21
		$this->server = $server;
22
		$this->username = $username;
23
		$this->password = $password;
24
		$this->mailbox = $mailbox ?: 'INBOX';
25
		$this->flags = $flags;
26
27
		$this->connect();
28
	}
29
30
	/** @return IMAPTransport */
31
	public function connect() {
32
		if ( !$this->imap ) {
33
			$this->imap = $this->createTransport()->open(
34
				$this->server,
35
				$this->username,
36
				$this->password,
37
				$this->mailbox,
38
				$this->flags
39
			);
40
		}
41
42
		return $this->imap;
43
	}
44
45
	/** @return IMAPTransport */
46
	public function imap() {
47
		return $this->imap;
48
	}
49
50
	/** @return IMAPTransport */
51
	public function createTransport() {
52
		return new IMAPTransport;
53
	}
54
55
	/** @return IMAPMessage */
56
	public function createMessage( $msgNum, $unseen = null ) {
57
		return new IMAPMessage($this, $msgNum, $unseen);
58
	}
59
60
	/** @return string[] */
61
	public function headers( $newestFirst = true ) {
62
		$this->connect();
63
64
		$headers = $this->imap()->headers();
65
66
		if ( $newestFirst ) {
67
			$headers = array_reverse($headers);
68
		}
69
70
		return $headers;
71
	}
72
73
	/** @return IMAPMessage */
74
	public function message( $msgNum ) {
75
		$this->connect();
76
77
		return $this->createMessage($msgNum);
78
	}
79
80
	/** @return IMAPMessage[] */
81
	public function messages( array $options = [] ) {
82
		$options += [
83
			'offset' => 0,
84
			'limit' => 0,
85
			'seen' => null,
86
			'newestFirst' => true,
87
		];
88
89
		$headers = $this->headers($options['newestFirst']);
90
91
		$messages = [];
92
		$eligibles = 0;
93
		foreach ( $headers AS $n => $header ) {
94
			if ( preg_match('/([UN]?)\s+(\d+)([\)\d])[\d ]\d\-/', $header, $match) ) {
95
				$unseen = (bool) trim($match[1]);
96
				$msgNum = (int) rtrim($match[2] . $match[3], ')');
97
98
				$eligible = $options['seen'] === null || $unseen != $options['seen'];
99
				if ( $eligible ) {
100
					$eligibles++;
101
102
					if ( $eligibles > $options['offset'] ) {
103
						$messages[] = $this->createMessage($msgNum, $unseen);
104
					}
105
				}
106
107
				if ( $options['limit'] && isset($messages[ $options['limit'] - 1 ]) ) {
108
					break;
109
				}
110
			}
111
			else {
112
				throw new RuntimeException("Can't extract message header: '$header'");
113
			}
114
		}
115
116
		return $messages;
117
	}
118
119
	/** @return string[] */
120
	public function getTextSubtypes() {
121
		return ['PLAIN'];
122
	}
123
124
	/** @return string[] */
125
	public function getHtmlSubtypes() {
126
		return ['HTML'];
127
	}
128
129
	/** @return object */
130
	public function msgInfo() {
131
		return $this->imap()->mailboxmsginfo();
132
	}
133
134
	/** @return bool */
135
	public function vacuum() {
136
		return $this->imap()->expunge();
137
	}
138
139
}
140