WidthCssRulesetCompilerService   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 173
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 0 7 2
A __construct() 0 9 1
A addWidthCondition() 0 10 2
A compileForImageCandidates() 0 14 4
A compileForSourceSizes() 0 15 3
A createImageCandidateRule() 0 9 1
A createSourceSizeMatchRule() 0 10 1
A addDensityCondition() 0 12 2
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\Service;
38
39
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface;
40
use Jkphl\Respimgcss\Domain\Contract\CssMinMaxMediaConditionInterface;
41
use Jkphl\Respimgcss\Domain\Contract\CssRulesetInterface;
42
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateInterface;
43
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateSetInterface;
44
use Jkphl\Respimgcss\Domain\Contract\LengthFactoryInterface;
45
use Jkphl\Respimgcss\Domain\Contract\SourceSizeImageCandidateMatch;
46
use Jkphl\Respimgcss\Domain\Contract\SourceSizeListInterface;
47
use Jkphl\Respimgcss\Domain\Model\Css\MediaCondition;
48
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition;
49
use Jkphl\Respimgcss\Domain\Model\Css\Rule;
50
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition;
51
52
/**
53
 * Pixel density CSS ruleset compiler service
54
 *
55
 * @package    Jkphl\Respimgcss
56
 * @subpackage Jkphl\Respimgcss\Domain
57
 */
