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

ReaderFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 66.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 42
ccs 8
cts 12
cp 0.6667
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createReader() 0 16 3
A addReader() 0 6 1
1
<?php
2
3
namespace TextFile\Factory;
4
5
use TextFile\Exception\InvalidReaderException;
6
use TextFile\Exception\InvalidWriterException;
7
use TextFile\Reader\ReaderInterface;
8
use TextFile\Walker\WalkerInterface;
9
10
/**
11
 * Class ReaderFactory
12
 *
13
 * @package TextFile\Factory
14
 */
15
class ReaderFactory
16
{
17
    /**
18
     * @var ReaderInterface[]
19
     */
20
    protected $readers = [];
21
22
    /**
23
     * @param string          $readerClass
24
     * @param WalkerInterface $walker
25
     *
26
     * @return ReaderInterface
27
     * @throws InvalidReaderException
28
     */
29 3
    public function createReader($readerClass, WalkerInterface $walker)
30
    {
31 3
        $walkerClassName = get_class($walker);
32
33 3
        if (isset($this->readers[$readerClass][$walkerClassName])) {
34 1
            return $this->readers[$readerClass][$walkerClassName];
35
        }
36
37 3
        if (!isset(class_implements($readerClass)[ReaderInterface::class])) {
38 1
            throw new InvalidReaderException();
39
        }
40
41 2
        $this->addReader($readerClass, $walker);
42
43 2
        return $this->readers[$readerClass][$walkerClassName];
44
    }
45
46
    /**
47
     * @param string          $readerClass
48
     * @param WalkerInterface $walker
49
     */
50
    protected function addReader($readerClass, WalkerInterface $walker)
51
    {
52
        $walkerClassName = get_class($walker);
53
54
        $this->readers[$readerClass][$walkerClassName] = new $readerClass($walker);
55
    }
56
}
57