ParserManagerService::prependParser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
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\Proxy\ParserPartProxy;
11
12
/**
13
 * Manages a prioritized list of IParser objects for parsing messages and parts
14
 * and creating proxied parts.
15
 *
16
 * The default ParserManager sets up a MimeParser in priority 0, and a
17
 * NonMimeParser in priority 1.
18
 *
19
 * @author Zaahid Bateson
20
 */
21
class ParserManagerService
22
{
23
    /**
24
     * @var IParserService[] List of parsers in order of priority (0 is highest
25
     *      priority).
26
     */
27
    protected array $parsers = [];
28
29 7
    public function __construct(MimeParserService $mimeParser, NonMimeParserService $nonMimeParser)
30
    {
31 7
        $this->setParsers([$mimeParser, $nonMimeParser]);
32
    }
33
34
    /**
35
     * Overrides the internal prioritized list of parses with the passed list,
36
     * calling $parser->setParserManager($this) on each one.
37
     *
38
     * @param IParserService[] $parsers
39
     */
40 7
    public function setParsers(array $parsers) : static
41
    {
42 7
        foreach ($parsers as $parser) {
43 7
            $parser->setParserManager($this);
44
        }
45 7
        $this->parsers = $parsers;
46 7
        return $this;
47
    }
48
49
    /**
50
     * Adds an IParser at the highest priority (up front), calling
51
     * $parser->setParserManager($this) on it.
52
     *
53
     * @param IParserService $parser The parser to add.
54
     */
55 1
    public function prependParser(IParserService $parser) : static
56
    {
57 1
        $parser->setParserManager($this);
58 1
        \array_unshift($this->parsers, $parser);
59 1
        return $this;
60
    }
61
62
    /**
63
     * Creates a ParserPartProxy for the passed $partBuilder using a compatible
64
     * IParser.
65
     *
66
     * Loops through registered IParsers calling 'canParse()' on each with the
67
     * passed PartBuilder, then calling either 'getParserMessageProxyFactory()'
68
     * or 'getParserPartProxyFactory()' depending on if the PartBuilder has a
69
     * parent, and finally calling 'newInstance' on the returned
70
     * ParserPartProxyFactory passing it the IParser, and returning the new
71
     * ParserPartProxy instance that was created.
72
     *
73
     * @param PartBuilder $partBuilder The PartBuilder to wrap in a proxy with
74
     *        an IParser
75
     * @throws CompatibleParserNotFoundException if a compatible parser for the
76
     *         type is not configured.
77
     * @return ParserPartProxy The created ParserPartProxy tied to a new
78
     *         IMessagePart and associated IParser.
79
     */
80 109
    public function createParserProxyFor(PartBuilder $partBuilder) : ParserPartProxy
81
    {
82 109
        foreach ($this->parsers as $parser) {
83 109
            if ($parser->canParse($partBuilder)) {
84 109
                $factory = ($partBuilder->getParent() === null) ?
85 108
                    $parser->getParserMessageProxyFactory() :
86 75
                    $parser->getParserPartProxyFactory();
87 109
                return $factory->newInstance($partBuilder, $parser);
88
            }
89
        }
90
        throw new CompatibleParserNotFoundException('Compatible parser for a part cannot be found with content-type: ' . $partBuilder->getHeaderContainer()->get('Content-Type'));
91
    }
92
}
93