58
class WidthCssRulesetCompilerService extends AbstractCssRulesetCompilerService
59
{
60
    /**
61
     * Source sizes list
62
     *
63
     * @var SourceSizeListInterface|null
64
     */
65
    protected $sourceSizeList;
66
67
    /**
68
     * Width CSS Ruleset Compiler Service constructor
69
     *
70
     * @param CssRulesetInterface $cssRuleset              CSS Ruleset
71
     * @param AbsoluteLengthInterface[] $breakpoints       Breakpoints
72
     * @param ImageCandidateSetInterface $imageCandidates  Image candidates
73
     * @param LengthFactoryInterface $lengthFactory        Length factory
74
     * @param SourceSizeListInterface|null $sourceSizeList Source sizes list
75
     */
76 8
    public function __construct(
77
        CssRulesetInterface $cssRuleset,
78
        array $breakpoints,
79
        ImageCandidateSetInterface $imageCandidates,
80
        LengthFactoryInterface $lengthFactory,
81
        SourceSizeListInterface $sourceSizeList = null
82
    ) {
83 8
        parent::__construct($cssRuleset, $breakpoints, $imageCandidates, $lengthFactory);
84 8
        $this->sourceSizeList = $sourceSizeList;
85 8
    }
86
87
    /**
88
     * Compile a CSS ruleset for a given density
89
     *
90
     * @param float $density Density
91
     *
92
     * @return CssRulesetInterface CSS ruleset
93
     */
94 6
    public function compile(float $density): CssRulesetInterface
95
    {
96 6
        ($this->sourceSizeList instanceof SourceSizeListInterface) ?
97 3
            $this->compileForSourceSizes($density) :
98 3
            $this->compileForImageCandidates($density);
99
100 6
        return $this->cssRuleset;
101
    }
102
103
    /**
104
     * Compile a CSS ruleset based on a list of source sizes
105
     *
106
     * @param float $density Density
107
     */
108 3
    protected function compileForSourceSizes(float $density): void
109
    {
110
        // Run through all breakpoints (from lowest to highest)
111
        /** @var AbsoluteLengthInterface $breakpoint */
112 3
        foreach ($this->breakpoints as $breakpoint) {
113
            // Try to find a matching image candidate
114
            /** @scrutinizer ignore-call */
115 3
            $imageCandidateMatch = $this->sourceSizeList->findImageCandidate(
116 3
                $this->imageCandidates,
117 3
                $breakpoint,
118 3
                $density
119
            );
120 3
            if ($imageCandidateMatch instanceof SourceSizeImageCandidateMatch) {
121 3
                $this->cssRuleset->appendRule(
122 3
                    $this->createSourceSizeMatchRule($imageCandidateMatch, $density)
123
                );
124
            }
125
        }
126 3
    }
127
128
    /**
129
     * Create a source size based CSS rule
130
     *
131
     * @param SourceSizeImageCandidateMatch $imageCandidateMatch Source size match
132
     * @param float $density                                     Density
133
     *
134
     * @return Rule Source size based CSS rule
135
     */
136 3
    protected function createSourceSizeMatchRule(SourceSizeImageCandidateMatch $imageCandidateMatch, float $density)
137
    {
138 3
        $rule = new Rule(
139 3
            $imageCandidateMatch->getImageCandidate(),
140 3
            array_filter([$imageCandidateMatch->getMediaCondition()], function (MediaCondition $mediaCondition) {
141 3
                return !!$mediaCondition->getValue();
142 3
            })
143
        );
144
145 3
        return $this->addDensityCondition($rule, $density);
146
    }
147
148
    /**
149
     * Add a density condition to a CSS rule
150
     *
151
     * @param Rule $rule     CSS rule
152
     * @param float $density Density
153
     *
154
     * @return Rule CSS rule
155
     */
156 6
    protected function addDensityCondition(Rule $rule, float $density): Rule
157
    {
158
        // If this is not the default density: Add a resolution condition
159 6
        if ($density > 1) {
160 5
            $resolutionMediaCondition = new ResolutionMediaCondition(
161 5
                $this->lengthFactory->createAbsoluteLength($density),
162 5
                CssMinMaxMediaConditionInterface::MIN
163
            );
164 5
            $rule                     = $rule->appendCondition($resolutionMediaCondition);
165
        }
166
167 6
        return $rule;
168
    }
169
170
    /**
171
     * Compile a CSS ruleset based on the registered breakpoints, image candidates and a given density
172
     *
173
     * @param float $density Density
174
     */
175 3
    protected function compileForImageCandidates(float $density): void
176
    {
177
        // Initialize a virtual breakpoint
178 3
        $lastImageCandidateWidth = 0;
179
180
        // Run through and test all image candidates
181
        /** @var ImageCandidateInterface $imageCandidate */
182 3
        foreach ($this->imageCandidates as $imageCandidate) {
183 3
            if ($lastImageCandidateWidth || ($density == 1)) {
184 3
                $this->cssRuleset->appendRule(
185 3
                    $this->createImageCandidateRule($imageCandidate, $density, $lastImageCandidateWidth)
186
                );
187
            }
188 3
            $lastImageCandidateWidth = $imageCandidate->getValue();
189
        }
190 3
    }
191
192
    /**
193
     * Create a width CSS rule for a particular image candidate
194
     *
195
     * @param ImageCandidateInterface $imageCandidate Image candidate
196
     * @param float $density                          Density
197
     * @param int $imageCandidateWidth                Image candidate width
198
     *
199
     * @return Rule Density CSS rule
200
     */
201 3
    protected function createImageCandidateRule(
202
        ImageCandidateInterface $imageCandidate,
203
        float $density,
204
        int $imageCandidateWidth
205
    ): Rule {
206 3
        $rule = new Rule($imageCandidate);
207 3
        $rule = $this->addWidthCondition($rule, $imageCandidateWidth, $density);
208
209 3
        return $this->addDensityCondition($rule, $density);
210
    }
211
212
    /**
213
     * Add a width condition to a CSS rule
214
     *
215
     * @param Rule $rule               CSS rule
216
     * @param int $imageCandidateWidth Image candidate width in pixels
217
     * @param float $density           Density
218
     *
219
     * @return Rule CSS rule
220
     */
221 3
    protected function addWidthCondition(Rule $rule, int $imageCandidateWidth, float $density): Rule
222
    {
223
        // If this is not the minimum width: Add a width condition
224 3
        if ($imageCandidateWidth) {
225 3
            $breakpoint          = $this->lengthFactory->createAbsoluteLength(round($imageCandidateWidth) / $density);
226 3
            $widthMediaCondition = new WidthMediaCondition($breakpoint, CssMinMaxMediaConditionInterface::MIN);
227 3
            $rule                = $rule->appendCondition($widthMediaCondition);
228
        }
229
230 3
        return $rule;
231
    }
232
}
233