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

WriterFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 70%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 36
ccs 7
cts 10
cp 0.7
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createWriter() 0 14 3
A addWriter() 0 4 1
1
<?php
2
3
namespace TextFile\Factory;
4
5
use TextFile\Exception\InvalidWriterException;
6
use TextFile\Writer\WriterInterface;
7
8
/**
9
 * Class WriterFactory
10
 *
11
 * @package TextFile\Factory
12
 */
13
class WriterFactory
14
{
15
    /**
16
     * @var WriterInterface[]
17
     */
18
    protected $writers = [];
19
20
    /**
21
     * @param string $writerClass
22
     *
23
     * @return WriterInterface
24
     * @throws InvalidWriterException
25
     */
26 3
    public function createWriter($writerClass)
27
    {
28 3
        if (isset($this->writers[$writerClass])) {
29 1
            return $this->writers[$writerClass];
30
        }
31
32 3
        if (!isset(class_implements($writerClass)[WriterInterface::class])) {
33 1
            throw new InvalidWriterException();
34
        }
35
36 2
        $this->addWriter($writerClass);
37
38 2
        return $this->writers[$writerClass];
39
    }
40
41
    /**
42
     * @param string $writerClass
43
     */
44
    protected function addWriter($writerClass)
45
    {
46
        $this->writers[$writerClass] = new $writerClass;
47
    }
48
}
49