Passed
Push — develop ( 435016...f39ca2 )
by Brent
05:23
created

VariableFactory::setImageRule()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

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