Completed
Push — master ( f7ad33...cd335b )
by Rudie
02:05
created

IMAPMessage::parts()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.5806
cc 4
eloc 15
nc 3
nop 0
1
<?php
2
3
namespace rdx\imap;
4
5
class IMAPMessage implements IMAPMessagePartInterface {
6
7
	public $mailbox; // typeof IMAPMailbox
8
9
	public $msgNumber = 1; // starts at 1, not 0
10
	public $header = '';
11
	public $unseen = true;
12
13
	public $headers; // typeof stdClass
14
	public $structure; // typeof stdClass
15
16
	public $subject = '';
17
	public $parts = array();
18
	public $parameters = array();
19
	public $plainBody;
20
	public $HTMLBody;
21
	public $attachments = array(); // typeof Array<IMAPMessageAttachment>
22
23
	public function __construct( IMAPMailbox $mailbox, $msgNumber, $header, $unseen ) {
24
		$this->mailbox = $mailbox;
25
		$this->msgNumber = $msgNumber;
26
		$this->header = $header;
27
		$this->unseen = $unseen;
28
	}
29
30
	protected function flags( $flags, $clear ) {
31
		$cb = $clear ? 'imap_clearflag_full' : 'imap_setflag_full';
32
33
		$feedback = array();
34
		foreach ( (array)$flags AS $flag ) {
35
			$flag = '\\' . ucfirst($flag);
36
			$feedback[] = $cb($this->mailbox->imap(), (string)$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
		$headers = $this->headers();
52
		return strtotime($headers->Date);
53
	}
54
55
	public function subject() {
56
		if ( !$this->subject ) {
57
			$headers = $this->headers();
58
59
			$subject = imap_utf8($headers->Subject);
60
61
			$this->subject = trim($subject);
62
		}
63
64
		return $this->subject;
65
	}
66
67
	public function headers() {
68
		if ( !$this->headers ) {
69
			$this->headers = imap_headerinfo($this->mailbox->imap(), $this->msgNumber);
70
		}
71
72
		return $this->headers;
73
	}
74
75
	public function parts() {
76
		if ( !$this->parts ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->parts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
77
			$structure = $this->structure();
78
79
			// Possibilities:
80
			// - PLAIN
81
			// - ALTERNATIVE
82
			// - MIXED
83
			// - DELIVERY-STATUS
84
			// - RFC822
85
			// - REPORT
86
			// - HTML
87
			// - CALENDAR
88
89
			if ( empty($structure->parts) ) {
90
				$this->parts[] = new IMAPMessagePart(
91
					$this,
92
					$structure,
93
					[1]
94
				);
95
			}
96
			else {
97
				foreach ($structure->parts as $n => $part) {
98
					$this->parts[] = new IMAPMessagePart(
99
						$this,
100
						$part,
101
						[$n+1]
102
					);
103
				}
104
			}
105
		}
106
107
		return $this->parts;
108
	}
109
110 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...
111
		$parts = [];
112
		$iterate = function($message) use (&$iterate, &$parts, $withContainers) {
113
			foreach ( $message->parts() as $part ) {
114
				if ( $part->parts() ) {
115
					if ( $withContainers ) {
116
						$parts[] = $part;
117
					}
118
119
					$iterate($part);
120
				}
121
				else {
122
					$parts[] = $part;
123
				}
124
			}
125
		};
126
127
		$iterate($this);
128
		return $parts;
129
	}
130
131
	public function part( $index ) {
132
		$parts = $this->parts();
133
		return @$parts[$index];
134
	}
135
136
	public function structure() {
137
		if ( !$this->structure ) {
138
			$this->structure = imap_fetchstructure($this->mailbox->imap(), $this->msgNumber);
139
		}
140
141
		return $this->structure;
142
	}
143
144
	public function content() {
145
		if ( count($this->parts()) == 1 ) {
146
			$parts = $this->part(0)->content();
0 ignored issues
show
Unused Code introduced by
$parts is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
147
		}
148
149
		return '';
150
	}
151
152 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...
153
		if ( !$this->parameters ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->parameters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
154
			$structure = $this->structure();
155
156
			$this->parameters['bytes'] = @$structure->bytes;
157
158
			foreach ((array) @$structure->parameters as $param) {
159
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
160
			}
161
			foreach ((array) @$structure->dparameters as $param) {
162
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
163
			}
164
		}
165
166
		return $this->parameters;
167
	}
168
169
	public function simpleStructure() {
170
		$parts = [];
171
		foreach ( $this->allParts(true) as $part ) {
172
			$name = '';
173
174
			$name .= implode('.', $part->section) . '. ';
175
			if ( $part->parts() ) {
176
				$name .= '*';
177
			}
178
			$name .= $part->subtype;
179
			if ( $bytes = $part->parameter('bytes') ) {
180
				$name .= ' (' . $bytes . ')';
181
			}
182
183
			$parts[] = $name;
184
		}
185
186
		return $parts;
187
	}
188
189
}
190