FilterBuilder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 100
ccs 0
cts 48
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A build() 0 33 3
A getFilterConfiguration() 0 18 4
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
            $configuration = $this->getFilterConfiguration($filterName);
66
            $configuration->configureOptions($resolver);
67
            $configuration->setParameters($resolver->resolve($filterConfiguration));
68
69
            $class = $this->mapping[$filterName];
70
71
            /** @var FilterInterface $filter */
72
            $filter = new $class(
73
                // filter's name
74
                $filterName,
75
                // filter's configuration
76
                $configuration,
77
                // event dispatcher
78
                $this->eventDispatcher
79
            );
80
            $filter->checkRequirements();
81
82
            $filters[$filterName] = $filter;
83
        }
84
85
        return $filters;
86
    }
87
88
    /**
89
     * Return filter configuration instance using the configured mapping.
90
     *
91
     * @param $filter
92
     * @return ConfigurationInterface
93
     * @throws Exception
94
     */
95
    protected function getFilterConfiguration($filter)
96
    {
97
        if (!array_key_exists($filter, $this->mapping)) {
98
            throw new Exception($filter.' filter not found in assets mapping. Check your configuration');
99
        }
100
        $class = $this->mapping[$filter].'Configuration';
101
102
        if (!class_exists($class)) {
103
            throw new Exception($class.' filter configuration class not found');
104
        }
105
        $configuration = new $class();
106
107
        if (!($configuration instanceof ConfigurationInterface)) {
108
            throw new Exception(get_class($configuration).' should be an instance of '.ConfigurationInterface::class);
109
        }
110
111
        return $configuration;
112
    }
113
}
114