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

MessageParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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