Passed
Branch v2 (aaaa8e)
by Brent
03:38
created

VariableFactory::setYamlRule()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 1
nop 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
10
class VariableFactory extends DynamicFactory
11
{
12
    private $yamlParser = null;
13
    private $markdownParser = null;
14
    private $imageParser = null;
15
    private $variableParser = null;
16
17
    public function __construct()
18
    {
19
        $this->setJsonRule();
20
        $this->setYamlRule();
21
        $this->setMarkdownRule();
22
        $this->setImageRule();
23
    }
24
25
    public static function make(): VariableFactory
26
    {
27
        return new self();
28
    }
29
30
    public function setYamlParser(Yaml $yamlParser): VariableFactory
31
    {
32
        $this->yamlParser = $yamlParser;
33
34
        return $this;
35
    }
36
37
    public function setMarkdownParser(Parsedown $markdownParser): VariableFactory
38
    {
39
        $this->markdownParser = $markdownParser;
40
41
        return $this;
42
    }
43
44
    public function setImageParser(ImageFactory $imageParser): VariableFactory
45
    {
46
        $this->imageParser = $imageParser;
47
48
        return $this;
49
    }
50
51
    public function setVariableParser(VariableParser $variableParser): VariableFactory
52
    {
53
        $this->variableParser = $variableParser;
54
55
        return $this;
56
    }
57
58
    public function create($value): AbstractVariable
59
    {
60
        foreach ($this->getRules() as $rule) {
61
            try {
62
                $variable = $rule($value);
63
            } catch (\TypeError $e) {
0 ignored issues
show
Bug introduced by
The class TypeError does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
64
                continue;
65
            }
66
67
            if ($variable instanceof AbstractVariable) {
68
                return $variable;
69
            }
70
        }
71
72
        return DefaultVariable::make($value);
73
    }
74
75
    private function setJsonRule(): DynamicFactory
76
    {
77
        return $this->setRule(JsonVariable::class, function (string $value) {
78
            if (is_string($value) && pathinfo($value, PATHINFO_EXTENSION) === 'json') {
79
                return JsonVariable::make($value);
80
            }
81
82
            return null;
83
        });
84
    }
85
86
    private function setYamlRule(): void
87
    {
88
        $this->setRule(YamlVariable::class, function (string $value) {
89
            if (! $this->yamlParser) {
90
                return null;
91
            }
92
93
            $extension = pathinfo($value, PATHINFO_EXTENSION);
94
95
            if (in_array($extension, ['yaml', 'yml'])) {
96
                return YamlVariable::make($value, $this->yamlParser, $this->variableParser);
97
            }
98
99
            return null;
100
        });
101
    }
102
103
    private function setMarkdownRule(): void
104
    {
105
        $this->setRule(MarkdownVariable::class, function (string $value) {
106
            if ($this->markdownParser && pathinfo($value, PATHINFO_EXTENSION) === 'md') {
107
                return MarkdownVariable::make($value, $this->markdownParser);
108
            }
109
110
            return null;
111
        });
112
    }
113
114
    private function setImageRule(): void
115
    {
116
        $this->setRule(ImageVariable::class, function ($value) {
117
            if (! $this->imageParser) {
118
                return null;
119
            }
120
121
            $srcPath = is_array($value) ? $value['src'] ?? null : $value;
122
123
            $extension = pathinfo($srcPath, PATHINFO_EXTENSION);
124
125
            // TODO: Let ImageVariable take a config array, and let it parse that array itself.
126
127
            if (in_array($extension, ['jpeg', 'jpg', 'png', 'gif'])) {
128
                return ImageVariable::make($srcPath, $this->imageParser, $value['alt'] ?? null);
129
            }
130
131
            return null;
132
        });
133
    }
134
}
135