Passed
Push — master ( a57d9e...3a516f )
by Joschi
02:19
created

CssMediaConditionFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 15.63%

Importance

Changes 0
Metric Value
dl 0
loc 87
ccs 5
cts 32
cp 0.1563
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createMediaCondition() 0 8 1
A createFromWidthMediaCondition() 0 9 1
A createFromResolutionMediaCondition() 0 17 1
A createFromMediaCondition() 0 14 3
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\Domain\Contract\CssMediaConditionInterface as DomainMediaConditionInterface;
40
use Jkphl\Respimgcss\Domain\Model\Css\ResolutionMediaCondition;
41
use Jkphl\Respimgcss\Domain\Model\Css\WidthMediaCondition;
42
use Jkphl\Respimgcss\Infrastructure\CssMediaConditionInterface as RenderableMediaConditionInterface;
43
44
/**
45
 * CSS media condition factory
46
 *
47
 * @package    Jkphl\Respimgcss
48
 * @subpackage Jkphl\Respimgcss\Infrastructure
49
 */
50
class CssMediaConditionFactory
51
{
52
    /**
53
     * Create renderable media conditions from a domain media condition
54
     *
55
     * @param DomainMediaConditionInterface $mediaCondition Domain media condition
56
     *
57
     * @return RenderableMediaConditionInterface[] Renderable media conditions
58
     */
59 1
    public static function createFromMediaCondition(DomainMediaConditionInterface $mediaCondition): array
60
    {
61
        switch (true) {
62 1
            case $mediaCondition instanceof ResolutionMediaCondition:
63
                $renderableMediaConditions = self::createFromResolutionMediaCondition($mediaCondition);
64
                break;
65 1
            case $mediaCondition instanceof WidthMediaCondition:
66
                $renderableMediaConditions = self::createFromWidthMediaCondition($mediaCondition);
67
                break;
68
            default:
69 1
                $renderableMediaConditions = [];
70
        }
71
72 1
        return $renderableMediaConditions;
73
    }
74
75
    /**
76
     * Create renderable media conditions from a resolution media condition
77
     *
78
     * @param ResolutionMediaCondition $resolutionMediaCondition Resolution media condition
79
     *
80
     * @return RenderableMediaConditionInterface[] Renderable media conditions
81
     */
82
    protected static function createFromResolutionMediaCondition(
83
        ResolutionMediaCondition $resolutionMediaCondition
84
    ): array {
85
        $resolutionValue      = $resolutionMediaCondition->getValue()->getValue();
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

85
        $resolutionValue      = $resolutionMediaCondition->getValue()->/** @scrutinizer ignore-call */ getValue();

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...
86
        $resolutionModifier   = $resolutionMediaCondition->getModifier();
87
        $resolutionProperties = ['-webkit-%sdevice-pixel-ratio', '%sresolution', '%sresolution'];
88
        $resolutionValues     = [
89
            strval($resolutionValue),
90
            round($resolutionValue * 96).'dpi',
91
            $resolutionValue.'ddpx'
92
        ];
93
        return array_map(
94
            function ($resolutionProperty, $resolutionValue) use ($resolutionModifier) {
95
                return self::createMediaCondition($resolutionProperty, $resolutionModifier, $resolutionValue);
96
            },
97
            $resolutionProperties,
98
            $resolutionValues
99
        );
100
    }
101
102
    /**
103
     * Create a renderable media condition
104
     *
105
     * @param string $property Condition property
106
     * @param string $modifier Condition modifier
107
     * @param string $value    Condition value
108
     *
109
     * @return CssMediaConditionInterface Renderable media condition
110
     */
111
    protected static function createMediaCondition(
112
        string $property,
113
        string $modifier,
114
        string $value
115
    ): CssMediaConditionInterface {
116
        $rule = new CssMediaConditionRule(sprintf($property, $modifier));
117
        $rule->setValue($value);
118
        return new CssMediaCondition($rule);
119
    }
120
121
    /**
122
     * Create renderable media conditions from a width media condition
123
     *
124
     * @param WidthMediaCondition $widthMediaCondition Resolution media condition
125
     *
126
     * @return RenderableMediaConditionInterface[] Renderable media conditions
127
     */
128
    protected static function createFromWidthMediaCondition(
129
        WidthMediaCondition $widthMediaCondition
130
    ): array {
131
        $widthValue    = $widthMediaCondition->getValue();
132
        $widthModifier = $widthMediaCondition->getModifier();
133
        $widthRule     = new CssMediaConditionRule(sprintf('%swidth', $widthModifier));
134
        $widthRule->setValue($widthValue->getValue());
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

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

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...
135
136
        return [new CssMediaCondition($widthRule)];
137
    }
138
}