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

VariableFactory   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
dl 0
loc 123
ccs 56
cts 58
cp 0.9655
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 6

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A make() 0 4 1
A setYamlParser() 0 6 1
A setMarkdownParser() 0 6 1
A setImageParser() 0 6 1
A setVariableParser() 0 6 1
A create() 0 16 4
A setJsonRule() 0 10 3
A setYamlRule() 0 16 3
A setMarkdownRule() 0 10 3
A setImageRule() 0 18 4
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