Passed
Push — master ( 36a68f...568aa3 )
by Joschi
02:18
created

createImageCandidateRule()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 3
dl 0
loc 23
ccs 11
cts 11
cp 1
crap 3
rs 9.0856
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\Domain\Service
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\Service;
38
39
use Jkphl\Respimgcss\Domain\Contract\CssMinMaxMediaConditionInterface;
40
use Jkphl\Respimgcss\Domain\Contract\CssRulesetInterface;
41
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateInterface;
42
use Jkphl\Respimgcss\Domain\Contract\LengthInterface;
43
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition;
44
use Jkphl\Respimgcss\Domain\Model\Css\Rule;
45
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition;
46
use Jkphl\Respimgcss\Domain\Model\Length;
47
48
/**
49
 * Pixel density CSS ruleset compiler service
50
 *
51
 * @package    Jkphl\Respimgcss
52
 * @subpackage Jkphl\Respimgcss\Domain
53
 */
54
class WidthCssRulesetCompilerService extends AbstractCssRulesetCompilerService
55
{
56
    /**
57
     * Compile a CSS ruleset based on the registered breakpoints, image candidates and a given density
58
     *
59
     * @param float $density Density
60
     *
61
     * @return CssRulesetInterface CSS ruleset
62
     */
63 3
    public function compile(float $density): CssRulesetInterface
64
    {
65
        // @TODO: Are the breakpoints relevant at all? Why not create custom breakpoints based on the image widths?
66
        // @TODO: If not, then use the image candidate which covers at least the first breakpoint (not the first image candidate in the set)
67
68
        // Compile the minimum size
69 3
        $this->compileBreakpoint($density, null);
70
71
        // Run through and compile for all breakpoints
72 3
        foreach ($this->breakpoints as $breakpoint) {
73 3
            $this->compileBreakpoint($density, $breakpoint);
74
        }
75
76 3
        return $this->cssRuleset;
77
    }
78
79
    /**
80
     * Compile the CSS rules for particular breakpoint, a given density and the registered image candidates
81
     *
82
     * @param float $density                   Device display density
83
     * @param LengthInterface|null $breakpoint Breakpoint length (NULL = minimum size / no breakpoint)
84
     */
85 3
    protected function compileBreakpoint(float $density, LengthInterface $breakpoint = null): void
86
    {
87
        // Calculate the density dependent device value
88 3
        $deviceValue = ($breakpoint === null) ? 0 : ($density * $breakpoint->getValue());
89
90
        // Run through and test all image candidates
91
        /** @var ImageCandidateInterface $imageCandidate */
92 3
        foreach ($this->imageCandidates as $imageCandidate) {
93 3
            if ($imageCandidate->getValue() >= $deviceValue) {
94 3
                $widthRule = $this->createImageCandidateRule($imageCandidate, $density, $breakpoint);
95 3
                $this->cssRuleset->appendRule($widthRule);
96 3
                break;
97
            }
98
        }
99 3
    }
100
101
    /**
102
     * Create a width CSS rule for a particular image candidate
103
     *
104
     * @param ImageCandidateInterface $imageCandidate Image candidate
105
     * @param float $density                          Density
106
     * @param LengthInterface|null $breakpoint        Breakpoint length (NULL = minimum size / no breakpoint)
107
     *
108
     * @return Rule Density CSS rule
109
     */
110 3
    protected function createImageCandidateRule(
111
        ImageCandidateInterface $imageCandidate,
112
        float $density,
113
        LengthInterface $breakpoint = null
114
    ): Rule {
115 3
        $rule = new Rule($imageCandidate);
116
117
        // If this is not the minimum width: Add a width condition
118 3
        if ($breakpoint !== null) {
119 3
            $widthMediaCondition = new WidthMediaCondition($breakpoint, CssMinMaxMediaConditionInterface::MIN);
120 3
            $rule                = $rule->appendCondition($widthMediaCondition);
121
        }
122
123
        // If this is not the default density: Add a resolution condition
124 3
        if ($density != 1) {
125 2
            $resolutionMediaCondition = new ResolutionMediaCondition(
126 2
                new Length($density),
127 2
                CssMinMaxMediaConditionInterface::MIN
128
            );
129 2
            $rule                     = $rule->appendCondition($resolutionMediaCondition);
130
        }
131
132 3
        return $rule;
133
    }
134
}