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

compileForImageCandidates()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 14
ccs 7
cts 7
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\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\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\ResolutionMediaCondition;
48
use Jkphl\Respimgcss\Domain\Model\Css\Rule;
49
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition;
50
use Jkphl\Respimgcss\Tests\Domain\Mock\AbsoluteLength;
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 6
    public function __construct(
77
        CssRulesetInterface $cssRuleset,
78
        array $breakpoints,
79
        ImageCandidateSetInterface $imageCandidates,
80
        LengthFactoryInterface $lengthFactory,
81
        SourceSizeListInterface $sourceSizeList = null
82
    ) {
83 6
        parent::__construct($cssRuleset, $breakpoints, $imageCandidates, $lengthFactory);
84 6
        $this->sourceSizeList = $sourceSizeList;
85 6
    }
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 4
    public function compile(float $density): CssRulesetInterface
95
    {
96 4
        ($this->sourceSizeList instanceof SourceSizeListInterface) ?
97 1
            $this->compileForSourceSizes($density) :
98 3
            $this->compileForImageCandidates($density);
99
100 4
        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 1
    protected function compileForSourceSizes(float $density): void
109
    {
110
        // Run through all breakpoints
111
        /** @var AbsoluteLengthInterface $breakpoint */
112 1
        foreach ($this->breakpoints as $breakpoint) {
113 1
            $imageCandidateMatch = $this->sourceSizeList->findImageCandidate(
0 ignored issues
show
Bug introduced by
The method findImageCandidate() does not exist on null. ( Ignorable by Annotation )

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

113
            /** @scrutinizer ignore-call */ 
114
            $imageCandidateMatch = $this->sourceSizeList->findImageCandidate(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114 1
                $this->imageCandidates,
115 1
                $breakpoint,
116 1
                $density
117
            );
118 1
            if ($imageCandidateMatch instanceof SourceSizeImageCandidateMatch) {
119 1
                $this->cssRuleset->appendRule(
120 1
                    $this->createSourceSizeMatchRule($imageCandidateMatch, $density)
121
                );
122
            }
123
        }
124 1
    }
125
126
    /**
127
     * Compile a CSS ruleset based on the registered breakpoints, image candidates and a given density
128
     *
129
     * @param float $density Density
130
     */
131 3
    protected function compileForImageCandidates(float $density): void
132
    {
133
        // Initialize a virtual breakpoint
134 3
        $lastImageCandidateWidth = 0;
135
136
        // Run through and test all image candidates
137
        /** @var ImageCandidateInterface $imageCandidate */
138 3
        foreach ($this->imageCandidates as $imageCandidate) {
139 3
            if ($lastImageCandidateWidth || ($density == 1)) {
140 3
                $this->cssRuleset->appendRule(
141 3
                    $this->createImageCandidateRule($imageCandidate, $density, $lastImageCandidateWidth)
142
                );
143
            }
144 3
            $lastImageCandidateWidth = $imageCandidate->getValue();
145
        }
146 3
    }
147
148
    /**
149
     * Create a width CSS rule for a particular image candidate
150
     *
151
     * @param ImageCandidateInterface $imageCandidate Image candidate
152
     * @param float $density                          Density
153
     * @param int $imageCandidateWidth                Image candidate width
154
     *
155
     * @return Rule Density CSS rule
156
     */
157 3
    protected function createImageCandidateRule(
158
        ImageCandidateInterface $imageCandidate,
159
        float $density,
160
        int $imageCandidateWidth
161
    ): Rule {
162 3
        $rule = new Rule($imageCandidate);
163 3
        $rule = $this->addWidthCondition($rule, $imageCandidateWidth, $density);
164
165 3
        return $this->addDensityCondition($rule, $density);
166
    }
167
168
    /**
169
     * Create a source size based CSS rule
170
     *
171
     * @param SourceSizeImageCandidateMatch $imageCandidateMatch Source size match
172
     * @param float $density                                     Density
173
     *
174
     * @return Rule Source size based CSS rule
175
     */
176 1
    protected function createSourceSizeMatchRule(SourceSizeImageCandidateMatch $imageCandidateMatch, float $density)
177
    {
178 1
        $rule = new Rule($imageCandidateMatch->getImageCandidate(), [$imageCandidateMatch->getMediaCondition()]);
179
180 1
        return $this->addDensityCondition($rule, $density);
181
    }
182
183
    /**
184
     * Add a width condition to a CSS rule
185
     *
186
     * @param Rule $rule               CSS rule
187
     * @param int $imageCandidateWidth Image candidate width in pixels
188
     * @param float $density           Density
189
     *
190
     * @return Rule CSS rule
191
     */
192 3
    protected function addWidthCondition(Rule $rule, int $imageCandidateWidth, float $density): Rule
193
    {
194
        // If this is not the minimum width: Add a width condition
195 3
        if ($imageCandidateWidth) {
196 3
            $breakpoint          = new AbsoluteLength(round($imageCandidateWidth) / $density);
197 3
            $widthMediaCondition = new WidthMediaCondition($breakpoint, CssMinMaxMediaConditionInterface::MIN);
198 3
            $rule                = $rule->appendCondition($widthMediaCondition);
199
        }
200
201 3
        return $rule;
202
    }
203
204
    /**
205
     * Add a density condition to a CSS rule
206
     *
207
     * @param Rule $rule     CSS rule
208
     * @param float $density Density
209
     *
210
     * @return Rule CSS rule
211
     */
212 4
    protected function addDensityCondition(Rule $rule, float $density): Rule
213
    {
214
        // If this is not the default density: Add a resolution condition
215 4
        if ($density > 1) {
216 3
            $resolutionMediaCondition = new ResolutionMediaCondition(
217 3
                new AbsoluteLength($density),
218 3
                CssMinMaxMediaConditionInterface::MIN
219
            );
220 3
            $rule                     = $rule->appendCondition($resolutionMediaCondition);
221
        }
222
223 4
        return $rule;
224
    }
225
}