Passed
Push — master ( 36a68f...568aa3 )
by Joschi
02:18
created

createFromWidthMediaCondition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 9.6666
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
        $renderableMediaConditions = [];
86 6
        $resolutionValue           = $resolutionMediaCondition->getValue()->getValue();
87 6
        $resolutionModifier        = $resolutionMediaCondition->getModifier();
88
89
        // -webkit-device-pixel-ratio media condition
90 6
        $webkitDevicePixelRatioRule = new CssMediaConditionRule(
91 6
            sprintf('-webkit-%sdevice-pixel-ratio', $resolutionModifier)
92
        );
93 6
        $webkitDevicePixelRatioRule->setValue($resolutionValue);
94 6
        $renderableMediaConditions[] = new CssMediaCondition($webkitDevicePixelRatioRule);
95
96
        // resolution media condition (dpi)
97 6
        $resolutionRule = new CssMediaConditionRule(sprintf('%sresolution', $resolutionModifier));
98 6
        $resolutionRule->setValue(round($resolutionValue * 96).'dpi');
99 6
        $renderableMediaConditions[] = new CssMediaCondition($resolutionRule);
100
101
        // resolution media condition (dppx)
102 6
        $resolutionRule = new CssMediaConditionRule(sprintf('%sresolution', $resolutionModifier));
103 6
        $resolutionRule->setValue($resolutionValue.'ddpx');
104 6
        $renderableMediaConditions[] = new CssMediaCondition($resolutionRule);
105
106 6
        return $renderableMediaConditions;
107
    }
108
109
    /**
110
     * Create renderable media conditions from a width media condition
111
     *
112
     * @param WidthMediaCondition $widthMediaCondition Resolution media condition
113
     *
114
     * @return RenderableMediaConditionInterface[] Renderable media conditions
115
     */
116 2
    protected static function createFromWidthMediaCondition(
117
        WidthMediaCondition $widthMediaCondition
118
    ): array {
119 2
        $widthValue    = $widthMediaCondition->getValue();
120 2
        $widthModifier = $widthMediaCondition->getModifier();
121 2
        $widthRule     = new CssMediaConditionRule(sprintf('%swidth', $widthModifier));
122 2
        $widthRule->setValue(strval($widthValue));
123
124 2
        return [new CssMediaCondition($widthRule)];
125
    }
126
}