Test Failed
Push — master ( 77d308...e79ab4 )
by Artem
01:53
created

AbstractFactory::loadConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
1
<?php
2
3
namespace Prozorov\DataVerification\Factories;
4
5
use Prozorov\DataVerification\Exceptions\{ConfigurationException, FactoryException};
6
use Webmozart\Assert\Assert;
7
8
abstract class AbstractFactory
9
{
10
    /**
11
     * @var string $allowedType
12
     */
13
    protected $allowedType;
14
15
    /**
16
     * @var array $config
17
     */
18
    protected $config = [];
19
20
    /**
21
     * @var array $resolved
22
     */
23
    protected $resolved = [];
24
25 9
    public function __construct(array $config)
26
    {
27 9
        $this->config = $config;
28 9
    }
29
30
    /**
31
     * make.
32
     *
33
     * @access	public
34
     * @param	string	$code	
35
     * @return	mixed
36
     */
37 1
    public function make(string $code)
38
    {
39 1
        if (! empty($this->resolved[$code])) {
40
            return $this->resolved[$code];
41
        }
42
43 1
        if (empty($this->config)) {
44
            throw new ConfigurationException('Фабрика не инициализирована');
45
        }
46
47 1
        if (is_callable($this->config[$code])) {
48
            $resolved = $this->config[$code]();
49 1
        } elseif (is_string($code)) {
0 ignored issues
show
introduced by
The condition is_string($code) is always true.
Loading history...
50 1
            $resolved = $this->getResolvedFromString($code);
51
        }
52
53 1
        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...
54
55 1
        $this->resolved[$code] = $resolved;
56
57 1
        return $this->resolved[$code];
58
    }
59
60
    /**
61
     * getResolvedFromString.
62
     *
63
     * @access	protected
64
     * @return	mixed
65
     */
66 1
    protected function getResolvedFromString(string $code)
67
    {
68 1
        if (! $this->entityExists($code)) {
69
            throw new FactoryException('Фабрика не может сделать такую сущность');
70
        }
71
72 1
        return new $this->config[$code];
73
    }
74
75
    /**
76
     * entityExists.
77
     *
78
     * @access	public
79
     * @param	string	$code	
80
     * @return	bool
81
     */
82 1
    public function entityExists(string $code): bool
83
    {
84 1
        return array_key_exists($code, $this->config);
85
    }
86
}
87