Passed
Push — 1.0.0 ( 4505d9...06b3ad )
by Zaahid
04:10
created

MessagePartFactory::getCachedInstance()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 12
rs 9.4285
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;
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
14
/**
15
 * Abstract factory for subclasses of MessagePart.
16
 *
17
 * @author Zaahid Bateson
18
 */
19
abstract class MessagePartFactory
20
{
21
    /**
22
     * @var PartStreamFilterManagerFactory responsible for creating
23
     *      PartStreamFilterManager instances
24
     */
25
    protected $partStreamFilterManagerFactory;
26
27
    /**
28
     * @var StreamDecoratorFactory the StreamDecoratorFactory instance
29
     */
30
    protected $streamDecoratorFactory;
31
32
    /**
33
     * @static MessagePartFactory[] cached instances of MessagePartFactory
34
     *      sub-classes
35
     */
36
    private static $instances = null;
37
38
    /**
39
     * Initializes class dependencies.
40
     *
41
     * @param StreamDecoratorFactory $streamDecoratorFactory
42
     * @param PartStreamFilterManagerFactory $psf
43
     */
44 3
    public function __construct(
45
        StreamDecoratorFactory $streamDecoratorFactory,
46
        PartStreamFilterManagerFactory $psf
47
    ) {
48 3
        $this->streamDecoratorFactory = $streamDecoratorFactory;
49 3
        $this->partStreamFilterManagerFactory = $psf;
50 3
    }
51
    
52
    /**
53
     * Sets a cached singleton instance.
54
     *
55
     * @param MessagePartFactory $instance
56
     */
57
    protected static function setCachedInstance(MessagePartFactory $instance)
58
    {
59
        if (self::$instances === null) {
60
            self::$instances = [];
61
        }
62
        $class = get_called_class();
63
        self::$instances[$class] = $instance;
64
    }
65
66
    /**
67
     * Returns a cached singleton instance if one exists, or null if one hasn't
68
     * been created yet.
69
     *
70
     * @return MessagePartFactory
71
     */
72
    protected static function getCachedInstance()
73
    {
74
        $class = get_called_class();
75
        if (self::$instances === null || !isset(self::$instances[$class])) {
76
            return null;
77
        }
78
        return self::$instances[$class];
79
    }
80
81
    /**
82
     * Returns the singleton instance for the class.
83
     *
84
     * @param StreamDecoratorFactory $sdf
85
     * @param PartStreamFilterManagerFactory $psf
86
     * @param HeaderFactory $hf
87
     * @param PartFilterFactory $pf
88
     * @return MessagePartFactory
89
     */
90
    public static function getInstance(
91
        StreamDecoratorFactory $sdf,
92
        PartStreamFilterManagerFactory $psf,
93
        HeaderFactory $hf = null,
0 ignored issues
show
Unused Code introduced by
The parameter $hf is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
        /** @scrutinizer ignore-unused */ HeaderFactory $hf = null,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
        PartFilterFactory $pf = null
0 ignored issues
show
Unused Code introduced by
The parameter $pf is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

94
        /** @scrutinizer ignore-unused */ PartFilterFactory $pf = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    ) {
96
        $instance = static::getCachedInstance();
97
        if ($instance === null) {
98
            $instance = new static($sdf, $psf);
99
            static::setCachedInstance($instance);
100
        }
101
        return $instance;
102
    }
103
104
    /**
105
     * Constructs a new MessagePart object and returns it
106
     * 
107
     * @param StreamInterface $messageStream
108
     * @param PartBuilder $partBuilder
109
     * @return \ZBateson\MailMimeParser\Message\Part\MessagePart
110
     */
111
    public abstract function newInstance(StreamInterface $messageStream, PartBuilder $partBuilder);
112
}
113