Passed
Push — master ( 1ca4f4...beacde )
by Joschi
02:44
created

createFromResolutionMediaCondition()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2.0009

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 1
dl 0
loc 24
ccs 15
cts 16
cp 0.9375
crap 2.0009
rs 8.9713
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\Infrastructure
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\Infrastructure;
38
39
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface;
40
use Jkphl\Respimgcss\Domain\Contract\AbsoluteLengthInterface;
41
use Jkphl\Respimgcss\Domain\Contract\CssMediaConditionInterface as DomainMediaConditionInterface;
42
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition;
43
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition;
44
use Jkphl\Respimgcss\Infrastructure\CssMediaConditionInterface as RenderableMediaConditionInterface;
45
46
/**
47
 * CSS media condition factory
48
 *
49
 * @package    Jkphl\Respimgcss
50
 * @subpackage Jkphl\Respimgcss\Infrastructure
51
 */
52
class CssMediaConditionFactory
53
{
54
    /**
55
     * Create renderable media conditions from a domain media condition
56
     *
57
     * @param DomainMediaConditionInterface $mediaCondition Domain media condition
58
     *
59
     * @return RenderableMediaConditionInterface[] Renderable media conditions
60
     */
61 9
    public static function createFromMediaCondition(DomainMediaConditionInterface $mediaCondition): array
62
    {
63
        switch (true) {
64 9
            case $mediaCondition instanceof ResolutionMediaCondition:
65 6
                $renderableMediaConditions = self::createFromResolutionMediaCondition($mediaCondition);
66 6
                break;
67 6
            case $mediaCondition instanceof WidthMediaCondition:
68 4
                $renderableMediaConditions = self::createFromWidthMediaCondition($mediaCondition);
69 4
                break;
70
            default:
71
                $renderableMediaConditions = [
72 2
                    self::createMediaCondition(
73 2
                        $mediaCondition->getProperty(),
74 2
                        '',
75 2
                        $mediaCondition->getValue()
76
                    )
77
                ];
78
        }
79
80 9
        return $renderableMediaConditions;
81
    }
82
83
    /**
84
     * Create renderable media conditions from a resolution media condition
85
     *
86
     * @param ResolutionMediaCondition $resolutionMediaCondition Resolution media condition
87
     *
88
     * @return RenderableMediaConditionInterface[] Renderable media conditions
89
     */
90 6
    protected static function createFromResolutionMediaCondition(
91
        ResolutionMediaCondition $resolutionMediaCondition
92
    ): array {
93 6
        $resolutionConditionValue = $resolutionMediaCondition->getValue();
94 6
        if ($resolutionConditionValue instanceof AbsoluteLengthInterface) {
0 ignored issues
show
introduced by
$resolutionConditionValue is always a sub-type of Jkphl\Respimgcss\Domain\...AbsoluteLengthInterface.
Loading history...
95 6
            $resolutionValue      = $resolutionConditionValue->getValue();
96 6
            $resolutionModifier   = $resolutionMediaCondition->getModifier();
97 6
            $resolutionProperties = ['-webkit-%sdevice-pixel-ratio', '%sresolution', '%sresolution'];
98
            $resolutionValues     = [
99 6
                strval($resolutionValue),
100 6
                round($resolutionValue * 96).'dpi',
101 6
                $resolutionValue.'ddpx'
102
            ];
103
104 6
            return array_map(
105 6
                function ($resolutionProperty, $resolutionValue) use ($resolutionModifier) {
106 6
                    return self::createMediaCondition($resolutionProperty, $resolutionModifier, $resolutionValue);
107 6
                },
108 6
                $resolutionProperties,
109 6
                $resolutionValues
110
            );
111
        }
112
113
        return [];
114
    }
115
116
    /**
117
     * Create a renderable media condition
118
     *
119
     * @param string $property Condition property
120
     * @param string $modifier Condition modifier
121
     * @param string $value    Condition value
122
     *
123
     * @return CssMediaConditionInterface Renderable media condition
124
     */
125 8
    protected static function createMediaCondition(
126
        string $property,
127
        string $modifier,
128
        string $value
129
    ): CssMediaConditionInterface {
130 8
        $rule = new CssMediaConditionRule(sprintf($property, $modifier));
131 8
        $rule->setValue($value);
132
133 8
        return new CssMediaCondition($rule);
134
    }
135
136
    /**
137
     * Create renderable media conditions from a width media condition
138
     *
139
     * @param WidthMediaCondition $widthMediaCondition Resolution media condition
140
     *
141
     * @return RenderableMediaConditionInterface[] Renderable media conditions
142
     */
143 4
    protected static function createFromWidthMediaCondition(WidthMediaCondition $widthMediaCondition): array
144
    {
145 4
        $widthValue    = $widthMediaCondition->getValue();
146 4
        $widthUnit     = ($widthValue instanceof UnitLengthInterface) ? $widthValue->getUnit() : '';
147 4
        $widthModifier = $widthMediaCondition->getModifier();
148 4
        $widthRule     = new CssMediaConditionRule(sprintf('%swidth', $widthModifier));
149 4
        $widthRule->setValue($widthValue->getValue().$widthUnit);
0 ignored issues
show
Bug introduced by
The call to Jkphl\Respimgcss\Domain\...thInterface::getValue() has too few arguments starting with viewport. ( Ignorable by Annotation )

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

149
        $widthRule->setValue($widthValue->/** @scrutinizer ignore-call */ getValue().$widthUnit);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
150
151 4
        return [new CssMediaCondition($widthRule)];
152
    }
153
}