Completed
Push — master ( d21468...5b2b8e )
by Joschi
02:16
created

SourceSizeMediaCondition::matches()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 5
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 9.2
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\WidthMediaCondition;
42
43
/**
44
 * Source size media condition
45
 *
46
 * @package    Jkphl\Respimgcss
47
 * @subpackage Jkphl\Respimgcss\Application\Model
48
 */
49
class SourceSizeMediaCondition
50
{
51
    /**
52
     * Media condition string
53
     *
54
     * @var string
55
     */
56
    protected $value;
57
    /**
58
     * Size and resolution conditions
59
     *
60
     * @var CssMinMaxMediaConditionInterface[]
61
     */
62
    protected $conditions;
63
64
    /**
65
     * Source size media condition constructor
66
     *
67
     * @param string $value                                  Media condition string
68
     * @param CssMinMaxMediaConditionInterface[] $conditions Size and resolution conditions
69
     */
70 10
    public function __construct(string $value, array $conditions = [])
71
    {
72 10
        $this->value      = $value;
73 10
        $this->conditions = $conditions;
74 10
    }
75
76
    /**
77
     * Return the media condition string
78
     *
79
     * @return string Media condition string
80
     */
81 4
    public function getValue(): string
82
    {
83 4
        return $this->value;
84
    }
85
86
    /**
87
     * Return the size and resolution conditions
88
     *
89
     * @return CssMinMaxMediaConditionInterface[] Size and resolution conditions
90
     */
91 4
    public function getConditions(): array
92
    {
93 4
        return $this->conditions;
94
    }
95
96
    /**
97
     * Test if this source size condition matches a particular width and density
98
     *
99
     * @param AbsoluteLengthInterface $width Width
100
     * @param float $density                 Density
101
     *
102
     * @return bool This source size condition matches
103
     */
104 1
    public function matches(AbsoluteLengthInterface $width, float $density): bool
105
    {
106 1
        $match = true;
107
108
        // Run through all conditions
109
        /** @var CssMinMaxMediaConditionInterface $condition */
110 1
        foreach ($this->conditions as $condition) {
111 1
            $value = ($condition instanceof WidthMediaCondition) ? $width->getValue() : $density;
112 1
            if (!$condition->matches($value)) {
113 1
                $match = false;
114 1
                break;
115
            }
116
        }
117
118 1
        return $match;
119
    }
120
}