Completed
Push — master ( 9d7197...6b758b )
by Rudie
02:04
created

IMAPMessage::subtype()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 3
eloc 5
nc 3
nop 0
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
19
	// protected $plainBody;
20
	// protected $HTMLBody;
21
	// protected $attachments = []; // Array<IMAPMessageAttachment>
22
23
	public function __construct( IMAPMailbox $mailbox, $msgNumber, $unseen = null ) {
24
		$this->mailbox = $mailbox;
25
		$this->msgNumber = $msgNumber;
26
		$this->unseen = $unseen;
27
28
		if ( $unseen === null ) {
29
			$this->unseen = (bool) trim($this->header('unseen'));
30
		}
31
	}
32
33
	protected function flags( $flags, $clear ) {
34
		$cb = [$this->mailbox()->imap(), $clear ? 'clearflag' : 'setflag'];
35
36
		$feedback = [];
37
		foreach ( (array)$flags AS $flag ) {
38
			$flag = '\\' . ucfirst($flag);
39
			$feedback[] = call_user_func($cb, $this->msgNumber, $flag);
40
		}
41
42
		return is_array($flags) ? $feedback : $feedback[0];
43
	}
44
45
	public function flag( $flags ) {
46
		return $this->flags($flags, false);
47
	}
48
49
	public function unflag( $flags ) {
50
		return $this->flags($flags, true);
51
	}
52
53
	public function utc() {
54
		return strtotime($this->header('date'));
55
	}
56
57
	public function subject() {
58
		if ( empty($this->subject) ) {
59
			$subject = $this->mailbox()->imap()->utf8($this->header('subject'));
60
			$this->subject = trim($subject);
61
		}
62
63
		return $this->subject;
64
	}
65
66
	public function headers() {
67
		if ( empty($this->headers) ) {
68
			$headers = $this->mailbox()->imap()->headerinfo($this->msgNumber);
69
			foreach ( $headers as $name => $value ) {
70
				$this->headers[ strtolower($name) ] = $value;
71
			}
72
		}
73
74
		return $this->headers;
75
	}
76
77
	public function header( $name ) {
78
		$headers = $this->headers();
79
		return @$headers[ strtolower($name) ];
80
	}
81
82
	public function createMessagePart( $structure, $section ) {
83
		return new IMAPMessagePart($this, $structure, $section);
84
	}
85
86
	public function parts() {
87
		if ( empty($this->parts) ) {
88
			$structure = $this->structure();
89
90
			// Possibilities:
91
			// - PLAIN
92
			// - ALTERNATIVE
93
			// - MIXED
94
			// - DELIVERY-STATUS
95
			// - RFC822
96
			// - REPORT
97
			// - HTML
98
			// - CALENDAR
99
			// - JPEG
100
101
			if ( empty($structure->parts) ) {
102
				$this->parts[] = $this->createMessagePart(
103
					$structure,
104
					[1]
105
				);
106
			}
107
			else {
108
				foreach ($structure->parts as $n => $part) {
109
					$this->parts[] = $this->createMessagePart(
110
						$part,
111
						[$n+1]
112
					);
113
				}
114
			}
115
		}
116
117
		return $this->parts;
118
	}
119
120
	public function structure() {
121
		if ( empty($this->structure) ) {
122
			$this->structure = $this->mailbox()->imap()->fetchstructure($this->msgNumber);
123
		}
124
125
		return $this->structure;
126
	}
127
128
	public function subtype() {
129
		if ( empty($this->subtype) ) {
130
			$structure = $this->structure();
131
			$this->subtype = @$structure->subtype ?: '';
0 ignored issues
show
Bug introduced by
The property subtype does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
132
		}
133
134
		return $this->subtype;
135
	}
136
137
	public function section() {
138
		return [];
139
	}
140
141
	public function content() {
142
		if ( count($this->parts()) == 1 ) {
143
			return $this->part(0)->content();
144
		}
145
146
		return '';
147
	}
148
149
	public function delete() {
150
		return $this->mailbox()->imap()->delete($this->msgNumber);
151
	}
152
153
	public function simpleStructure() {
154
		$parts = [];
155
		foreach ( $this->allParts(true) as $part ) {
156
			$name = '';
157
158
			$name .= implode('.', $part->section()) . '. ';
159
			if ( $part->parts() ) {
160
				$name .= '*';
161
			}
162
			$name .= $part->subtype();
163
			if ( $bytes = $part->parameter('bytes') ) {
164
				$name .= ' (' . $bytes . ')';
165
			}
166
167
			$parts[] = $name;
168
		}
169
170
		return $parts;
171
	}
172
173
	public function msgNumber() {
174
		return $this->msgNumber;
175
	}
176
177
	public function mailbox() {
178
		return $this->mailbox;
179
	}
180
181
}
182