Passed
Push — develop ( ec1e42...80559e )
by Brent
03:22
created

VariableFactory::setJsonRule()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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