Completed
Push — 1.0.0 ( 608796...a0ab23 )
by Zaahid
04:48
created

MimePartFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
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\Message\Part\Factory;
8
9
use Psr\Http\Message\StreamInterface;
10
use ZBateson\MailMimeParser\Stream\StreamDecoratorFactory;
11
use ZBateson\MailMimeParser\Header\HeaderFactory;
12
use ZBateson\MailMimeParser\Message\PartFilterFactory;
13
use ZBateson\MailMimeParser\Message\Part\MimePart;
14
use ZBateson\MailMimeParser\Message\Part\PartBuilder;
15
16
/**
17
 * Responsible for creating MimePart instances.
18
 *
19
 * @author Zaahid Bateson
20
 */
21
class MimePartFactory extends MessagePartFactory
22
{
23
    /**
24
     * @var HeaderFactory an instance used for creating MimePart objects 
25
     */
26
    protected $headerFactory;
27
28
    /**
29
     * @var PartFilterFactory an instance used for creating MimePart objects
30
     */
31
    protected $partFilterFactory;
32
33
    /**
34
     * Initializes dependencies.
35
     *
36
     * @param StreamDecoratorFactory $sdf
37
     * @param PartStreamFilterManagerFactory $psf
38
     * @param HeaderFactory $hf
39
     * @param PartFilterFactory $pf
40
     */
41 1
    public function __construct(
42
        StreamDecoratorFactory $sdf,
43
        PartStreamFilterManagerFactory $psf,
44
        HeaderFactory $hf,
45
        PartFilterFactory $pf
46
    ) {
47 1
        parent::__construct($sdf, $psf);
48 1
        $this->headerFactory = $hf;
49 1
        $this->partFilterFactory = $pf;
50 1
    }
51
52
    /**
53
     * Returns the singleton instance for the class.
54
     *
55
     * @param StreamDecoratorFactory $sdf
56
     * @param PartStreamFilterManagerFactory $psf
57
     * @param HeaderFactory $hf
58
     * @param PartFilterFactory $pf
59
     * @return MessagePartFactory
60
     */
61
    public static function getInstance(
62
        StreamDecoratorFactory $sdf,
63
        PartStreamFilterManagerFactory $psf,
64
        HeaderFactory $hf = null,
65
        PartFilterFactory $pf = null
66
    ) {
67
        $instance = static::getCachedInstance();
68
        if ($instance === null) {
69
            $instance = new static($sdf, $psf, $hf, $pf);
0 ignored issues
show
Bug introduced by
It seems like $pf can also be of type null; however, parameter $pf of ZBateson\MailMimeParser\...tFactory::__construct() does only seem to accept ZBateson\MailMimeParser\Message\PartFilterFactory, 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

69
            $instance = new static($sdf, $psf, $hf, /** @scrutinizer ignore-type */ $pf);
Loading history...
Bug introduced by
It seems like $hf can also be of type null; however, parameter $hf of ZBateson\MailMimeParser\...tFactory::__construct() does only seem to accept ZBateson\MailMimeParser\Header\HeaderFactory, 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

69
            $instance = new static($sdf, $psf, /** @scrutinizer ignore-type */ $hf, $pf);
Loading history...
70
            static::setCachedInstance($instance);
71
        }
72
        return $instance;
73
    }
74
75
    /**
76
     * Constructs a new MimePart object and returns it
77
     * 
78
     * @param StreamInterface $messageStream
79
     * @param PartBuilder $partBuilder
80
     * @return \ZBateson\MailMimeParser\Message\Part\MimePart
81
     */
82 1
    public function newInstance(StreamInterface $messageStream, PartBuilder $partBuilder)
83
    {
84 1
        return new MimePart(
85 1
            $this->headerFactory,
86 1
            $this->partFilterFactory,
87 1
            $partBuilder,
88 1
            $this->partStreamFilterManagerFactory->newInstance(),
89 1
            $this->streamDecoratorFactory->getLimitedPartStream($messageStream, $partBuilder),
90 1
            $this->streamDecoratorFactory->getLimitedContentStream($messageStream, $partBuilder)
91
        );
92
    }
93
}
94