AbstractFactory   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 85.19%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 28
c 2
b 0
f 0
dl 0
loc 99
ccs 23
cts 27
cp 0.8519
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getResolvedFromString() 0 11 3
A entityExists() 0 3 1
B make() 0 29 7
1
<?php
2
3
namespace Prozorov\DataVerification\Factories;
4
5
use Prozorov\DataVerification\Exceptions\{ConfigurationException, FactoryException};
6
use Webmozart\Assert\Assert;
7
use Psr\Container\ContainerInterface;
8
9
abstract class AbstractFactory
10
{
11
    /**
12
     * @var string $allowedType
13
     */
14
    protected $allowedType;
15
16
    /**
17
     * @var array $config
18
     */
19
    protected $config = [];
20
21
    /**
22
     * @var array $resolved
23
     */
24
    protected $resolved = [];
25
26
    /**
27
     * @var bool $singletons
28
     */
29
    protected $singletons = true;
30
31
    /**
32
     * @var ContainerInterface $container
33
     */
34
    protected $container;
35
36 17
    public function __construct(array $config, ContainerInterface $container = null)
0 ignored issues
show
Unused Code introduced by
The parameter $container 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

36
    public function __construct(array $config, /** @scrutinizer ignore-unused */ ContainerInterface $container = 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...
37
    {
38 17
        $this->config = $config;
39 17
    }
40
41
    /**
42
     * make.
43
     *
44
     * @access	public
45
     * @param	string	$code	
46
     * @return	mixed
47
     */
48 5
    public function make(string $code)
49
    {
50 5
        if (! empty($this->resolved[$code])) {
51 1
            return $this->resolved[$code];
52
        }
53
54 5
        if (empty($this->config)) {
55
            throw new ConfigurationException('Фабрика не инициализирована');
56
        }
57
58 5
        if (empty($this->config[$code])) {
59 1
            throw new FactoryException('Отсутствуют инструкции по созданию объекта ' . $code);
60
        }
61
62 4
        if (is_callable($this->config[$code])) {
63
            $resolved = $this->config[$code]();
64 4
        } elseif (is_string($code)) {
0 ignored issues
show
introduced by
The condition is_string($code) is always true.
Loading history...
65 4
            $resolved = $this->getResolvedFromString($code);
66
        }
67
68 4
        Assert::isInstanceOf($resolved, $this->allowedType);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $resolved does not seem to be defined for all execution paths leading up to this point.
Loading history...
69
70 4
        if (! $this->singletons) {
71 2
            return $resolved;
72
        }
73
74 2
        $this->resolved[$code] = $resolved;
75
76 2
        return $this->resolved[$code];
77
    }
78
79
    /**
80
     * entityExists.
81
     *
82
     * @access	public
83
     * @param	string	$code	
84
     * @return	bool
85
     */
86 4
    public function entityExists(string $code): bool
87
    {
88 4
        return array_key_exists($code, $this->config);
89
    }
90
91
    /**
92
     * getResolvedFromString.
93
     *
94
     * @access	protected
95
     * @return	mixed
96
     */
97 4
    protected function getResolvedFromString(string $code)
98
    {
99 4
        if (! $this->entityExists($code)) {
100
            throw new FactoryException('Фабрика не может сделать такую сущность');
101
        }
102
103 4
        if (empty($this->container)) {
104 4
            return new $this->config[$code];
105
        }
106
107
        return $this->container->get($this->config[$code]);
108
    }
109
}
110