Completed
Push — 1.0.0 ( 608796...a0ab23 )
by Zaahid
04:48
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\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\PartBuilder;
14
15
/**
16
 * Abstract factory for subclasses of MessagePart.
17
 *
18
 * @author Zaahid Bateson
19
 */
20
abstract class MessagePartFactory
21
{
22
    /**
23
     * @var PartStreamFilterManagerFactory responsible for creating
24
     *      PartStreamFilterManager instances
25
     */
26
    protected $partStreamFilterManagerFactory;
27
28
    /**
29
     * @var StreamDecoratorFactory the StreamDecoratorFactory instance
30
     */
31
    protected $streamDecoratorFactory;
32
33
    /**
34
     * @static MessagePartFactory[] cached instances of MessagePartFactory
35
     *      sub-classes
36
     */
37
    private static $instances = null;
38
39
    /**
40
     * Initializes class dependencies.
41
     *
42
     * @param StreamDecoratorFactory $streamDecoratorFactory
43
     * @param PartStreamFilterManagerFactory $psf
44
     */
45 3
    public function __construct(
46
        StreamDecoratorFactory $streamDecoratorFactory,
47
        PartStreamFilterManagerFactory $psf
48
    ) {
49 3
        $this->streamDecoratorFactory = $streamDecoratorFactory;
50 3
        $this->partStreamFilterManagerFactory = $psf;
51 3
    }
52
    
53
    /**
54
     * Sets a cached singleton instance.
55
     *
56
     * @param MessagePartFactory $instance
57
     */
58
    protected static function setCachedInstance(MessagePartFactory $instance)
59
    {
60
        if (self::$instances === null) {
61
            self::$instances = [];
62
        }
63
        $class = get_called_class();
64
        self::$instances[$class] = $instance;
65
    }
66
67
    /**
68
     * Returns a cached singleton instance if one exists, or null if one hasn't
69
     * been created yet.
70
     *
71
     * @return MessagePartFactory
72
     */
73
    protected static function getCachedInstance()
74
    {
75
        $class = get_called_class();
76
        if (self::$instances === null || !isset(self::$instances[$class])) {
77
            return null;
78
        }
79
        return self::$instances[$class];
80
    }
81
82
    /**
83
     * Returns the singleton instance for the class.
84
     *
85
     * @param StreamDecoratorFactory $sdf
86
     * @param PartStreamFilterManagerFactory $psf
87
     * @param HeaderFactory $hf
88
     * @param PartFilterFactory $pf
89
     * @return MessagePartFactory
90
     */
91
    public static function getInstance(
92
        StreamDecoratorFactory $sdf,
93
        PartStreamFilterManagerFactory $psf,
94
        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

94
        /** @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...
95
        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

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