Passed
Push — master ( 46ed75...ca2387 )
by Zaahid
03:33
created

NonMimeParserService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 4
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
8
namespace ZBateson\MailMimeParser\Parser;
9
10
use ZBateson\MailMimeParser\Parser\Part\UUEncodedPartHeaderContainerFactory;
11
use ZBateson\MailMimeParser\Parser\Proxy\ParserMimePartProxy;
12
use ZBateson\MailMimeParser\Parser\Proxy\ParserNonMimeMessageProxy;
13
use ZBateson\MailMimeParser\Parser\Proxy\ParserNonMimeMessageProxyFactory;
14
use ZBateson\MailMimeParser\Parser\Proxy\ParserPartProxy;
15
use ZBateson\MailMimeParser\Parser\Proxy\ParserUUEncodedPartProxy;
16
use ZBateson\MailMimeParser\Parser\Proxy\ParserUUEncodedPartProxyFactory;
17
18
/**
19
 * Parses content for non-mime messages and uu-encoded child parts.
20
 *
21
 * @author Zaahid Bateson
22
 */
23
class NonMimeParserService extends AbstractParserService
24
{
25
    /**
26
     * @var UUEncodedPartHeaderContainerFactory
27
     */
28
    protected UUEncodedPartHeaderContainerFactory $partHeaderContainerFactory;
29
30 9
    public function __construct(
31
        ParserNonMimeMessageProxyFactory $parserNonMimeMessageProxyFactory,
32
        ParserUUEncodedPartProxyFactory $parserUuEncodedPartProxyFactory,
33
        PartBuilderFactory $partBuilderFactory,
34
        UUEncodedPartHeaderContainerFactory $uuEncodedPartHeaderContainerFactory
35
    ) {
36 9
        parent::__construct($parserNonMimeMessageProxyFactory, $parserUuEncodedPartProxyFactory, $partBuilderFactory);
37 9
        $this->partHeaderContainerFactory = $uuEncodedPartHeaderContainerFactory;
38
    }
39
40
    /**
41
     * Always returns true, and should therefore be the last parser reached by
42
     * a ParserManager.
43
     */
44 6
    public function canParse(PartBuilder $part) : bool
45
    {
46 6
        return true;
47
    }
48
49
    /**
50
     * Creates a UUEncodedPartHeaderContainer attached to a PartBuilder, and
51
     * calls $this->parserManager->createParserProxyFor().
52
     *
53
     * It also sets the PartBuilder's stream part start pos and content start
54
     * pos to that of $parent->getNextParStart() (since a 'begin' line is read
55
     * prior to another child being created, see parseNextPart()).
56
     */
57 4
    private function createPart(ParserNonMimeMessageProxy $parent) : ParserPartProxy
58
    {
59 4
        $hc = $this->partHeaderContainerFactory->newInstance($parent->getNextPartMode(), $parent->getNextPartFilename());
0 ignored issues
show
Bug introduced by
It seems like $parent->getNextPartFilename() can also be of type null; however, parameter $filename of ZBateson\MailMimeParser\...rFactory::newInstance() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        $hc = $this->partHeaderContainerFactory->newInstance($parent->getNextPartMode(), /** @scrutinizer ignore-type */ $parent->getNextPartFilename());
Loading history...
Bug introduced by
It seems like $parent->getNextPartMode() can also be of type null; however, parameter $mode of ZBateson\MailMimeParser\...rFactory::newInstance() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        $hc = $this->partHeaderContainerFactory->newInstance(/** @scrutinizer ignore-type */ $parent->getNextPartMode(), $parent->getNextPartFilename());
Loading history...
60 4
        $pb = $this->partBuilderFactory->newChildPartBuilder($hc, $parent);
61 4
        $proxy = $this->parserManager->createParserProxyFor($pb);
62 4
        $pb->setStreamPartStartPos($parent->getNextPartStart());
0 ignored issues
show
Bug introduced by
It seems like $parent->getNextPartStart() can also be of type null; however, parameter $streamPartStartPos of ZBateson\MailMimeParser\...setStreamPartStartPos() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

62
        $pb->setStreamPartStartPos(/** @scrutinizer ignore-type */ $parent->getNextPartStart());
Loading history...
63 4
        $pb->setStreamContentStartPos($parent->getNextPartStart());
0 ignored issues
show
Bug introduced by
It seems like $parent->getNextPartStart() can also be of type null; however, parameter $streamContentStartPos of ZBateson\MailMimeParser\...StreamContentStartPos() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

63
        $pb->setStreamContentStartPos(/** @scrutinizer ignore-type */ $parent->getNextPartStart());
Loading history...
64 4
        return $proxy;
65
    }
66
67
    /**
68
     * Reads content from the passed ParserPartProxy's stream till a uu-encoded
69
     * 'begin' line is found, setting $proxy->setStreamPartContentAndEndPos() to
70
     * the last byte read before the begin line.
71
     *
72
     * @param ParserNonMimeMessageProxy|ParserUUEncodedPartProxy $proxy
73
     */
74 7
    private function parseNextPart(ParserPartProxy $proxy) : static
75
    {
76 7
        $handle = $proxy->getMessageResourceHandle();
77 7
        while (!\feof($handle)) {
78 7
            $start = \ftell($handle);
79 7
            $line = \trim(MessageParserService::readLine($handle));
80 7
            if (\preg_match('/^begin ([0-7]{3}) (.*)$/', $line, $matches)) {
81 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

81
                $proxy->/** @scrutinizer ignore-call */ 
82
                        setNextPartStart($start);
Loading history...
82 4
                $proxy->setNextPartMode((int) $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

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

83
                $proxy->/** @scrutinizer ignore-call */ 
84
                        setNextPartFilename($matches[2]);
Loading history...
84 4
                return $this;
85
            }
86 7
            $proxy->setStreamPartAndContentEndPos(\ftell($handle));
87
        }
88 6
        return $this;
89
    }
90
91 8
    public function parseContent(ParserPartProxy $proxy) : static
92
    {
93 8
        $handle = $proxy->getMessageResourceHandle();
94 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

94
        if ($proxy->/** @scrutinizer ignore-call */ getNextPartStart() !== null || \feof($handle)) {
Loading history...
95 2
            return $this;
96
        }
97 7
        if ($proxy->getStreamContentStartPos() === null) {
98 7
            $proxy->setStreamContentStartPos(\ftell($handle));
99
        }
100 7
        $this->parseNextPart($proxy);
101 7
        return $this;
102
    }
103
104 6
    public function parseNextChild(ParserMimePartProxy $proxy) : ?ParserPartProxy
105
    {
106 6
        $handle = $proxy->getMessageResourceHandle();
107 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

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

111
        $proxy->/** @scrutinizer ignore-call */ 
112
                clearNextPart();
Loading history...
112 4
        return $child;
113
    }
114
}
115