DefaultConfigurator::configure()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 22
nc 4
nop 1
1
<?php
2
3
namespace Brendt\Image\Config;
4
5
use Brendt\Image\Exception\InvalidConfigurationException;
6
use Brendt\Image\ResponsiveFactory;
7
use Brendt\Image\Scaler\AbstractScaler;
8
use Brendt\Image\Scaler\FileSizeScaler;
9
use Brendt\Image\Scaler\Scaler;
10
use Brendt\Image\Scaler\SizesScaler;
11
use Brendt\Image\Scaler\WidthScaler;
12
13
class DefaultConfigurator implements ResponsiveFactoryConfigurator
14
{
15
16
    /**
17
     * The default config
18
     *
19
     * @var array
20
     */
21
    protected $config = [
22
        'driver'           => 'gd',
23
        'publicPath'       => './',
24
        'sourcePath'       => './',
25
        'rebase'           => false,
26
        'enableCache'      => false,
27
        'optimize'         => false,
28
        'scaler'           => 'filesize',
29
        'stepModifier'     => 0.5,
30
        'minFileSize'      => 5000,
31
        'maxFileSize'      => null,
32
        'minWidth'         => 300,
33
        'maxWidth'         => null,
34
        'sizes'            => [],
35
        'optimizerOptions' => [],
36
        'includeSource'    => true,
37
    ];
38
39
    /**
40
     * ResponsiveFactoryConfigurator constructor.
41
     *
42
     * @param array $config
43
     *
44
     * @throws InvalidConfigurationException
45
     */
46
    public function __construct(array $config = []) {
47
        if (isset($config['driver']) && !in_array($config['driver'], ['gd', 'imagick'])) {
48
            throw new InvalidConfigurationException('Invalid driver. Possible drivers are `gd` and `imagick`');
49
        }
50
51
        $this->config = array_merge($this->config, $config);
52
    }
53
54
    /**
55
     * @param ResponsiveFactory $factory
56
     *
57
     * @return void
58
     */
59
    public function configure(ResponsiveFactory $factory) {
60
        /** @var AbstractScaler $scaler */
61
        switch ($this->config['scaler']) {
62
            case 'filesize':
63
                $scaler = new FileSizeScaler($this);
64
                break;
65
            case 'width':
66
                $scaler = new WidthScaler($this);
67
                break;
68
            case 'sizes':
69
            default:
70
                $scaler = new SizesScaler($this);
71
                $scaler->setSizes($this->config['sizes']);
72
                break;
73
        }
74
75
        $factory
76
            ->setDriver($this->config['driver'])
77
            ->setPublicPath($this->config['publicPath'])
78
            ->setSourcePath($this->config['sourcePath'])
79
            ->setRebase($this->config['rebase'])
80
            ->setEnableCache($this->config['enableCache'])
81
            ->setOptimize($this->config['optimize'])
82
            ->setOptimizerOptions($this->config['optimizerOptions'])
83
            ->setScaler($scaler);
84
    }
85
86
    /**
87
     * @param Scaler $scaler
88
     *
89
     * @return Scaler
90
     */
91
    public function configureScaler(Scaler $scaler) {
92
        $scaler
93
            ->setIncludeSource($this->config['includeSource'])
94
            ->setMinFileSize($this->config['minFileSize'])
95
            ->setMinWidth($this->config['minWidth'])
96
            ->setMaxFileSize($this->config['maxFileSize'])
97
            ->setMaxWidth($this->config['maxWidth'])
98
            ->setStepModifier($this->config['stepModifier']);
99
100
        return $scaler;
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function getConfig() {
107
        return $this->config;
108
    }
109
110
    /**
111
     * @param $key
112
     *
113
     * @return mixed|null
114
     */
115
    public function get($key) {
116
        return isset($this->config[$key]) ? $this->config[$key] : null;
117
    }
118
}
119