Completed
Branch master (0ca02e)
by Rudie
02:12
created

IMAPMessage   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 34
c 9
b 0
f 0
lcom 1
cbo 4
dl 0
loc 167
rs 9.2

18 Methods

Rating   Name   Duplication   Size   Complexity  
A flag() 0 3 1
A unflag() 0 3 1
A __construct() 0 9 2
A flags() 0 11 4
A utc() 0 3 1
A subject() 0 8 2
A headers() 0 7 2
A header() 0 4 1
A createMessagePart() 0 3 1
B parts() 0 33 4
A structure() 0 7 2
A subtype() 0 8 3
A section() 0 3 1
A content() 0 7 2
A delete() 0 3 1
A simpleStructure() 0 19 4
A msgNumber() 0 3 1
A mailbox() 0 3 1
1
<?php
2
3
namespace rdx\imap;
4
5
use rdx\imap\IMAPMailbox;
6
use rdx\imap\IMAPMessageContent;
7
use rdx\imap\IMAPMessagePart;
8
9
class IMAPMessage extends IMAPMessageContent implements IMAPMessagePartInterface {
10
11
	protected $mailbox; // rdx\imap\IMAPMailbox
12
13
	protected $msgNumber = 1; // starts at 1, not 0
14
	protected $unseen = true;
15
16
	protected $headers = [];
17
	protected $subject = '';
18
	protected $subtype = '';
19
20
	public function __construct( IMAPMailbox $mailbox, $msgNumber, $unseen = null ) {
21
		$this->mailbox = $mailbox;
22
		$this->msgNumber = $msgNumber;
23
		$this->unseen = $unseen;
24
25
		if ( $unseen === null ) {
26
			$this->unseen = (bool) trim($this->header('unseen'));
27
		}
28
	}
29
30
	protected function flags( $flags, $clear ) {
31
		$cb = [$this->mailbox()->imap(), $clear ? 'unflag' : 'flag'];
32
33
		$feedback = [];
34
		foreach ( (array)$flags AS $flag ) {
35
			$flag = '\\' . ucfirst($flag);
36
			$feedback[] = call_user_func($cb, $this->msgNumber, $flag);
37
		}
38
39
		return is_array($flags) ? $feedback : $feedback[0];
40
	}
41
42
	public function flag( $flags ) {
43
		return $this->flags($flags, false);
44
	}
45
46
	public function unflag( $flags ) {
47
		return $this->flags($flags, true);
48
	}
49
50
	public function utc() {
51
		return strtotime($this->header('date'));
52
	}
53
54
	public function subject() {
55
		if ( empty($this->subject) ) {
56
			$subject = $this->mailbox()->imap()->utf8($this->header('subject'));
57
			$this->subject = trim($subject);
58
		}
59
60
		return $this->subject;
61
	}
62
63
	public function headers() {
64
		if ( empty($this->headers) ) {
65
			$this->headers = $this->mailbox()->imap()->headerinfo($this->msgNumber);
66
		}
67
68
		return $this->headers;
69
	}
70
71
	public function header( $name ) {
72
		$headers = $this->headers();
73
		return @$headers[ strtolower($name) ];
74
	}
75
76
	public function createMessagePart( $structure, $section ) {
77
		return new IMAPMessagePart($this, $structure, $section);
78
	}
79
80
	public function parts() {
81
		if ( empty($this->parts) ) {
82
			$structure = $this->structure();
83
84
			// Possibilities:
85
			// - PLAIN
86
			// - ALTERNATIVE
87
			// - MIXED
88
			// - DELIVERY-STATUS
89
			// - RFC822
90
			// - REPORT
91
			// - HTML
92
			// - CALENDAR
93
			// - JPEG
94
95
			if ( empty($structure->parts) ) {
96
				$this->parts[] = $this->createMessagePart(
97
					$structure,
98
					[1]
99
				);
100
			}
101
			else {
102
				foreach ($structure->parts as $n => $part) {
103
					$this->parts[] = $this->createMessagePart(
104
						$part,
105
						[$n+1]
106
					);
107
				}
108
			}
109
		}
110
111
		return $this->parts;
112
	}
113
114
	public function structure() {
115
		if ( empty($this->structure) ) {
116
			$this->structure = $this->mailbox()->imap()->fetchstructure($this->msgNumber);
117
		}
118
119
		return $this->structure;
120
	}
121
122
	public function subtype() {
123
		if ( empty($this->subtype) ) {
124
			$structure = $this->structure();
125
			$this->subtype = @$structure->subtype ?: '';
126
		}
127
128
		return $this->subtype;
129
	}
130
131
	public function section() {
132
		return [];
133
	}
134
135
	public function content() {
136
		if ( count($this->parts()) == 1 ) {
137
			return $this->part(0)->content();
138
		}
139
140
		return '';
141
	}
142
143
	public function delete() {
144
		return $this->mailbox()->imap()->delete($this->msgNumber);
145
	}
146
147
	public function simpleStructure() {
148
		$parts = [];
149
		foreach ( $this->allParts(true) as $part ) {
150
			$name = '';
151
152
			$name .= implode('.', $part->section()) . '. ';
153
			if ( $part->parts() ) {
154
				$name .= '*';
155
			}
156
			$name .= $part->subtype();
157
			if ( $bytes = $part->parameter('bytes') ) {
158
				$name .= ' (' . $bytes . ')';
159
			}
160
161
			$parts[] = $name;
162
		}
163
164
		return $parts;
165
	}
166
167
	public function msgNumber() {
168
		return $this->msgNumber;
169
	}
170
171
	public function mailbox() {
172
		return $this->mailbox;
173
	}
174
175
}
176