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

IMAPMessagePart::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
namespace rdx\imap;
4
5
use rdx\imap\IMAPMessage;
6
use rdx\imap\IMAPMessagePart;
7
8
class IMAPMessagePart implements IMAPMessagePartInterface {
9
10
	protected $section = [];
11
	protected $subtype = '';
12
13
	protected $parts = [];
14
	protected $parameters = [];
15
16
	protected $message; // typeof IMAPMessage
17
	protected $structure; // typeof stdClass
18
	protected $skippedParts = []; // Array<stdClass>
19
20
	public function __construct( IMAPMessage $message, $structure, array $section ) {
21
		$this->message = $message;
22
		$this->structure = $structure;
23
		$this->section = $section;
24
		$this->subtype = strtoupper($structure->subtype);
25
26
		$this->parts();
27
	}
28
29
	public function parts() {
30
		if ( !empty($this->structure->parts) ) {
31
			$parts = $this->structure->parts;
32
33
			while ( count($parts) == 1 && empty($parts[0]->bytes) && !empty($parts[0]->parts) ) {
34
				$this->skippedParts[] = $parts[0];
35
				$parts = $parts[0]->parts;
36
			}
37
38
			foreach ( $parts as $n => $part ) {
39
				$this->parts[] = $this->message()->createMessagePart(
40
					$part,
41
					array_merge($this->section(), [$n+1])
42
				);
43
			}
44
		}
45
	}
46
47 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...
48
		$parts = [];
49
		$iterate = function($message) use (&$iterate, &$parts, $withContainers) {
50
			foreach ( $message->parts() as $part ) {
51
				if ( $part->parts() ) {
52
					if ( $withContainers ) {
53
						$parts[] = $part;
54
					}
55
56
					$iterate($part);
57
				}
58
				else {
59
					$parts[] = $part;
60
				}
61
			}
62
		};
63
64
		$iterate($this);
65
		return $parts;
66
	}
67
68
	public function part( $index ) {
69
		$parts = $this->parts();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $parts is correct as $this->parts() (which targets rdx\imap\IMAPMessagePart::parts()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
70
		return @$parts[$index];
71
	}
72
73
	public function structure() {
74
		return $this->structure;
75
	}
76
77 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...
78
		if ( empty($this->parameters) ) {
79
			$structure = $this->structure();
80
81
			$this->parameters['bytes'] = @$structure->bytes;
82
83
			foreach ((array) @$structure->parameters as $param) {
84
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
85
			}
86
			foreach ((array) @$structure->dparameters as $param) {
87
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
88
			}
89
		}
90
91
		return $this->parameters;
92
	}
93
94
	public function parameter( $name ) {
95
		$parameters = $this->parameters();
96
		return @$parameters[ strtolower($name) ];
97
	}
98
99
	public function content() {
100
		$body = $this->message()->mailbox()->imap()->fetchbody(
101
			$this->message()->msgNumber(),
102
			implode('.', $this->section())
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