Completed
Push — master ( 070c71...411399 )
by Rudie
02:07
created

IMAPMessagePart::subtype()   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 0
1
<?php
2
3
namespace rdx\imap;
4
5
class IMAPMessagePart implements IMAPMessagePartInterface {
6
7
	protected $section = [];
8
	protected $subtype = '';
9
10
	protected $parts = [];
11
	protected $parameters = [];
12
13
	protected $message; // typeof IMAPMessage
14
	protected $structure; // typeof stdClass
15
	protected $skippedParts = []; // Array<stdClass>
16
17
	public function __construct( IMAPMessage $message, $structure, array $section ) {
18
		$this->message = $message;
19
		$this->structure = $structure;
20
		$this->section = $section;
21
		$this->subtype = strtoupper($structure->subtype);
22
23
		if ( !empty($structure->parts) ) {
24
			$parts = $structure->parts;
25
26
			while ( count($parts) == 1 && empty($parts[0]->bytes) && !empty($parts[0]->parts) ) {
27
				$this->skippedParts[] = $parts[0];
28
				$parts = $parts[0]->parts;
29
			}
30
31
			foreach ( $parts as $n => $part ) {
32
				$this->parts[] = new IMAPMessagePart(
33
					$this->message(),
34
					$part,
35
					array_merge($this->section(), [$n+1])
36
				);
37
			}
38
		}
39
	}
40
41
	public function parts() {
42
		return $this->parts;
43
	}
44
45 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...
46
		$parts = [];
47
		$iterate = function($message) use (&$iterate, &$parts, $withContainers) {
48
			foreach ( $message->parts() as $part ) {
49
				if ( $part->parts() ) {
50
					if ( $withContainers ) {
51
						$parts[] = $part;
52
					}
53
54
					$iterate($part);
55
				}
56
				else {
57
					$parts[] = $part;
58
				}
59
			}
60
		};
61
62
		$iterate($this);
63
		return $parts;
64
	}
65
66
	public function part( $index ) {
67
		$parts = $this->parts();
68
		return @$parts[$index];
69
	}
70
71
	public function structure() {
72
		return $this->structure;
73
	}
74
75 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...
76
		if ( empty($this->parameters) ) {
77
			$structure = $this->structure();
78
79
			$this->parameters['bytes'] = @$structure->bytes;
80
81
			foreach ((array) @$structure->parameters as $param) {
82
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
83
			}
84
			foreach ((array) @$structure->dparameters as $param) {
85
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
86
			}
87
		}
88
89
		return $this->parameters;
90
	}
91
92
	public function parameter( $name ) {
93
		$parameters = $this->parameters();
94
		return @$parameters[ strtolower($name) ];
95
	}
96
97
	public function content() {
98
		$body = imap_fetchbody(
99
			$this->message()->mailbox()->imap(),
100
			$this->message()->msgNumber(),
101
			implode('.', $this->section()),
102
			FT_PEEK
103
		);
104
		return $this->decode($body);
105
	}
106
107
	public function decode( $content ) {
108
		return quoted_printable_decode($content);
109
	}
110
111
	public function message() {
112
		return $this->message;
113
	}
114
115
	public function section() {
116
		return $this->section;
117
	}
118
119
	public function subtype() {
120
		return $this->subtype;
121
	}
122
123
}
124