Passed
Push — master ( 689ec5...45f08c )
by Sebastian
05:26
created

PresetsManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 63
c 0
b 0
f 0
dl 0
loc 155
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A requireNotGlobal() 0 14 2
A hasPreset() 0 3 2
A registerPreset() 0 12 1
A getPreset() 0 31 4
A init() 0 11 2
A registerGlobalPreset() 0 23 2
A __construct() 0 3 1
1
<?php
2
/**
3
 * File containing the class {@see PresetsManager}.
4
 *
5
 * @see PresetsManager
6
 *@subpackage RGBAColor
7
 * @package AppUtils
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils\RGBAColor;
13
14
use AppUtils\RGBAColor;
15
16
/**
17
 * The presets manager allows adding more presets that can
18
 * then be used with the {@see ColorPresets} class to
19
 * create color instances.
20
 *
21
 * To get the manager instance, use the factory's method:
22
 * {@see ColorFactory::getPresetsManager()}.
23
 *
24
 * @package AppUtils
25
 * @subpackage RGBAColor
26
 * @author Sebastian Mordziol <[email protected]>
27
 *
28
 * @link https://en.wikipedia.org/wiki/List_of_colors_(compact)
29
 */
30
class PresetsManager
31
{
32
    public const ERROR_CANNOT_OVERWRITE_BUILT_IN_PRESET = 94001;
33
34
    public const COLOR_WHITE = 'white';
35
    public const COLOR_BLACK = 'black';
36
    public const COLOR_TRANSPARENT = 'transparent';
37
38
    /**
39
     * @var array<string,array<string,int>>
40
     */
41
    private static $globalPresets = array();
42
43
    /**
44
     * @var bool
45
     */
46
    private static $initialized = false;
47
48
    /**
49
     * @var array<string,array<string,int>>
50
     */
51
    private $customPresets = array();
52
53
    public function __construct()
54
    {
55
        $this->init();
56
    }
57
58
    /**
59
     * Registers the global color presets.
60
     *
61
     * @throws ColorException
62
     */
63
    private function init() : void
64
    {
65
        if(self::$initialized === true)
66
        {
67
            return;
68
        }
69
70
        $this
71
            ->registerGlobalPreset(self::COLOR_WHITE, 255, 255, 255, 255)
72
            ->registerGlobalPreset(self::COLOR_BLACK, 0,0,0, 255)
73
            ->registerGlobalPreset(self::COLOR_TRANSPARENT, 0, 0, 0, 0);
74
    }
75
76
    public function hasPreset(string $name) : bool
77
    {
78
        return isset($this->customPresets[$name]) || isset(self::$globalPresets[$name]);
79
    }
80
81
    /**
82
     * @param string $name
83
     * @return RGBAColor
84
     *
85
     * @throws ColorException
86
     * @see RGBAColor::ERROR_UNKNOWN_COLOR_PRESET
87
     */
88
    public function getPreset(string $name) : RGBAColor
89
    {
90
        $preset = null;
91
92
        if(isset($this->customPresets[$name]))
93
        {
94
            $preset = $this->customPresets[$name];
95
        }
96
        else if(isset(self::$globalPresets[$name]))
97
        {
98
            $preset = self::$globalPresets[$name];
99
        }
100
101
        if($preset !== null)
102
        {
103
            return ColorFactory::create(
104
                ColorChannel::EightBit($preset[RGBAColor::CHANNEL_RED]),
105
                ColorChannel::EightBit($preset[RGBAColor::CHANNEL_GREEN]),
106
                ColorChannel::EightBit($preset[RGBAColor::CHANNEL_BLUE]),
107
                ColorChannel::EightBit($preset[RGBAColor::CHANNEL_ALPHA]),
108
                $name
109
            );
110
        }
111
112
        throw new ColorException(
113
            'No such color preset.',
114
            sprintf(
115
                'The color preset [%s] has not been registered, either as global or custom preset.',
116
                $name
117
            ),
118
            RGBAColor::ERROR_UNKNOWN_COLOR_PRESET
119
        );
120
    }
121
122
    /**
123
     * @param string $name
124
     * @param int $red
125
     * @param int $green
126
     * @param int $blue
127
     * @param int $alpha
128
     * @return $this
129
     * @throws ColorException
130
     */
131
    private function registerGlobalPreset(string $name, int $red, int $green, int $blue, int $alpha) : PresetsManager
132
    {
133
        $this->requireNotGlobal($name);
134
135
        if(!isset(self::$globalPresets[$name]))
136
        {
137
            self::$globalPresets[$name] = array(
138
                RGBAColor::CHANNEL_RED => $red,
139
                RGBAColor::CHANNEL_GREEN => $green,
140
                RGBAColor::CHANNEL_BLUE=> $blue,
141
                RGBAColor::CHANNEL_ALPHA => $alpha
142
            );
143
144
            return $this;
145
        }
146
147
        throw new ColorException(
148
            'Cannot replace global color preset',
149
            sprintf(
150
            'The built-in global presets like [%s] may not be overwritten. Prefer adding a regular preset instead.',
151
                $name
152
            ),
153
            self::ERROR_CANNOT_OVERWRITE_BUILT_IN_PRESET
154
        );
155
    }
156
157
    public function registerPreset(string $name, int $red, int $green, int $blue, int $alpha=255) : PresetsManager
158
    {
159
        $this->requireNotGlobal($name);
160
161
        $this->customPresets[$name] = array(
162
            RGBAColor::CHANNEL_RED => $red,
163
            RGBAColor::CHANNEL_GREEN => $green,
164
            RGBAColor::CHANNEL_BLUE=> $blue,
165
            RGBAColor::CHANNEL_ALPHA => $alpha
166
        );
167
168
        return $this;
169
    }
170
171
    private function requireNotGlobal(string $name) : void
172
    {
173
        if(!isset(self::$globalPresets[$name]))
174
        {
175
            return;
176
        }
177
178
        throw new ColorException(
179
            'Cannot replace global color preset',
180
            sprintf(
181
                'The built-in global presets like [%s] may not be overwritten.',
182
                $name
183
            ),
184
            self::ERROR_CANNOT_OVERWRITE_BUILT_IN_PRESET
185
        );
186
    }
187
}
188
189