Passed
Push — 2.0 ( 00e111...1e45aa )
by Zaahid
03:30
created

NonMimeParser::canParse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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;
8
9
use ZBateson\MailMimeParser\Parser\Part\UUEncodedPartHeaderContainerFactory;
10
use ZBateson\MailMimeParser\Parser\Proxy\ParserMimePartProxy;
11
use ZBateson\MailMimeParser\Parser\Proxy\ParserNonMimeMessageProxy;
12
use ZBateson\MailMimeParser\Parser\Proxy\ParserNonMimeMessageProxyFactory;
13
use ZBateson\MailMimeParser\Parser\Proxy\ParserPartProxy;
14
use ZBateson\MailMimeParser\Parser\Proxy\ParserUUEncodedPartProxyFactory;
15
16
/**
17
 * Parses content for non-mime messages and uu-encoded child parts.
18
 *
19
 * @author Zaahid Bateson
20
 */
21
class NonMimeParser extends AbstractParser
22
{
23
    /**
24
     * @var UUEncodedPartHeaderContainerFactory
25
     */
26
    protected $partHeaderContainerFactory;
27
28 9
    public function __construct(
29
        ParserNonMimeMessageProxyFactory $parserNonMimeMessageProxyFactory,
30
        ParserUUEncodedPartProxyFactory $parserUuEncodedPartProxyFactory,
31
        PartBuilderFactory $partBuilderFactory,
32
        UUEncodedPartHeaderContainerFactory $uuEncodedPartHeaderContainerFactory
33
    ) {
34 9
        parent::__construct($parserNonMimeMessageProxyFactory, $parserUuEncodedPartProxyFactory, $partBuilderFactory);
35 9
        $this->partHeaderContainerFactory = $uuEncodedPartHeaderContainerFactory;
36 9
    }
37
38
    /**
39
     * Always returns true, and should therefore be the last parser reached by
40
     * a ParserManager.
41
     *
42
     * @param PartBuilder $part
43
     * @return bool
44
     */
45 6
    public function canParse(PartBuilder $part)
46
    {
47 6
        return true;
48
    }
49
50
    /**
51
     * Creates a UUEncodedPartHeaderContainer attached to a PartBuilder, and
52
     * calls $this->parserManager->createParserProxyFor().
53
     *
54
     * It also sets the PartBuilder's stream part start pos and content start
55
     * pos to that of $parent->getNextParStart() (since a 'begin' line is read
56
     * prior to another child being created, see parseNextPart()).
57
     *
58
     * @param ParserNonMimeMessageProxy $parent
59
     * @return ParserPartProxy
60
     */
61 4
    private function createPart(ParserNonMimeMessageProxy $parent)
62
    {
63 4
        $hc = $this->partHeaderContainerFactory->newInstance($parent->getNextPartMode(), $parent->getNextPartFilename());
64 4
        $pb = $this->partBuilderFactory->newChildPartBuilder($hc, $parent);
65 4
        $proxy = $this->parserManager->createParserProxyFor($pb);
66 4
        $pb->setStreamPartStartPos($parent->getNextPartStart());
67 4
        $pb->setStreamContentStartPos($parent->getNextPartStart());
68 4
        return $proxy;
69
    }
70
71
    /**
72
     * Reads content from the passed ParserPartProxy's stream till a uu-encoded
73
     * 'begin' line is found, setting $proxy->setStreamPartContentAndEndPos() to
74
     * the last byte read before the begin line.
75
     *
76
     * @param ParserNonMimeMessageProxy|ParserUUEncodedPartProxy $proxy
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\...arserUUEncodedPartProxy 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...
77
     */
78 7
    private function parseNextPart(ParserPartProxy $proxy)
79
    {
80 7
        $handle = $proxy->getMessageResourceHandle();
81 7
        while (!feof($handle)) {
82 7
            $start = ftell($handle);
83 7
            $line = trim(MessageParser::readLine($handle));
84 7
            if (preg_match('/^begin ([0-7]{3}) (.*)$/', $line, $matches)) {
85 4
                $proxy->setNextPartStart($start);
0 ignored issues
show
Bug introduced by
The method setNextPartStart() does not exist on ZBateson\MailMimeParser\...r\Proxy\ParserPartProxy. It seems like you code against a sub-type of ZBateson\MailMimeParser\...r\Proxy\ParserPartProxy such as ZBateson\MailMimeParser\...arserUUEncodedPartProxy or ZBateson\MailMimeParser\...rserNonMimeMessageProxy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
                $proxy->/** @scrutinizer ignore-call */ 
86
                        setNextPartStart($start);
Loading history...
86 4
                $proxy->setNextPartMode($matches[1]);
0 ignored issues
show
Bug introduced by
The method setNextPartMode() does not exist on ZBateson\MailMimeParser\...r\Proxy\ParserPartProxy. It seems like you code against a sub-type of ZBateson\MailMimeParser\...r\Proxy\ParserPartProxy such as ZBateson\MailMimeParser\...arserUUEncodedPartProxy or ZBateson\MailMimeParser\...rserNonMimeMessageProxy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
                $proxy->/** @scrutinizer ignore-call */ 
87
                        setNextPartMode($matches[1]);
Loading history...
87 4
                $proxy->setNextPartFilename($matches[2]);
0 ignored issues
show
Bug introduced by
The method setNextPartFilename() does not exist on ZBateson\MailMimeParser\...r\Proxy\ParserPartProxy. It seems like you code against a sub-type of ZBateson\MailMimeParser\...r\Proxy\ParserPartProxy such as ZBateson\MailMimeParser\...arserUUEncodedPartProxy or ZBateson\MailMimeParser\...rserNonMimeMessageProxy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
                $proxy->/** @scrutinizer ignore-call */ 
88
                        setNextPartFilename($matches[2]);
Loading history...
88 4
                return;
89
            }
90 7
            $proxy->setStreamPartAndContentEndPos(ftell($handle));
91
        }
92 6
    }
93
94 8
    public function parseContent(ParserPartProxy $proxy)
95
    {
96 8
        $handle = $proxy->getMessageResourceHandle();
97 8
        if ($proxy->getNextPartStart() !== null || feof($handle)) {
0 ignored issues
show
Bug introduced by
The method getNextPartStart() does not exist on ZBateson\MailMimeParser\...r\Proxy\ParserPartProxy. It seems like you code against a sub-type of ZBateson\MailMimeParser\...r\Proxy\ParserPartProxy such as ZBateson\MailMimeParser\...arserUUEncodedPartProxy or ZBateson\MailMimeParser\...rserNonMimeMessageProxy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        if ($proxy->/** @scrutinizer ignore-call */ getNextPartStart() !== null || feof($handle)) {
Loading history...
98 2
            return;
99
        }
100 7
        if ($proxy->getStreamContentStartPos() === null) {
0 ignored issues
show
introduced by
The condition $proxy->getStreamContentStartPos() === null is always false.
Loading history...
101 7
            $proxy->setStreamContentStartPos(ftell($handle));
102
        }
103 7
        $this->parseNextPart($proxy);
104 7
    }
105
106 6
    public function parseNextChild(ParserMimePartProxy $proxy)
107
    {
108 6
        $handle = $proxy->getMessageResourceHandle();
109 6
        if ($proxy->getNextPartStart() === null || feof($handle)) {
0 ignored issues
show
Bug introduced by
The method getNextPartStart() does not exist on ZBateson\MailMimeParser\...oxy\ParserMimePartProxy. It seems like you code against a sub-type of ZBateson\MailMimeParser\...oxy\ParserMimePartProxy such as ZBateson\MailMimeParser\...rserNonMimeMessageProxy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        if ($proxy->/** @scrutinizer ignore-call */ getNextPartStart() === null || feof($handle)) {
Loading history...
110 5
            return null;
111
        }
112 4
        $child = $this->createPart(
113 4
            $proxy
114
        );
115 4
        $proxy->clearNextPart();
0 ignored issues
show
Bug introduced by
The method clearNextPart() does not exist on ZBateson\MailMimeParser\...oxy\ParserMimePartProxy. It seems like you code against a sub-type of ZBateson\MailMimeParser\...oxy\ParserMimePartProxy such as ZBateson\MailMimeParser\...rserNonMimeMessageProxy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        $proxy->/** @scrutinizer ignore-call */ 
116
                clearNextPart();
Loading history...
116 4
        return $child;
117
    }
118
}
119