Factory::make()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * @author: RunnerLee
4
 * @email: [email protected]
5
 * @time: 2018-02
6
 */
7
8
namespace Runner\Heshen;
9
10
use Runner\Heshen\Contracts\StatefulInterface;
11
12
class Factory
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $loader = [];
18
19
    /**
20
     * @var Blueprint[]
21
     */
22
    protected $blueprints = [];
23
24
    /**
25
     * Factory constructor.
26
     *
27
     * @param array $loader
28
     */
29 1
    public function __construct(array $loader)
30
    {
31 1
        $this->loader = $loader;
32 1
    }
33
34
    /**
35
     * @param StatefulInterface $stateful
36
     *
37
     * @return Machine
38
     */
39 1
    public function make(StatefulInterface $stateful): Machine
40
    {
41 1
        $blueprint = $this->loader[get_class($stateful)];
42
43 1
        if (!array_key_exists($blueprint, $this->blueprints)) {
44 1
            $this->blueprints[$blueprint] = new $blueprint();
45
        }
46
47 1
        return new Machine($stateful, $this->blueprints[$blueprint]);
48
    }
49
}
50