Completed
Push — master ( 5b2b8e...821ba7 )
by Joschi
02:04
created

SourceSizeMediaCondition::getMinimumResolution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
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
80
     */
81
    protected $minimumResolution = 1;
82
    /**
83
     * Maximum resolution
84
     *
85
     * @var float
86
     */
87
    protected $maximumResolution = 1;
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 10
    public function __construct(string $value, array $conditions = [])
96
    {
97 10
        $this->value      = $value;
98 10
        $this->conditions = $conditions;
99 10
        $this->initialize();
100 10
    }
101
102
    /**
103
     * Initialize the minimum / maximum values
104
     */
105 10
    protected function initialize(): void
106
    {
107
        /** @var CssMinMaxMediaConditionInterface $condition */
108 10
        foreach ($this->conditions as $condition) {
109
            // If this is a width condition
110 7
            if ($condition instanceof WidthMediaCondition) {
111 6
                $this->initializeCondition($condition, 'minimumWidth', 'maximumWidth');
112 6
                continue;
113
            }
114
            // If this is a resolution condition
115 3
            if ($condition instanceof ResolutionMediaCondition) {
116 3
                $this->initializeCondition($condition, 'minimumResolution', 'maximumResolution');
117
            }
118
        }
119 10
    }
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 6
    protected function initializeCondition(
129
        CssMinMaxMediaConditionInterface $condition,
130
        string $minProperty,
131
        string $maxProperty
132
    ): void {
133 6
        $modifier = $condition->getModifier();
134 6
        $value    = $condition->getValue()->getValue();
135
136
        // Minimum value
137 6
        if ($modifier == CssMinMaxMediaConditionInterface::MIN) {
138 4
            $this->$minProperty = ($this->$minProperty === null) ? $value : (min($value, $this->$minProperty));
139 4
            return;
140
        }
141
142
        // Maximum value
143 2
        if ($modifier == CssMinMaxMediaConditionInterface::MAX) {
144 2
            $this->$maxProperty = ($this->$maxProperty === null) ? $value : (max($value, $this->$maxProperty));
145 2
            return;
146
        }
147
148 1
        $this->$minProperty = $this->$maxProperty = $value;
149 1
    }
150
151
    /**
152
     * Return the media condition string
153
     *
154
     * @return string Media condition string
155
     */
156 2
    public function getValue(): string
157
    {
158 2
        return $this->value;
159
    }
160
161
    /**
162
     * Return the size and resolution conditions
163
     *
164
     * @return CssMinMaxMediaConditionInterface[] Size and resolution conditions
165
     */
166 5
    public function getConditions(): array
167
    {
168 5
        return $this->conditions;
169
    }
170
171
    /**
172
     * Test if this source size condition matches a particular width and density
173
     *
174
     * @param AbsoluteLengthInterface $width Width
175
     * @param float $density                 Density
176
     *
177
     * @return bool This source size condition matches
178
     */
179 1
    public function matches(AbsoluteLengthInterface $width, float $density): bool
180
    {
181 1
        $match = true;
182
183
        // Run through all conditions
184
        /** @var CssMinMaxMediaConditionInterface $condition */
185 1
        foreach ($this->conditions as $condition) {
186 1
            $value = ($condition instanceof WidthMediaCondition) ? $width->getValue() : $density;
187 1
            if (!$condition->matches($value)) {
188 1
                $match = false;
189 1
                break;
190
            }
191
        }
192
193 1
        return $match;
194
    }
195
196
    /**
197
     * Return the minimum width
198
     *
199
     * @return int|null Minimum width
200
     */
201 1
    public function getMinimumWidth(): ?int
202
    {
203 1
        return $this->minimumWidth;
204
    }
205
206
    /**
207
     * Return the maximum width
208
     *
209
     * @return int|null Maximum width
210
     */
211 1
    public function getMaximumWidth(): ?int
212
    {
213 1
        return $this->maximumWidth;
214
    }
215
216
    /**
217
     * Return the minimum resolution
218
     *
219
     * @return float Minimum resolution
220
     */
221
    public function getMinimumResolution(): float
222
    {
223
        return $this->minimumResolution;
224
    }
225
226
    /**
227
     * Return the maximum resolution
228
     *
229
     * @return float Maximum resolution
230
     */
231
    public function getMaximumResolution(): float
232
    {
233
        return $this->maximumResolution;
234
    }
235
236
    /**
237
     * Initialize a resolution media condition
238
     *
239
     * @param WidthMediaCondition $condition Resolution media condition
240
     */
241
    protected function initializeResolutionCondition(WidthMediaCondition $condition)
0 ignored issues
show
Unused Code introduced by
The parameter $condition is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

241
    protected function initializeResolutionCondition(/** @scrutinizer ignore-unused */ WidthMediaCondition $condition)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
242
    {
243
244
    }
245
}