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

MessageParserService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 21
c 0
b 0
f 0
dl 0
loc 74
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A readLine() 0 8 3
A parse() 0 10 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 Psr\Http\Message\StreamInterface;
11
use ZBateson\MailMimeParser\Message\Factory\PartHeaderContainerFactory;
12
use ZBateson\MailMimeParser\IMessage;
13
14
/**
15
 * Parses a mail mime message into its component parts.  To invoke, call
16
 * {@see MailMimeParser::parse()}.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
class MessageParserService
21
{
22
    /**
23
     * @var PartHeaderContainerFactory To create a container to read the
24
     *      message's headers into.
25
     */
26
    protected PartHeaderContainerFactory $partHeaderContainerFactory;
27
28
    /**
29
     * @var ParserManagerService To figure out what parser is responsible for parsing a
30
     *      message.
31
     */
32
    protected ParserManagerService $parserManager;
33
34
    /**
35
     * @var PartBuilderFactory To create a PartBuilder representing this
36
     *      message, and to pass it to ParserManager.
37
     */
38
    protected PartBuilderFactory $partBuilderFactory;
39
40
    /**
41
     * @var HeaderParserService To parse the headers into a PartHeaderContainer.
42
     */
43
    protected HeaderParserService $headerParser;
44
45 4
    public function __construct(
46
        PartBuilderFactory $pbf,
47
        PartHeaderContainerFactory $phcf,
48
        ParserManagerService $pm,
49
        HeaderParserService $hp
50
    ) {
51 4
        $this->partBuilderFactory = $pbf;
52 4
        $this->partHeaderContainerFactory = $phcf;
53 4
        $this->parserManager = $pm;
54 4
        $this->headerParser = $hp;
55
    }
56
57
    /**
58
     * Convenience method to read a line of up to 4096 characters from the
59
     * passed resource handle.
60
     *
61
     * If the line is larger than 4096 characters, the remaining characters in
62
     * the line are read and discarded, and only the first 4096 characters are
63
     * returned.
64
     *
65
     * @param resource $handle
66
     * @return string|false the read line or false on EOF or on error.
67
     */
68 107
    public static function readLine($handle) : string|false
69
    {
70 107
        $size = 4096;
71 107
        $ret = $line = \fgets($handle, $size);
72 107
        while (\strlen($line) === $size - 1 && \substr($line, -1) !== "\n") {
73 3
            $line = \fgets($handle, $size);
74
        }
75 107
        return $ret;
76
    }
77
78
    /**
79
     * Parses the passed stream into an {@see ZBateson\MailMimeParser\IMessage}
80
     * object and returns it.
81
     *
82
     * @param StreamInterface $stream the stream to parse the message from
83
     */
84 106
    public function parse(StreamInterface $stream) : IMessage
85
    {
86 106
        $headerContainer = $this->partHeaderContainerFactory->newInstance();
87 106
        $partBuilder = $this->partBuilderFactory->newPartBuilder($headerContainer, $stream);
88 106
        $this->headerParser->parse(
89 106
            $partBuilder->getMessageResourceHandle(),
90 106
            $headerContainer
91 106
        );
92 106
        $proxy = $this->parserManager->createParserProxyFor($partBuilder);
93 106
        return $proxy->getPart();
94
    }
95
}
96