Completed
Push — master ( 28b764...a7b092 )
by Michaël
02:42
created

WalkerFactory::createWalker()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace TextFile\Factory;
4
5
use TextFile\Exception\InvalidWalkerException;
6
use TextFile\Walker\WalkerInterface;
7
8
/**
9
 * Class WalkerFactory
10
 *
11
 * @package TextFile\Factory
12
 */
13
class WalkerFactory
14
{
15
    /**
16
     * @var WalkerInterface[]
17
     */
18
    protected $walkers = [];
19
20
    /**
21
     * @param string $walkerClass
22
     *
23
     * @return WalkerInterface
24
     * @throws InvalidWalkerException
25
     */
26 3
    public function createWalker($walkerClass)
27
    {
28 3
        if (isset($this->walkers[$walkerClass])) {
29 1
            return $this->walkers[$walkerClass];
30
        }
31
32 3
        if (!isset(class_implements($walkerClass)[WalkerInterface::class])) {
33 1
            throw new InvalidWalkerException();
34
        }
35
36 2
        $this->addWalker($walkerClass);
37
38 2
        return $this->walkers[$walkerClass];
39
    }
40
41
    /**
42
     * @param string $walkerClass
43
     */
44
    protected function addWalker($walkerClass)
45
    {
46
        $this->walkers[$walkerClass] = new $walkerClass;
47
    }
48
}
49