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

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
        PartStreamFilterManagerFactory $psf,
0 ignored issues
show
Unused Code introduced by
The parameter $psf 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

96
        /** @scrutinizer ignore-unused */ PartStreamFilterManagerFactory $psf,

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...
97
        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

97
        /** @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...
98
        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

98
        /** @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...
99
        MessageHelperService $mhs = null
0 ignored issues
show
Unused Code introduced by
The parameter $mhs 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

99
        /** @scrutinizer ignore-unused */ MessageHelperService $mhs = 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...
100
    ) {
101
        $instance = static::getCachedInstance();
102
        if ($instance === null) {
103
            $ref = new ReflectionClass(get_called_class());
104
            $n = $ref->getConstructor()->getNumberOfParameters();
105
            $args = [];
106
            for ($i = 0; $i < $n; ++$i) {
107
                $args[] = func_get_arg($i);
108
            }
109
            $instance = $ref->newInstanceArgs($args);
110
            static::setCachedInstance($instance);
111
        }
112
        return $instance;
113
    }
114
115
    /**
116
     * Constructs a new MessagePart object and returns it
117
     * 
118
     * @param PartBuilder $partBuilder
119
     * @param StreamInterface $messageStream
120
     * @return \ZBateson\MailMimeParser\Message\Part\MessagePart
121
     */
122
    public abstract function newInstance(PartBuilder $partBuilder, StreamInterface $messageStream = null);
123
}
124