Test Failed
Push — 2.0 ( 4d4d2f...3431b8 )
by Zaahid
04:28
created

ParserMimePartProxy   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 186
rs 10
wmc 25

14 Methods

Rating   Name   Duplication   Size   Complexity  
A addChild() 0 4 1
A isEndBoundaryFound() 0 3 1
A parseAll() 0 7 2
A getHeaderContainer() 0 3 1
A isParentBoundaryFound() 0 3 1
A setEndBoundaryFound() 0 15 6
A setParsedPartChildrenContainer() 0 3 1
A isMultiPart() 0 11 2
A getContentType() 0 3 1
A setEof() 0 5 2
A ensureLastChildRead() 0 4 2
A getMimeBoundary() 0 10 3
A parseNextChild() 0 5 1
A __construct() 0 8 1
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Parser\Proxy;
8
9
use ZBateson\MailMimeParser\Message\PartHeaderContainer;
10
use ZBateson\MailMimeParser\Message\IMessagePart;
11
use ZBateson\MailMimeParser\Parser\IParser;
12
use ZBateson\MailMimeParser\Parser\PartBuilder;
13
use ZBateson\MailMimeParser\Parser\Part\ParsedPartChildrenContainer;
14
15
/**
16
 * Description of MimePartProxy
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class ParserMimePartProxy extends ParserPartProxy
21
{
22
    /**
23
     * @var PartHeaderContainer
24
     */
25
    protected $headerContainer;
26
27
    /**
28
     * @var boolean set to true once the end boundary of the currently-parsed
29
     *      part is found.
30
     */
31
    protected $endBoundaryFound = false;
32
33
    /**
34
     * @var boolean set to true once a boundary belonging to this parent's part
35
     *      is found.
36
     */
37
    protected $parentBoundaryFound = false;
38
39
    /**
40
     * @var boolean|null|string FALSE if not queried for in the content-type
41
     *      header of this part, NULL if the current part does not have a
42
     *      boundary, and otherwise contains the value of the boundary parameter
43
     *      of the content-type header if the part contains one.
44
     */
45
    protected $mimeBoundary = false;
46
47
    protected $lastAddedChild = null;
48
49
    protected $parsedPartChildrenContainer = null;
50
51
    public function __construct(
52
        PartHeaderContainer $headerContainer,
53
        PartBuilder $partBuilder,
54
        IParser $parser,
55
        ParserPartProxy $parent = null
56
    ) {
57
        parent::__construct($partBuilder, $parser, $parent);
58
        $this->headerContainer = $headerContainer;
59
    }
60
61
    public function setParsedPartChildrenContainer(ParsedPartChildrenContainer $parsedPartChildrenContainer)
62
    {
63
        $this->parsedPartChildrenContainer = $parsedPartChildrenContainer;
64
    }
65
66
    protected function ensureLastChildRead()
67
    {
68
        if ($this->lastAddedChild !== null) {
69
            $this->lastAddedChild->parseAll();
70
        }
71
    }
72
73
    public function parseNextChild()
74
    {
75
        $this->ensureLastChildRead();
76
        $this->parseContent();
77
        return $this->parser->parseNextChild($this);
78
    }
79
80
    public function parseAll()
81
    {
82
        $this->parseContent();
83
        $child = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $child is dead and can be removed.
Loading history...
84
        do {
85
            $child = $this->parseNextChild();
86
        } while ($child !== null);
87
    }
88
89
    public function addChild(ParserPartProxy $child)
90
    {
91
        $this->parsedPartChildrenContainer->add($child->getPart());
92
        $this->lastAddedChild = $child;
93
    }
94
95
    public function getHeaderContainer()
96
    {
97
        return $this->headerContainer;
98
    }
99
100
    /**
101
     * Returns a ParameterHeader representing the parsed Content-Type header for
102
     * this PartBuilder.
103
     *
104
     * @return \ZBateson\MailMimeParser\Header\ParameterHeader
105
     */
106
    public function getContentType()
107
    {
108
        return $this->headerContainer->get('Content-Type');
109
    }
110
111
    /**
112
     * Returns the parsed boundary parameter of the Content-Type header if set
113
     * for a multipart message part.
114
     *
115
     * @return string
116
     */
117
    public function getMimeBoundary()
118
    {
119
        if ($this->mimeBoundary === false) {
120
            $this->mimeBoundary = null;
121
            $contentType = $this->getContentType();
122
            if ($contentType !== null) {
123
                $this->mimeBoundary = $contentType->getValueFor('boundary');
124
            }
125
        }
126
        return $this->mimeBoundary;
127
    }
128
129
    /**
130
     * Returns true if this part's content-type is multipart/*
131
     *
132
     * @return boolean
133
     */
134
    public function isMultiPart()
135
    {
136
        $contentType = $this->getContentType();
137
        if ($contentType !== null) {
138
            // casting to bool, preg_match returns 1 for true
139
            return (bool) (preg_match(
140
                '~multipart/.*~i',
141
                $contentType->getValue()
142
            ));
143
        }
144
        return false;
145
    }
146
147
    /**
148
     * Returns true if the passed $line of read input matches this PartBuilder's
149
     * mime boundary, or any of its parent's mime boundaries for a multipart
150
     * message.
151
     *
152
     * If the passed $line is the ending boundary for the current PartBuilder,
153
     * $this->isEndBoundaryFound will return true after.
154
     *
155
     * @param string $line
156
     * @return boolean
157
     */
158
    public function setEndBoundaryFound($line)
159
    {
160
        $boundary = $this->getMimeBoundary();
161
        if ($this->parent !== null && $this->parent->setEndBoundaryFound($line)) {
162
            $this->parentBoundaryFound = true;
163
            return true;
164
        } elseif ($boundary !== null) {
0 ignored issues
show
introduced by
The condition $boundary !== null is always true.
Loading history...
165
            if ($line === "--$boundary--") {
166
                $this->endBoundaryFound = true;
167
                return true;
168
            } elseif ($line === "--$boundary") {
169
                return true;
170
            }
171
        }
172
        return false;
173
    }
174
175
    /**
176
     * Returns true if MessageParser passed an input line to setEndBoundary that
177
     * matches a parent's mime boundary, and the following input belongs to a
178
     * new part under its parent.
179
     *
180
     * @return boolean
181
     */
182
    public function isParentBoundaryFound()
183
    {
184
        return ($this->parentBoundaryFound);
185
    }
186
187
    /**
188
     *
189
     * @return type
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\Parser\Proxy\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
190
     */
191
    public function isEndBoundaryFound()
192
    {
193
        return ($this->endBoundaryFound);
194
    }
195
196
    /**
197
     * Called once EOF is reached while reading content.  The method sets the
198
     * flag used by PartBuilder::isParentBoundaryFound to true on this part and
199
     * all parent PartBuilders.
200
     */
201
    public function setEof()
202
    {
203
        $this->parentBoundaryFound = true;
204
        if ($this->parent !== null) {
205
            $this->parent->parentBoundaryFound = true;
206
        }
207
    }
208
}
209