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

Generator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 8
ccs 7
cts 7
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\Application\Factory\ImageCandidateFactory;
41
use Jkphl\Respimgcss\Application\Factory\LengthFactory;
42
use Jkphl\Respimgcss\Application\Model\ImageCandidateSet;
43
use Jkphl\Respimgcss\Application\Service\CssRulesetCompilerService;
44
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateInterface;
45
use Jkphl\Respimgcss\Ports\CssRuleset;
46
use Jkphl\Respimgcss\Ports\CssRulesetInterface;
47
use Jkphl\Respimgcss\Ports\GeneratorInterface;
48
49
/**
50
 * Responsive image CSS generator (internal)
51
 *
52
 * @package    Jkphl\Respimgcss
53
 * @subpackage Jkphl\Respimgcss\Ports
54
 */
55
abstract class Generator implements GeneratorInterface
56
{
57
    /**
58
     * Breakpoints
59
     *
60
     * @var UnitLengthInterface[]
61
     */
62
    protected $breakpoints;
63
    /**
64
     * EM to pixel ratio
65
     *
66
     * @var int
67
     */
68
    protected $emPixel;
69
    /**
70
     * Image candidates
71
     *
72
     * @var ImageCandidateSet
73
     */
74
    protected $imageCandidates = null;
75
76
    /**
77
     * Generator constructor
78
     *
79
     * @param string[] $breakpoints List of breakpoint length strings
80
     * @param int $emPixel          EM to pixel ratio
81
     */
82 4
    public function __construct(array $breakpoints, int $emPixel)
83
    {
84 4
        $this->emPixel     = $emPixel;
85 4
        $lengthFactory     = new LengthFactory($this->emPixel);
86 4
        $this->breakpoints = array_map(
87 4
            [$lengthFactory, 'createLengthFromString'],
88 4
            $breakpoints,
89 4
            array_fill(0, count($breakpoints), $this->emPixel)
90
        );
91 4
    }
92
93
    /**
94
     * Register an image candidate
95
     *
96
     * @param string $file            Image candidate file path and name
97
     * @param string|null $descriptor Image candidate descriptor
98
     *
99
     * @return GeneratorInterface Self reference
100
     * @api
101
     */
102 4
    public function registerImageCandidate(string $file, string $descriptor = null): GeneratorInterface
103
    {
104 4
        $imageCandidate = ($descriptor === null) ?
105 4
            ImageCandidateFactory::createImageCandidateFromString($file) :
106 4
            ImageCandidateFactory::createImageCandidateFromFileAndDescriptor($file, $descriptor);
107
108
        // If the image candidate set doesn't exist yet
109 4
        if ($this->imageCandidates === null) {
110 4
            $this->imageCandidates = new ImageCandidateSet($imageCandidate);
111
112 4
            return $this;
113
        }
114
115
        // Register the image candidate
116 4
        $this->imageCandidates[] = $imageCandidate;
117
118 4
        return $this;
119
    }
120
121
    /**
122
     * Return the registered image candidates
123
     *
124
     * @return ImageCandidateInterface[] Image candidates
125
     */
126 4
    public function getImageCandidates(): array
127
    {
128 4
        return ($this->imageCandidates === null) ? [] : $this->imageCandidates->toArray();
129
    }
130
131
    /**
132
     * Create a CSS rulset for the registered image candidates
133
     *
134
     * @param float[] $densities Device display densities
135
     *
136
     * @return CssRulesetInterface CSS Ruleset
137
     */
138 4
    public function make(array $densities = [1]): CssRulesetInterface
139
    {
140 4
        $cssRuleset = new CssRuleset();
141
142
        // If all necessary properties are given
143 4
        if (count($this->breakpoints) && count($this->imageCandidates) && count($densities)) {
144
            // Instantiate a CSS ruleset compiler service and compile for all densities
145 4
            $cssRulesetCompilerService = new CssRulesetCompilerService(
146 4
                $cssRuleset,
147 4
                $this->breakpoints,
148 4
                $this->imageCandidates,
149 4
                $this->emPixel
150
            );
151 4
            $cssRuleset                = new CssRuleset($cssRulesetCompilerService->compile($densities));
152
        }
153
154 4
        return $cssRuleset;
155
    }
156
}