Passed
Push — master ( beacde...d21468 )
by Joschi
03:06
created

createFromResolutionMediaCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

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

143
        $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...
144
145 4
        return [new CssMediaCondition($widthRule)];
146
    }
147
}