Passed
Push — master ( 910d8e...f583f7 )
by Joschi
02:39
created

SourceSizeMediaCondition::initializeMinProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Application\Model
9
 * @author     Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Respimgcss\Application\Model;
38
39
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface;
40
use Jkphl\Respimgcss\Domain\Contract\CssMinMaxMediaConditionInterface;
41
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition;
42
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition;
43
44
/**
45
 * Source size media condition
46
 *
47
 * @package    Jkphl\Respimgcss
48
 * @subpackage Jkphl\Respimgcss\Application\Model
49
 */
50
class SourceSizeMediaCondition
51
{
52
    /**
53
     * Media condition string
54
     *
55
     * @var string
56
     */
57
    protected $value;
58
    /**
59
     * Size and resolution conditions
60
     *
61
     * @var CssMinMaxMediaConditionInterface[]
62
     */
63
    protected $conditions;
64
    /**
65
     * Minimum width
66
     *
67
     * @var int|null
68
     */
69
    protected $minimumWidth = null;
70
    /**
71
     * Maximum width
72
     *
73
     * @var int|null
74
     */
75
    protected $maximumWidth = null;
76
    /**
77
     * Minimum resolution
78
     *
79
     * @var float|null
80
     */
81
    protected $minimumResolution = null;
82
    /**
83
     * Maximum resolution
84
     *
85
     * @var float|null
86
     */
87
    protected $maximumResolution = null;
88
89
    /**
90
     * Source size media condition constructor
91
     *
92
     * @param string $value                                  Media condition string
93
     * @param CssMinMaxMediaConditionInterface[] $conditions Size and resolution conditions
94
     */
95 15
    public function __construct(string $value, array $conditions = [])
96
    {
97 15
        $this->value      = $value;
98 15
        $this->conditions = $conditions;
99 15
        $this->initialize();
100 15
    }
101
102
    /**
103
     * Initialize the minimum / maximum values
104
     */
105 15
    protected function initialize(): void
106
    {
107
        /** @var CssMinMaxMediaConditionInterface $condition */
108 15
        foreach ($this->conditions as $condition) {
109
            // If this is a width condition
110 12
            if ($condition instanceof WidthMediaCondition) {
111 10
                $this->initializeCondition($condition, 'minimumWidth', 'maximumWidth');
112 10
                continue;
113
            }
114
            // If this is a resolution condition
115 6
            if ($condition instanceof ResolutionMediaCondition) {
116 6
                $this->initializeCondition($condition, 'minimumResolution', 'maximumResolution');
117
            }
118
        }
119 15
    }
120
121
    /**
122
     * Initialize a media condition
123
     *
124
     * @param CssMinMaxMediaConditionInterface $condition Media condition
125
     * @param string $minProperty                         Minimum property name
126
     * @param string $maxProperty                         Maximum property name
127
     */
128 11
    protected function initializeCondition(
129
        CssMinMaxMediaConditionInterface $condition,
130
        string $minProperty,
131
        string $maxProperty
132
    ): void {
133 11
        $modifier = $condition->getModifier();
134 11
        $value    = $condition->getValue()->getValue();
135
136
        // Minimum value
137 11
        if ($modifier == CssMinMaxMediaConditionInterface::MIN) {
138 8
            $this->initializeMinProperty($minProperty, $value);
139 8
            return;
140
        }
141
142
        // Maximum value
143 7
        if ($modifier == CssMinMaxMediaConditionInterface::MAX) {
144 6
            $this->initializeMaxProperty($maxProperty, $value);
145 6
            return;
146
        }
147
148 3
        $this->$minProperty = $this->$maxProperty = $value;
149 3
    }
150
151
    /**
152
     * Initialize a minimum property
153
     *
154
     * @param string $minProperty Property name
155
     * @param float $value        Property Value
156
     */
157 8
    protected function initializeMinProperty(string $minProperty, float $value): void
158
    {
159 8
        $this->$minProperty = ($this->$minProperty === null) ? $value : (min($value, $this->$minProperty));
160 8
    }
161
162
    /**
163
     * Initialize a maximum property
164
     *
165
     * @param string $maxProperty Property name
166
     * @param float $value        Property Value
167
     */
168 6
    protected function initializeMaxProperty(string $maxProperty, float $value): void
169
    {
170 6
        $this->$maxProperty = ($this->$maxProperty === null) ? $value : (max($value, $this->$maxProperty));
171 6
    }
172
173
    /**
174
     * Return the media condition string
175
     *
176
     * @return string Media condition string
177
     */
178 2
    public function getValue(): string
179
    {
180 2
        return $this->value;
181
    }
182
183
    /**
184
     * Return the size and resolution conditions
185
     *
186
     * @return CssMinMaxMediaConditionInterface[] Size and resolution conditions
187
     */
188 7
    public function getConditions(): array
189
    {
190 7
        return $this->conditions;
191
    }
192
193
    /**
194
     * Test if this source size condition matches a particular width and density
195
     *
196
     * @param AbsoluteLengthInterface $width Width
197
     * @param float $density                 Density
198
     *
199
     * @return bool This source size condition matches
200
     */
201 5
    public function matches(AbsoluteLengthInterface $width, float $density): bool
202
    {
203 5
        $match = true;
204
205
        // Run through all conditions
206
        /** @var CssMinMaxMediaConditionInterface $condition */
207 5
        foreach ($this->conditions as $condition) {
208 5
            $value = ($condition instanceof WidthMediaCondition) ? $width->getValue() : $density;
209 5
            if (!$condition->matches($value)) {
210 3
                $match = false;
211 5
                break;
212
            }
213
        }
214
215 5
        return $match;
216
    }
217
218
    /**
219
     * Return the minimum width
220
     *
221
     * @return int|null Minimum width
222
     */
223 4
    public function getMinimumWidth(): ?int
224
    {
225 4
        return $this->minimumWidth;
226
    }
227
228
    /**
229
     * Return the maximum width
230
     *
231
     * @return int|null Maximum width
232
     */
233 6
    public function getMaximumWidth(): ?int
234
    {
235 6
        return $this->maximumWidth;
236
    }
237
238
    /**
239
     * Return the minimum resolution
240
     *
241
     * @return float Minimum resolution
242
     */
243 3
    public function getMinimumResolution(): ?float
244
    {
245 3
        return $this->minimumResolution;
246
    }
247
248
    /**
249
     * Return the maximum resolution
250
     *
251
     * @return float Maximum resolution
252
     */
253 3
    public function getMaximumResolution(): ?float
254
    {
255 3
        return $this->maximumResolution;
256
    }
257
}