Passed
Branch php8-testing (0e47ea)
by Zaahid
03:09
created

MessagePartFactory::setCachedInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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

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

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

96
        /** @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...
97
    ) {
98 104
        $instance = static::getCachedInstance();
99 104
        if ($instance === null) {
100 3
            $ref = new ReflectionClass(get_called_class());
101 3
            $n = $ref->getConstructor()->getNumberOfParameters();
102 3
            $args = [];
103 3
            for ($i = 0; $i < $n; ++$i) {
104 3
                $args[] = func_get_arg($i);
105
            }
106 3
            $instance = $ref->newInstanceArgs($args);
107 3
            static::setCachedInstance($instance);
108
        }
109 104
        return $instance;
110
    }
111
112
    /**
113
     * Constructs a new MessagePart object and returns it
114
     * 
115
     * @param PartBuilder $partBuilder
116
     * @param StreamInterface $messageStream
117
     * @return \ZBateson\MailMimeParser\Message\Part\MessagePart
118
     */
119
    public abstract function newInstance(PartBuilder $partBuilder, StreamInterface $messageStream = null);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
120
}
121