Passed
Branch 4.9 (cb955a)
by Mikhail
01:30
created

MultipleFileGenerator::find()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace generators;
4
5
use filesystem\Filesystem;
6
7
class MultipleFileGenerator implements IFileGenerator {
8
    private $fileGenerators = [];
9
    private $filesystem;
10
11
    function __construct(Filesystem $filesystem)
12
    {
13
        $this->filesystem = $filesystem;
14
    }
15
16
    public function addGenerator(AbstractGenerator $generator)
17
    {
18
        $className = (new \ReflectionClass($generator))->getShortName();
19
        $this->fileGenerators[$className] = new FileGenerator($generator, $this->filesystem);
20
21
        return $this;
22
    }
23
24
    public function removeGenerator(string $className) {
25
        $inputClassName = (new \ReflectionClass($className))->getShortName();
26
27
        $this->fileGenerators = array_filter($this->fileGenerators, function($fileGenerator) use ($inputClassName) {
28
            $currentClassName = (new \ReflectionClass($fileGenerator->extract()))->getShortName();
29
30
            return $inputClassName !== $currentClassName;
31
        });
32
33
        return $this;
34
    }
35
36
    public function find(string $className): AbstractFileGenerator
37
    {
38
        $found = @$this->fileGenerators[(new \ReflectionClass($className))->getShortName()];
39
40
        if ($found) {
41
            return $found;
42
        }
43
44
        throw new \InvalidArgumentException('There is no file generator for ' . $className);
45
    }
46
47
    public function excluding(string $className) {
48
        return (clone $this)->removeGenerator($className);
49
    }
50
51
    public function including(AbstractGenerator $generator) {
52
        return (clone $this)->addGenerator($generator);
53
    }
54
55
    public function each(callable $callback)
56
    {
57
        array_walk($this->fileGenerators, $callback);
58
59
        return $this;
60
    }
61
62
    public function readFromTemplate()
63
    {
64
        array_walk($this->fileGenerators, function($fileGenerator) {
65
            $fileGenerator->readFromTemplate();
66
        });
67
68
        return $this;
69
    }
70
71
    public function read()
72
    {
73
        array_walk($this->fileGenerators, function($fileGenerator) {
74
            $fileGenerator->read();
75
        });
76
77
        return $this;
78
    }
79
80
    public function exists(): bool
81
    {
82
        return array_reduce($this->fileGenerators, function($exists, $fileGenerator) {
83
            return $exists && $fileGenerator->exists();
84
        }, true);
85
    }
86
87
    public function remove()
88
    {
89
        array_walk($this->fileGenerators, function($fileGenerator) {
90
            $fileGenerator->remove();
91
        });
92
93
        return $this;
94
    }
95
96
    public function throwIfExists(string $message = '')
97
    {
98
        array_walk($this->fileGenerators, function($fileGenerator) use ($message) {
99
            $fileGenerator->throwIfExists($message);
100
        });
101
102
        return $this;
103
    }
104
105
    public function throwIfNotExists(string $message = '')
106
    {
107
        array_walk($this->fileGenerators, function($fileGenerator) use ($message) {
108
            $fileGenerator->throwIfNotExists($message);
109
        });
110
111
        return $this;
112
    }
113
    
114
    public function write()
115
    {
116
        array_walk($this->fileGenerators, function($fileGenerator) {
117
            $fileGenerator->write();
118
        });
119
120
        return $this;
121
    }
122
}
123