Test Failed
Push — 2.0 ( 4d4d2f...3431b8 )
by Zaahid
04:28
created

ParserMessageFactory::newInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 22
rs 9.7666
1
<?php
2
3
/**
4
 * This file is part of the ZBateson\MailMimeParser project.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 */
8
9
namespace ZBateson\MailMimeParser\Parser\Proxy;
10
11
use ZBateson\MailMimeParser\Message;
12
use ZBateson\MailMimeParser\Message\MessageService;
13
use ZBateson\MailMimeParser\Message\PartHeaderContainer;
14
use ZBateson\MailMimeParser\Message\Factory\PartHeaderContainerFactory;
15
use ZBateson\MailMimeParser\Parser\MimeParserFactory;
16
use ZBateson\MailMimeParser\Parser\NonMimeParserFactory;
17
use ZBateson\MailMimeParser\Parser\Part\ParsedPartChildrenContainerFactory;
18
use ZBateson\MailMimeParser\Parser\Part\ParsedPartStreamContainerFactory;
19
use ZBateson\MailMimeParser\Parser\PartBuilder;
20
use ZBateson\MailMimeParser\Stream\StreamFactory;
21
use Psr\Http\Message\StreamInterface;
22
23
/**
24
 * Responsible for creating ParsedMessage instances.
25
 *
26
 * @author Zaahid Bateson
27
 */
28
class ParserMessageFactory
29
{
30
        /**
31
     * @var StreamFactory the StreamFactory instance
32
     */
33
    protected $streamFactory;
34
35
    /**
36
     * @var PartHeaderContainerFactory
37
     */
38
    protected $partHeaderContainerFactory;
39
40
    /**
41
     * @var ParsedPartStreamContainerFactory
42
     */
43
    protected $parsedPartStreamContainerFactory;
44
45
    /**
46
     * @var ParsedPartChildrenContainerFactory
47
     */
48
    protected $parsedPartChildrenContainerFactory;
49
50
    /**
51
     * @var MessageService helper class for message manipulation routines.
52
     */
53
    protected $messageService;
54
55
    /**
56
     * @var ZBateson\MailMimeParser\Parser\IParserFactory[]
57
     */
58
    protected $parserFactories;
59
60
    public function __construct(
61
        StreamFactory $sdf,
62
        PartHeaderContainerFactory $phcf,
63
        ParsedPartStreamContainerFactory $pscf,
64
        ParsedPartChildrenContainerFactory $ppccf,
65
        MimeParserFactory $mpf,
66
        NonMimeParserFactory $nmpf,
67
        MessageService $mhs
68
    ) {
69
        $this->streamFactory = $sdf;
70
        $this->partHeaderContainerFactory = $phcf;
71
        $this->parsedPartStreamContainerFactory = $pscf;
72
        $this->parsedPartChildrenContainerFactory = $ppccf;
73
        $this->parserFactories = [ $mpf, $nmpf ];
0 ignored issues
show
Documentation Bug introduced by
It seems like array($mpf, $nmpf) of type array<integer,ZBateson\M...r\NonMimeParserFactory> is incompatible with the declared type ZBateson\MailMimeParser\...Parser\IParserFactory[] of property $parserFactories.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
        $this->messageService = $mhs;
75
    }
76
77
    public function prependMessageParser(IParser $parser)
0 ignored issues
show
Bug introduced by
The type ZBateson\MailMimeParser\Parser\Proxy\IParser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
78
    {
79
        array_unshift($this->parsers, $parser);
0 ignored issues
show
Bug Best Practice introduced by
The property parsers does not exist on ZBateson\MailMimeParser\...xy\ParserMessageFactory. Did you maybe forget to declare it?
Loading history...
80
    }
81
82
    protected function getMessageParser(PartHeaderContainer $container)
83
    {
84
        foreach ($this->parserFactories as $pf) {
85
            if ($pf->canParse($container)) {
86
                return $pf->newInstance();
87
            }
88
        }
89
        return null;
90
    }
91
92
    /**
93
     * Constructs a new IMessage object and returns it
94
     *
95
     * @param PartBuilder $partBuilder
96
     * @param StreamInterface $stream
97
     * @return \ZBateson\MailMimeParser\Message\IMimePart
98
     */
99
    public function newInstance(PartBuilder $partBuilder, PartHeaderContainer $headerContainer)
100
    {
101
        // changes to headers by the user can't affect parsing which could come
102
        // after a change to headers is made by the user on the Part
103
        $copied = $this->partHeaderContainerFactory->newInstance($headerContainer);
104
        $parserProxy = new ParserMessageProxy($copied, $partBuilder, $this->getMessageParser($headerContainer));
0 ignored issues
show
Bug introduced by
It seems like $this->getMessageParser($headerContainer) can also be of type null; however, parameter $parser of ZBateson\MailMimeParser\...ageProxy::__construct() does only seem to accept ZBateson\MailMimeParser\Parser\IParser, 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

104
        $parserProxy = new ParserMessageProxy($copied, $partBuilder, /** @scrutinizer ignore-type */ $this->getMessageParser($headerContainer));
Loading history...
105
        $streamContainer = $this->parsedPartStreamContainerFactory->newInstance($parserProxy);
106
        $childrenContainer = $this->parsedPartChildrenContainerFactory->newInstance($parserProxy);
107
108
        $message = new Message(
109
            $streamContainer,
110
            $headerContainer,
111
            $childrenContainer,
112
            $this->messageService
113
        );
114
        $parserProxy->setPart($message);
115
        $parserProxy->setParsedPartStreamContainer($streamContainer);
116
        $parserProxy->setParsedPartChildrenContainer($childrenContainer);
117
118
        $streamContainer->setStream($this->streamFactory->newMessagePartStream($message));
119
        $message->attach($streamContainer);
120
        return $message;
121
    }
122
123
}
124