Completed
Push — master ( 9f2dac...cab9cb )
by Arnaud
07:03 queued 01:55
created

FilterBuilder::build()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
ccs 0
cts 23
cp 0
rs 8.8571
cc 3
eloc 18
nc 3
nop 1
crap 12
1
<?php
2
3
namespace JK\Sam\Filter;
4
5
use Exception;
6
use JK\Sam\Configuration\ConfigurationInterface;
7
use JK\Sam\Filter\Compass\CompassFilter;
8
use JK\Sam\Filter\Copy\CopyFilter;
9
use JK\Sam\Filter\Merge\MergeFilter;
10
use JK\Sam\Filter\Minify\MinifyFilter;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
14
class FilterBuilder
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $mapping = [];
20
21
    /**
22
     * @var bool
23
     */
24
    protected $isDebug = true;
25
26
    /**
27
     * @var EventDispatcherInterface
28
     */
29
    protected $eventDispatcher;
30
31
    /**
32
     * FilterBuilder constructor.
33
     *
34
     * @param EventDispatcherInterface $eventDispatcher
35
     */
36
    public function __construct(EventDispatcherInterface $eventDispatcher)
37
    {
38
        $this->mapping = [
39
            'compass' => CompassFilter::class,
40
            'copy' => CopyFilter::class,
41
            'merge' => MergeFilter::class,
42
            'minify' => MinifyFilter::class,
43
        ];
44
        $this->eventDispatcher = $eventDispatcher;
45
    }
46
47
    /**
48
     * Build the filter with the given configuration.
49
     *
50
     * @param array $filterConfigurations
51
     * @return FilterInterface[]
52
     * @throws Exception
53
     */
54
    public function build(array $filterConfigurations)
55
    {
56
        $resolver = new OptionsResolver();
57
        $filters = [];
58
59
        foreach ($filterConfigurations as $filterName => $filterConfiguration) {
60
            $resolver->clear();
61
62
            if ($filterConfiguration === null) {
63
                $filterConfiguration = [];
64
            }
65
66
            $configuration = $this->getFilterConfiguration($filterName);
67
            $configuration->configureOptions($resolver);
68
            $configuration->setParameters($resolver->resolve($filterConfiguration));
69
70
            $class = $this->mapping[$filterName];
71
72
            /** @var Filter $filter */
73
            $filter = new $class(
74
                // filter's name
75
                $filterName,
76
                // filter's configuration
77
                $configuration,
78
                // event dispatcher
79
                $this->eventDispatcher
80
            );
81
            $filter->checkRequirements();
82
83
            $filters[$filterName] = $filter;
84
        }
85
86
        return $filters;
87
    }
88
89
    /**
90
     * Return filter configuration instance using the configured mapping.
91
     *
92
     * @param $filter
93
     * @return ConfigurationInterface
94
     * @throws Exception
95
     */
96
    protected function getFilterConfiguration($filter)
97
    {
98
        if (!array_key_exists($filter, $this->mapping)) {
99
            throw new Exception($filter.' filter not found in assets mapping. Check your configuration');
100
        }
101
        $class = $this->mapping[$filter].'Configuration';
102
103
        if (!class_exists($class)) {
104
            throw new Exception($class.' filter configuration class not found');
105
        }
106
        $configuration = new $class();
107
108
        if (!($configuration instanceof ConfigurationInterface)) {
109
            throw new Exception(get_class($configuration).' should be an instance of '.ConfigurationInterface::class);
110
        }
111
112
        return $configuration;
113
    }
114
}
115