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

ParserManagerService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 1
b 0
f 0
dl 0
loc 69
ccs 18
cts 19
cp 0.9474
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createParserProxyFor() 0 11 4
A __construct() 0 3 1
A setParsers() 0 7 2
A prependParser() 0 5 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 6
    public function __construct(MimeParserService $mimeParser, NonMimeParserService $nonMimeParser)
30
    {
31 6
        $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
     */
39 6
    public function setParsers(array $parsers) : static
40
    {
41 6
        foreach ($parsers as $parser) {
42 6
            $parser->setParserManager($this);
43
        }
44 6
        $this->parsers = $parsers;
45 6
        return $this;
46
    }
47
48
    /**
49
     * Adds an IParser at the highest priority (up front), calling
50
     * $parser->setParserManager($this) on it.
51
     *
52
     * @param IParserService $parser The parser to add.
53
     */
54 1
    public function prependParser(IParserService $parser) : static
55
    {
56 1
        $parser->setParserManager($this);
57 1
        \array_unshift($this->parsers, $parser);
58 1
        return $this;
59
    }
60
61
    /**
62
     * Creates a ParserPartProxy for the passed $partBuilder using a compatible
63
     * IParser.
64
     *
65
     * Loops through registered IParsers calling 'canParse()' on each with the
66
     * passed PartBuilder, then calling either 'getParserMessageProxyFactory()'
67
     * or 'getParserPartProxyFactory()' depending on if the PartBuilder has a
68
     * parent, and finally calling 'newInstance' on the returned
69
     * ParserPartProxyFactory passing it the IParser, and returning the new
70
     * ParserPartProxy instance that was created.
71
     *
72
     * @param PartBuilder $partBuilder The PartBuilder to wrap in a proxy with
73
     *        an IParser
74
     * @throws CompatibleParserNotFoundException if a compatible parser for the
75
     *         type is not configured.
76
     * @return ParserPartProxy The created ParserPartProxy tied to a new
77
     *         IMessagePart and associated IParser.
78
     */
79 107
    public function createParserProxyFor(PartBuilder $partBuilder) : ParserPartProxy
80
    {
81 107
        foreach ($this->parsers as $parser) {
82 107
            if ($parser->canParse($partBuilder)) {
83 107
                $factory = ($partBuilder->getParent() === null) ?
84 106
                    $parser->getParserMessageProxyFactory() :
85 75
                    $parser->getParserPartProxyFactory();
86 107
                return $factory->newInstance($partBuilder, $parser);
87
            }
88
        }
89
        throw new CompatibleParserNotFoundException('Compatible parser for a part cannot be found with content-type: ' . $partBuilder->getHeaderContainer()->get('Content-Type'));
90
    }
91
}
92