Passed
Pull Request — master (#171)
by Zaahid
07:22 queued 03:34
created

NonMimeParser::parseNextPart()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

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

86
                $proxy->/** @scrutinizer ignore-call */ 
87
                        setNextPartStart($start);
Loading history...
87 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

87
                $proxy->/** @scrutinizer ignore-call */ 
88
                        setNextPartMode($matches[1]);
Loading history...
88 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

88
                $proxy->/** @scrutinizer ignore-call */ 
89
                        setNextPartFilename($matches[2]);
Loading history...
89 4
                return;
90
            }
91 7
            $proxy->setStreamPartAndContentEndPos(ftell($handle));
92
        }
93 6
    }
94
95 8
    public function parseContent(ParserPartProxy $proxy)
96
    {
97 8
        $handle = $proxy->getMessageResourceHandle();
98 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

98
        if ($proxy->/** @scrutinizer ignore-call */ getNextPartStart() !== null || feof($handle)) {
Loading history...
99 2
            return;
100
        }
101 7
        if ($proxy->getStreamContentStartPos() === null) {
0 ignored issues
show
introduced by
The condition $proxy->getStreamContentStartPos() === null is always false.
Loading history...
102 7
            $proxy->setStreamContentStartPos(ftell($handle));
103
        }
104 7
        $this->parseNextPart($proxy);
105 7
    }
106
107 6
    public function parseNextChild(ParserMimePartProxy $proxy)
108
    {
109 6
        $handle = $proxy->getMessageResourceHandle();
110 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

110
        if ($proxy->/** @scrutinizer ignore-call */ getNextPartStart() === null || feof($handle)) {
Loading history...
111 5
            return null;
112
        }
113 4
        $child = $this->createPart(
114 4
            $proxy
115
        );
116 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

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