Passed
Push — 1.0.0 ( d49389...cbb558 )
by Zaahid
03:21
created

MimePartFactory::newInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 2
dl 0
loc 16
ccs 14
cts 14
cp 1
crap 2
rs 9.8333
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\StreamFactory;
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 StreamFactory $sdf
37
     * @param PartStreamFilterManagerFactory $psf
38
     * @param HeaderFactory $hf
39
     * @param PartFilterFactory $pf
40
     */
41 1
    public function __construct(
42
        StreamFactory $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
     * Constructs a new MimePart object and returns it
54
     * 
55
     * @param PartBuilder $partBuilder
56
     * @param StreamInterface $messageStream
57
     * @return \ZBateson\MailMimeParser\Message\Part\MimePart
58
     */
59 1
    public function newInstance(PartBuilder $partBuilder, StreamInterface $messageStream = null)
60
    {
61 1
        $partStream = null;
62 1
        $contentStream = null;
63 1
        if ($messageStream !== null) {
64 1
            $partStream = $this->streamFactory->getLimitedPartStream($messageStream, $partBuilder);
65 1
            $contentStream = $this->streamFactory->getLimitedContentStream($messageStream, $partBuilder);
66
        }
67 1
        return new MimePart(
68 1
            $this->partStreamFilterManagerFactory->newInstance(),
69 1
            $this->streamFactory,
70 1
            $this->partFilterFactory,
71 1
            $this->headerFactory,
72 1
            $partBuilder,
73 1
            $partStream,
74 1
            $contentStream
75
        );
76
    }
77
}
78