Passed
Push — develop ( 77c96c...32ca41 )
by Brent
10:18
created

VariableFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Stitcher\Variable;
4
5
use Pageon\Html\Image\ImageFactory;
6
use Parsedown;
7
use Stitcher\DynamicFactory;
8
use Symfony\Component\Yaml\Yaml;
9
use TypeError;
10
11
class VariableFactory extends DynamicFactory
12
{
13
    /** @var \Symfony\Component\Yaml\Yaml */
14
    private $yamlParser;
15
16
    /** @var \Parsedown */
17
    private $markdownParser;
18
19
    /** @var \Pageon\Html\Image\ImageFactory */
20
    private $imageParser;
21
22
    /** @var \Stitcher\Variable\VariableParser */
23
    private $variableParser;
24
25 40
    public function __construct()
26
    {
27 40
        $this->setJsonRule();
28 40
        $this->setYamlRule();
29 40
        $this->setMarkdownRule();
30 40
        $this->setImageRule();
31 40
        $this->setDirectoryRule();
32 40
        $this->setHtmlRule();
33 40
    }
34
35 35
    public static function make(): VariableFactory
36
    {
37 35
        return new self();
38
    }
39
40 37
    public function setYamlParser(Yaml $yamlParser): VariableFactory
41
    {
42 37
        $this->yamlParser = $yamlParser;
43
44 37
        return $this;
45
    }
46
47 37
    public function setMarkdownParser(Parsedown $markdownParser): VariableFactory
48
    {
49 37
        $this->markdownParser = $markdownParser;
50
51 37
        return $this;
52
    }
53
54 37
    public function setImageParser(ImageFactory $imageParser): VariableFactory
55
    {
56 37
        $this->imageParser = $imageParser;
57
58 37
        return $this;
59
    }
60
61 40
    public function setVariableParser(VariableParser $variableParser): VariableFactory
62
    {
63 40
        $this->variableParser = $variableParser;
64
65 40
        return $this;
66
    }
67
68 31
    public function create($value): AbstractVariable
69
    {
70 31
        foreach ($this->getRules() as $rule) {
71
            try {
72 31
                $variable = $rule($value);
73 28
            } catch (TypeError $e) {
0 ignored issues
show
Bug introduced by
The class TypeError does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
74 28
                continue;
75
            }
76
77 31
            if ($variable instanceof AbstractVariable) {
78 31
                return $variable;
79
            }
80
        }
81
82 30
        return DefaultVariable::make($value);
83
    }
84
85 View Code Duplication
    private function setJsonRule(): DynamicFactory
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87 40
        return $this->setRule(JsonVariable::class, function (string $value) {
88 28
            if (pathinfo($value, PATHINFO_EXTENSION) !== 'json') {
89 28
                return null;
90
            }
91
92 1
            return JsonVariable::make($value);
93 40
        });
94
    }
95
96
    private function setYamlRule(): void
97
    {
98 40
        $this->setRule(YamlVariable::class, function (string $value) {
99 28
            if (! $this->yamlParser) {
100
                return null;
101
            }
102
103 28
            $extension = pathinfo($value, PATHINFO_EXTENSION);
104
105 28
            if (! \in_array($extension, ['yaml', 'yml'])) {
106 28
                return null;
107
            }
108
109 24
            return YamlVariable::make($value, $this->yamlParser, $this->variableParser);
110 40
        });
111 40
    }
112
113
    private function setMarkdownRule(): void
114
    {
115 40
        $this->setRule(MarkdownVariable::class, function (string $value) {
116 28
            if (! $this->markdownParser) {
117
                return null;
118
            }
119
120 28
            if (pathinfo($value, PATHINFO_EXTENSION) !== 'md') {
121 28
                return null;
122
            }
123
124 3
            return MarkdownVariable::make($value, $this->markdownParser);
125 40
        });
126 40
    }
127
128 View Code Duplication
    private function setHtmlRule(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130 40
        $this->setRule(HtmlVariable::class, function (string $value) {
131 27
            if (pathinfo($value, PATHINFO_EXTENSION) !== 'html') {
132 27
                return null;
133
            }
134
135
            return new HtmlVariable($value);
136 40
        });
137 40
    }
138
139
    private function setImageRule(): void
140
    {
141 40
        $this->setRule(ImageVariable::class, function ($value) {
142 31
            if (! $this->imageParser) {
143
                return null;
144
            }
145
146 31
            $srcPath = \is_array($value) ? $value['src'] ?? null : $value;
147
148 31
            $extension = pathinfo($srcPath, PATHINFO_EXTENSION);
149
150 31
            if (! \in_array($extension, ['jpeg', 'jpg', 'png', 'gif'])) {
151 30
                return null;
152
            }
153
154 16
            return ImageVariable::make($value, $this->imageParser);
155 40
        });
156 40
    }
157
158
    private function setDirectoryRule(): void
159
    {
160 40
        $this->setRule(DirectoryVariable::class, function ($value) {
161 30
            if (!is_string($value) || substr($value, -1) !== '/') {
162 30
                return null;
163
            }
164
165
            if (strpos($value, 'http') !== null) {
166
                return null;
167
            }
168
169
            return new DirectoryVariable($value, $this->variableParser);
170 40
        });
171 40
    }
172
}
173