Completed
Push — master ( c1617c...820ca9 )
by Rudie
02:11
created

IMAPMessage::createMessagePart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace rdx\imap;
4
5
use rdx\imap\IMAPMailbox;
6
use rdx\imap\IMAPMessagePart;
7
8
class IMAPMessage implements IMAPMessagePartInterface {
9
10
	protected $mailbox; // typeof IMAPMailbox
11
12
	protected $msgNumber = 1; // starts at 1, not 0
13
	protected $unseen = true;
14
15
	protected $headers = [];
16
	protected $structure; // typeof stdClass
17
18
	protected $subject = '';
19
	protected $parts = [];
20
	protected $parameters = [];
21
	protected $plainBody;
22
	protected $HTMLBody;
23
	protected $attachments = []; // typeof Array<IMAPMessageAttachment>
24
25
	public function __construct( IMAPMailbox $mailbox, $msgNumber, $unseen = null ) {
26
		$this->mailbox = $mailbox;
27
		$this->msgNumber = $msgNumber;
28
		$this->unseen = $unseen;
29
30
		if ( $unseen === null ) {
31
			$this->unseen = (bool) trim($this->header('unseen'));
32
		}
33
	}
34
35
	protected function flags( $flags, $clear ) {
36
		$cb = [$this->imap(), $clear ? 'clearflag' : 'setflag'];
0 ignored issues
show
Bug introduced by
The method imap() does not seem to exist on object<rdx\imap\IMAPMessage>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
38
		$feedback = [];
39
		foreach ( (array)$flags AS $flag ) {
40
			$flag = '\\' . ucfirst($flag);
41
			$feedback[] = call_user_func($cb, $this->msgNumber, $flag);
42
		}
43
44
		return is_array($flags) ? $feedback : $feedback[0];
45
	}
46
47
	public function flag( $flags ) {
48
		return $this->flags($flags, false);
49
	}
50
51
	public function unflag( $flags ) {
52
		return $this->flags($flags, true);
53
	}
54
55
	public function utc() {
56
		return strtotime($this->header('date'));
57
	}
58
59
	public function subject() {
60
		if ( empty($this->subject) ) {
61
			$subject = $this->mailbox()->imap()->utf8($this->header('subject'));
62
			$this->subject = trim($subject);
63
		}
64
65
		return $this->subject;
66
	}
67
68
	public function headers() {
69
		if ( empty($this->headers) ) {
70
			$headers = $this->mailbox()->imap()->headerinfo($this->msgNumber);
71
			foreach ( $headers as $name => $value ) {
72
				$this->headers[ strtolower($name) ] = $value;
73
			}
74
		}
75
76
		return $this->headers;
77
	}
78
79
	public function header( $name ) {
80
		$headers = $this->headers();
81
		return @$headers[ strtolower($name) ];
82
	}
83
84
	public function createMessagePart( $structure, $section ) {
85
		return new IMAPMessagePart($this, $structure, $section);
86
	}
87
88
	public function parts() {
89
		if ( empty($this->parts) ) {
90
			$structure = $this->structure();
91
92
			// Possibilities:
93
			// - PLAIN
94
			// - ALTERNATIVE
95
			// - MIXED
96
			// - DELIVERY-STATUS
97
			// - RFC822
98
			// - REPORT
99
			// - HTML
100
			// - CALENDAR
101
			// - JPEG
102
103
			if ( empty($structure->parts) ) {
104
				$this->parts[] = $this->createMessagePart(
105
					$structure,
106
					[1]
107
				);
108
			}
109
			else {
110
				foreach ($structure->parts as $n => $part) {
111
					$this->parts[] = $this->createMessagePart(
112
						$part,
113
						[$n+1]
114
					);
115
				}
116
			}
117
		}
118
119
		return $this->parts;
120
	}
121
122 View Code Duplication
	public function allParts( $withContainers = false ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
		$parts = [];
124
		$iterate = function($message) use (&$iterate, &$parts, $withContainers) {
125
			foreach ( $message->parts() as $part ) {
126
				if ( $part->parts() ) {
127
					if ( $withContainers ) {
128
						$parts[] = $part;
129
					}
130
131
					$iterate($part);
132
				}
133
				else {
134
					$parts[] = $part;
135
				}
136
			}
137
		};
138
139
		$iterate($this);
140
		return $parts;
141
	}
142
143
	public function part( $index ) {
144
		$parts = $this->parts();
145
		return @$parts[$index];
146
	}
147
148
	public function structure() {
149
		if ( empty($this->structure) ) {
150
			$this->structure = $this->mailbox()->imap()->fetchstructure($this->msgNumber);
151
		}
152
153
		return $this->structure;
154
	}
155
156
	public function content() {
157
		if ( count($this->parts()) == 1 ) {
158
			return $this->part(0)->content();
159
		}
160
161
		return '';
162
	}
163
164 View Code Duplication
	public function parameters() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
		if ( empty($this->parameters) ) {
166
			$structure = $this->structure();
167
168
			$this->parameters['bytes'] = @$structure->bytes;
169
170
			foreach ((array) @$structure->parameters as $param) {
171
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
172
			}
173
			foreach ((array) @$structure->dparameters as $param) {
174
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
175
			}
176
		}
177
178
		return $this->parameters;
179
	}
180
181
	public function simpleStructure() {
182
		$parts = [];
183
		foreach ( $this->allParts(true) as $part ) {
184
			$name = '';
185
186
			$name .= implode('.', $part->section()) . '. ';
187
			if ( $part->parts() ) {
188
				$name .= '*';
189
			}
190
			$name .= $part->subtype();
191
			if ( $bytes = $part->parameter('bytes') ) {
192
				$name .= ' (' . $bytes . ')';
193
			}
194
195
			$parts[] = $name;
196
		}
197
198
		return $parts;
199
	}
200
201
	public function msgNumber() {
202
		return $this->msgNumber;
203
	}
204
205
	public function mailbox() {
206
		return $this->mailbox;
207
	}
208
209
}
210