AbstractMinMaxMediaCondition   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 85
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getModifier() 0 3 1
A matches() 0 13 3
A __construct() 0 15 4
A getValue() 0 3 1
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Domain
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\Domain\Model\Css;
38
39
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface;
40
use Jkphl\Respimgcss\Domain\Contract\CssMinMaxMediaConditionInterface;
41
use Jkphl\Respimgcss\Domain\Exceptions\InvalidArgumentException;
42
43
/**
44
 * Abstract min/max media condition
45
 *
46
 * @package    Jkphl\Respimgcss
47
 * @subpackage Jkphl\Respimgcss\Domain
48
 */
49
abstract class AbstractMinMaxMediaCondition extends MediaCondition implements CssMinMaxMediaConditionInterface
50
{
51
    /**
52
     * Property name
53
     *
54
     * @var string
55
     */
56
    const PROPERTY = 'none';
57
    /**
58
     * Property modifier
59
     *
60
     * @var string
61
     */
62
    protected $modifier = self::EQ;
63
    /**
64
     * Property value
65
     *
66
     * @var AbsoluteLengthInterface
67
     */
68
    protected $value;
69
70
    /**
71
     * Min/Max CSS media condition constructor
72
     *
73
     * @param AbsoluteLengthInterface $value Property value
74
     * @param string $modifier               Condition modifier
75
     *
76
     */
77 27
    public function __construct(AbsoluteLengthInterface $value, string $modifier = self::EQ)
78
    {
79 27
        parent::__construct(static::PROPERTY, $value);
80
81 27
        if (($modifier !== self::EQ)
82 27
            && ($modifier !== self::MIN)
83 27
            && ($modifier !== self::MAX)
84
        ) {
85 2
            throw new InvalidArgumentException(
86 2
                sprintf(InvalidArgumentException::INVALID_CSS_CONDITION_MODIFIER_STR, $modifier),
87 2
                InvalidArgumentException::INVALID_CSS_CONDITION_MODIFIER
88
            );
89
        }
90
91 25
        $this->modifier = $modifier;
92 25
    }
93
94
    /**
95
     * Return the property modifier
96
     *
97
     * @return string Property modifier
98
     */
99 22
    public function getModifier(): string
100
    {
101 22
        return $this->modifier;
102
    }
103
104
    /**
105
     * Return the property value
106
     *
107
     * @return AbsoluteLengthInterface Property value
108
     */
109 22
    public function getValue(): AbsoluteLengthInterface
110
    {
111 22
        return $this->value;
112
    }
113
114
    /**
115
     * Test whether this condition matches a value
116
     *
117
     * @param float $value Value
118
     *
119
     * @return bool Successful match
120
     */
121 8
    public function matches(float $value): bool
122
    {
123 8
        $conditionValue = $this->value->getValue();
124
125 8
        if ($this->modifier === self::MIN) {
126 7
            return $value >= $conditionValue;
127
        }
128
129 4
        if ($this->modifier === self::MAX) {
130 3
            return $value <= $conditionValue;
131
        }
132
133 2
        return $value == $conditionValue;
134
    }
135
}
136