ConfigBuilder   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
eloc 31
dl 0
loc 179
c 0
b 0
f 0
ccs 52
cts 52
cp 1
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A columns() 0 5 1
A __construct() 0 5 1
A imageColors() 0 5 1
A imageHue() 0 5 1
A imageCellSize() 0 5 1
A threshold() 0 5 1
A states() 0 5 1
A seed() 0 5 1
A createFromPreset() 0 11 2
A rows() 0 5 1
A neighborhoodType() 0 5 1
A neighborhoodSize() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Barryvanveen\CCA\Builders;
6
7
use Barryvanveen\CCA\Config;
8
use Barryvanveen\CCA\Config\Options;
9
use Barryvanveen\CCA\Config\Presets;
10
11
class ConfigBuilder
12
{
13
    /** @var array */
14
    protected $options = [];
15
16
    /** @var Config */
17
    protected $config;
18
19 6
    public function __construct()
20
    {
21 6
        $this->options = [];
22
23 6
        $this->config = new Config($this->options);
24 6
    }
25
26
    /**
27
     * Instantiate the config based on a preset.
28
     *
29
     * @see \Barryvanveen\CCA\Config\Presets::VALID_PRESETS
30
     *
31
     * @param string $preset
32
     *
33
     * @return ConfigBuilder
34
     *
35
     * @throws \Barryvanveen\CCA\Exceptions\InvalidPresetException
36
     */
37 3
    public static function createFromPreset(string $preset): self
38
    {
39 3
        $presetOptions = Presets::getPresetOptions($preset);
40
41 3
        $builder = new self();
42
43 3
        foreach ($presetOptions as $option => $value) {
44 3
            $builder->{$option}($value);
45
        }
46
47 3
        return $builder;
48
    }
49
50
    /**
51
     * Set the number of columns.
52
     *
53
     * @param int $columns
54
     */
55 6
    public function columns(int $columns)
56
    {
57 6
        $this->options[Options::COLUMNS] = $columns;
58
59 6
        $this->config = new Config($this->options);
60 6
    }
61
62
    /**
63
     * Set the size of each cell in the generated image.
64
     *
65
     * @param int $size
66
     */
67 3
    public function imageCellSize(int $size)
68
    {
69 3
        $this->options[Options::IMAGE_CELL_SIZE] = $size;
70
71 3
        $this->config = new Config($this->options);
72 3
    }
73
74
    /**
75
     * Set the colors that should be used when generating images. Each color in the array should be an instance of
76
     * RgbColor. The number of colors should be equal to (or greater than) the number of states. Setting imageColors()
77
     * takes precedence over setting imageHue().
78
     *
79
     * @see \Phim\Color\RgbColor
80
     *
81
     * @param array $colors
82
     */
83 3
    public function imageColors(array $colors)
84
    {
85 3
        $this->options[Options::IMAGE_COLORS] = $colors;
86
87 3
        $this->config = new Config($this->options);
88 3
    }
89
90
    /**
91
     * Set the hue (e.g. the color) used to pick colors for the generated image. Colors are then picked by varying
92
     * the saturation of the color. Should be an integer between 0 and 360. Setting imageColors() takes
93
     * precedence over setting imageHue().
94
     *
95
     * @see https://en.wikipedia.org/wiki/HSL_and_HSV
96
     *
97
     * @param int $hue
98
     */
99 3
    public function imageHue(int $hue)
100
    {
101 3
        $this->options[Options::IMAGE_HUE] = $hue;
102
103 3
        $this->config = new Config($this->options);
104 3
    }
105
106
    /**
107
     * Set the size of the neighborhood.
108
     *
109
     * @param int $neighborhoodSize
110
     */
111 3
    public function neighborhoodSize(int $neighborhoodSize)
112
    {
113 3
        $this->options[Options::NEIGHBORHOOD_SIZE] = $neighborhoodSize;
114
115 3
        $this->config = new Config($this->options);
116 3
    }
117
118
    /**
119
     * Set the neighborhood type.
120
     *
121
     * @see \Barryvanveen\CCA\Config\NeighborhoodOptions::NEIGHBORHOOD_TYPES
122
     *
123
     * @param string $neighborhoodType
124
     */
125 3
    public function neighborhoodType(string $neighborhoodType)
126
    {
127 3
        $this->options[Options::NEIGHBORHOOD_TYPE] = $neighborhoodType;
128
129 3
        $this->config = new Config($this->options);
130 3
    }
131
132
    /**
133
     * Set the number of rows.
134
     *
135
     * @param int $rows
136
     */
137 6
    public function rows(int $rows)
138
    {
139 6
        $this->options[Options::ROWS] = $rows;
140
141 6
        $this->config = new Config($this->options);
142 6
    }
143
144
    /**
145
     * Set the seed with which to initiate the random number generator. Can be used for reproducible results or for
146
     * testing purposes.
147
     *
148
     * @param int $seed
149
     */
150 3
    public function seed(int $seed)
151
    {
152 3
        $this->options[Options::SEED] = $seed;
153
154 3
        $this->config = new Config($this->options);
155 3
    }
156
157
    /**
158
     * Set the number of states each cell can take on.
159
     *
160
     * @param int $states
161
     */
162 3
    public function states(int $states)
163
    {
164 3
        $this->options[Options::STATES] = $states;
165
166 3
        $this->config = new Config($this->options);
167 3
    }
168
169
    /**
170
     * Set the threshold. A cell must have at least this number of successor states among its neighbors in order for
171
     * itself to cycle to the successor state.
172
     *
173
     * @param int $threshold
174
     */
175 3
    public function threshold(int $threshold)
176
    {
177 3
        $this->options[Options::THRESHOLD] = $threshold;
178
179 3
        $this->config = new Config($this->options);
180 3
    }
181
182
    /**
183
     * Get the config object.
184
     *
185
     * @return Config
186
     */
187 6
    public function get(): Config
188
    {
189 6
        return $this->config;
190
    }
191
}
192