NonMimeParserService::canParse()   A
last analyzed

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 0
Metric Value
eloc 1
c 0
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
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
    protected UUEncodedPartHeaderContainerFactory $partHeaderContainerFactory;
26
27 10
    public function __construct(
28
        ParserNonMimeMessageProxyFactory $parserNonMimeMessageProxyFactory,
29
        ParserUUEncodedPartProxyFactory $parserUuEncodedPartProxyFactory,
30
        PartBuilderFactory $partBuilderFactory,
31
        UUEncodedPartHeaderContainerFactory $uuEncodedPartHeaderContainerFactory
32
    ) {
33 10
        parent::__construct($parserNonMimeMessageProxyFactory, $parserUuEncodedPartProxyFactory, $partBuilderFactory);
34 10
        $this->partHeaderContainerFactory = $uuEncodedPartHeaderContainerFactory;
35
    }
36
37
    /**
38
     * Always returns true, and should therefore be the last parser reached by
39
     * a ParserManager.
40
     */
41 6
    public function canParse(PartBuilder $part) : bool
42
    {
43 6
        return true;
44
    }
45
46
    /**
47
     * Creates a UUEncodedPartHeaderContainer attached to a PartBuilder, and
48
     * calls $this->parserManager->createParserProxyFor().
49
     *
50
     * It also sets the PartBuilder's stream part start pos and content start
51
     * pos to that of $parent->getNextParStart() (since a 'begin' line is read
52
     * prior to another child being created, see parseNextPart()).
53
     */
54 4
    private function createPart(ParserNonMimeMessageProxy $parent) : ParserPartProxy
55
    {
56 4
        $hc = $this->partHeaderContainerFactory->newInstance($parent->getNextPartMode(), $parent->getNextPartFilename());
0 ignored issues
show
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

56
        $hc = $this->partHeaderContainerFactory->newInstance(/** @scrutinizer ignore-type */ $parent->getNextPartMode(), $parent->getNextPartFilename());
Loading history...
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

56
        $hc = $this->partHeaderContainerFactory->newInstance($parent->getNextPartMode(), /** @scrutinizer ignore-type */ $parent->getNextPartFilename());
Loading history...
57 4
        $pb = $this->partBuilderFactory->newChildPartBuilder($hc, $parent);
58 4
        $proxy = $this->parserManager->createParserProxyFor($pb);
59 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

59
        $pb->setStreamPartStartPos(/** @scrutinizer ignore-type */ $parent->getNextPartStart());
Loading history...
60 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

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

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

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

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

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

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

108
        $proxy->/** @scrutinizer ignore-call */ 
109
                clearNextPart();
Loading history...
109 4
        return $child;
110
    }
111
}
112