Passed
Push — develop ( 37d230...30ebd1 )
by Brent
03:14
created

VariableFactory::setMarkdownRule()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 1
nop 0
dl 0
loc 14
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.4285
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 = null;
15
16
    /** @var \Parsedown */
17
    private $markdownParser = null;
18
19
    /** @var \Pageon\Html\Image\ImageFactory */
20
    private $imageParser = null;
21
22
    /** @var \Stitcher\Variable\VariableParser */
23
    private $variableParser = null;
24
25 38
    public function __construct()
26
    {
27 38
        $this->setJsonRule();
28 38
        $this->setYamlRule();
29 38
        $this->setMarkdownRule();
30 38
        $this->setImageRule();
31 38
    }
32
33 34
    public static function make(): VariableFactory
34
    {
35 34
        return new self();
36
    }
37
38 35
    public function setYamlParser(Yaml $yamlParser): VariableFactory
39
    {
40 35
        $this->yamlParser = $yamlParser;
41
42 35
        return $this;
43
    }
44
45 35
    public function setMarkdownParser(Parsedown $markdownParser): VariableFactory
46
    {
47 35
        $this->markdownParser = $markdownParser;
48
49 35
        return $this;
50
    }
51
52 35
    public function setImageParser(ImageFactory $imageParser): VariableFactory
53
    {
54 35
        $this->imageParser = $imageParser;
55
56 35
        return $this;
57
    }
58
59 38
    public function setVariableParser(VariableParser $variableParser): VariableFactory
60
    {
61 38
        $this->variableParser = $variableParser;
62
63 38
        return $this;
64
    }
65
66 30
    public function create($value): AbstractVariable
67
    {
68 30
        foreach ($this->getRules() as $rule) {
69
            try {
70 30
                $variable = $rule($value);
71 27
            } 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...
72 27
                continue;
73
            }
74
75 30
            if ($variable instanceof AbstractVariable) {
76 30
                return $variable;
77
            }
78
        }
79
80 29
        return DefaultVariable::make($value);
81
    }
82
83
    private function setJsonRule(): DynamicFactory
84
    {
85 38
        return $this->setRule(JsonVariable::class, function (string $value) {
86 27
            if (pathinfo($value, PATHINFO_EXTENSION) !== 'json') {
87 27
                return null;
88
            }
89
90 1
            return JsonVariable::make($value);
91 38
        });
92
    }
93
94
    private function setYamlRule(): void
95
    {
96 38
        $this->setRule(YamlVariable::class, function (string $value) {
97 27
            if (! $this->yamlParser) {
98
                return null;
99
            }
100
101 27
            $extension = pathinfo($value, PATHINFO_EXTENSION);
102
103 27
            if (!in_array($extension, ['yaml', 'yml'])) {
104 27
                return null;
105
            }
106
107 23
            return YamlVariable::make($value, $this->yamlParser, $this->variableParser);
108 38
        });
109 38
    }
110
111
    private function setMarkdownRule(): void
112
    {
113 38
        $this->setRule(MarkdownVariable::class, function (string $value) {
114 27
            if (! $this->markdownParser) {
115
                return null;
116
            }
117
118 27
            if (pathinfo($value, PATHINFO_EXTENSION) !== 'md') {
119 27
                return null;
120
            }
121
122 3
            return MarkdownVariable::make($value, $this->markdownParser);
123 38
        });
124 38
    }
125
126
    private function setImageRule(): void
127
    {
128 38
        $this->setRule(ImageVariable::class, function ($value) {
129 30
            if (! $this->imageParser) {
130
                return null;
131
            }
132
133 30
            $srcPath = is_array($value) ? $value['src'] ?? null : $value;
134
135 30
            $extension = pathinfo($srcPath, PATHINFO_EXTENSION);
136
137 30
            if (!in_array($extension, ['jpeg', 'jpg', 'png', 'gif'])) {
138 29
                return null;
139
            }
140
141 16
            return ImageVariable::make($value, $this->imageParser);
142 38
        });
143 38
    }
144
}
145