Passed
Push — master ( 3a516f...f0e18d )
by Joschi
02:33 queued 18s
created

createFromResolutionMediaCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
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 17
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\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 7
    public static function createFromMediaCondition(DomainMediaConditionInterface $mediaCondition): array
60
    {
61
        switch (true) {
62 7
            case $mediaCondition instanceof ResolutionMediaCondition:
63 6
                $renderableMediaConditions = self::createFromResolutionMediaCondition($mediaCondition);
64 6
                break;
65 3
            case $mediaCondition instanceof WidthMediaCondition:
66 2
                $renderableMediaConditions = self::createFromWidthMediaCondition($mediaCondition);
67 2
                break;
68
            default:
69 1
                $renderableMediaConditions = [];
70
        }
71
72 7
        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 6
    protected static function createFromResolutionMediaCondition(
83
        ResolutionMediaCondition $resolutionMediaCondition
84
    ): array {
85 6
        $resolutionValue      = $resolutionMediaCondition->getValue()->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Jkphl\Respimgcss\Domain\Contract\LengthInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Jkphl\Respimgcss\Domain\Model\AbstractLength or Jkphl\Respimgcss\Applica...act\UnitLengthInterface or Jkphl\Respimgcss\Application\Model\AbstractLength or Jkphl\Respimgcss\Application\Model\AbstractLength. Are you sure you never get one of those? ( 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();
Loading history...
86 6
        $resolutionModifier   = $resolutionMediaCondition->getModifier();
87 6
        $resolutionProperties = ['-webkit-%sdevice-pixel-ratio', '%sresolution', '%sresolution'];
88
        $resolutionValues     = [
89 6
            strval($resolutionValue),
90 6
            round($resolutionValue * 96).'dpi',
91 6
            $resolutionValue.'ddpx'
92
        ];
93 6
        return array_map(
94 6
            function ($resolutionProperty, $resolutionValue) use ($resolutionModifier) {
95 6
                return self::createMediaCondition($resolutionProperty, $resolutionModifier, $resolutionValue);
96 6
            },
97 6
            $resolutionProperties,
98 6
            $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 6
    protected static function createMediaCondition(
112
        string $property,
113
        string $modifier,
114
        string $value
115
    ): CssMediaConditionInterface {
116 6
        $rule = new CssMediaConditionRule(sprintf($property, $modifier));
117 6
        $rule->setValue($value);
118 6
        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 2
    protected static function createFromWidthMediaCondition(
129
        WidthMediaCondition $widthMediaCondition
130
    ): array {
131 2
        $widthValue    = $widthMediaCondition->getValue();
132 2
        $widthModifier = $widthMediaCondition->getModifier();
133 2
        $widthRule     = new CssMediaConditionRule(sprintf('%swidth', $widthModifier));
134 2
        $widthRule->setValue($widthValue->getValue());
135
136 2
        return [new CssMediaCondition($widthRule)];
137
    }
138
}