Test Failed
Push — master ( 45b155...96789e )
by Brent
04:23 queued 02:15
created

DefaultConfigurator::configure()   B

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
    ];
37
38
    /**
39
     * ResponsiveFactoryConfigurator constructor.
40
     *
41
     * @param array $config
42
     *
43
     * @throws InvalidConfigurationException
44
     */
45
    public function __construct(array $config = []) {
46
        if (isset($config['driver']) && !in_array($config['driver'], ['gd', 'imagick'])) {
47
            throw new InvalidConfigurationException('Invalid driver. Possible drivers are `gd` and `imagick`');
48
        }
49
50
        $this->config = array_merge($this->config, $config);
51
    }
52
53
    /**
54
     * @param ResponsiveFactory $factory
55
     *
56
     * @return void
57
     */
58
    public function configure(ResponsiveFactory $factory) {
59
        /** @var AbstractScaler $scaler */
60
        switch ($this->config['scaler']) {
61
            case 'filesize':
62
                $scaler = new FileSizeScaler($this);
63
                break;
64
            case 'width':
65
                $scaler = new WidthScaler($this);
66
                break;
67
            case 'sizes':
68
            default:
69
                $scaler = new SizesScaler($this);
70
                $scaler->setSizes($this->config['sizes']);
71
                break;
72
        }
73
74
        $factory
75
            ->setDriver($this->config['driver'])
76
            ->setPublicPath($this->config['publicPath'])
77
            ->setSourcePath($this->config['sourcePath'])
78
            ->setRebase($this->config['rebase'])
79
            ->setEnableCache($this->config['enableCache'])
80
            ->setOptimize($this->config['optimize'])
81
            ->setOptimizerOptions($this->config['optimizerOptions'])
82
            ->setScaler($scaler);
83
    }
84
85
    /**
86
     * @param Scaler $scaler
87
     *
88
     * @return Scaler
89
     */
90
    public function configureScaler(Scaler $scaler) {
91
        $scaler
92
            ->setMinFileSize($this->config['minFileSize'])
93
            ->setMinWidth($this->config['minWidth'])
94
            ->setMaxFileSize($this->config['maxFileSize'])
95
            ->setMaxWidth($this->config['maxWidth'])
96
            ->setStepModifier($this->config['stepModifier']);
97
98
        return $scaler;
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getConfig() {
105
        return $this->config;
106
    }
107
108
    /**
109
     * @param $key
110
     *
111
     * @return mixed|null
112
     */
113
    public function get($key) {
114
        return isset($this->config[$key]) ? $this->config[$key] : null;
115
    }
116
}
117