Completed
Push — master ( c85598...d2510c )
by Joschi
02:15
created

Generator::make()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 2
nop 1
dl 0
loc 16
ccs 9
cts 9
cp 1
crap 4
rs 9.2
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\ImageCandidateSetInterface;
40
use Jkphl\Respimgcss\Application\Contract\UnitLengthInterface;
41
use Jkphl\Respimgcss\Application\Factory\ImageCandidateFactory;
42
use Jkphl\Respimgcss\Application\Factory\LengthFactory;
43
use Jkphl\Respimgcss\Application\Model\ImageCandidateSet;
44
use Jkphl\Respimgcss\Application\Service\CssRulesetCompilerService;
45
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateInterface;
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 1
    public function __construct(array $breakPoints, int $emPixel)
83
    {
84 1
        $this->emPixel     = $emPixel;
85 1
        $this->breakPoints = array_map(
86 1
            [LengthFactory::class, 'createLengthFromString'],
87 1
            $breakPoints,
88 1
            array_fill(0, count($breakPoints), $this->emPixel)
89
        );
90 1
    }
91
92
    /**
93
     * Register an image candidate
94
     *
95
     * @param string $file            Image candidate file path and name
96
     * @param string|null $descriptor Image candidate descriptor
97
     *
98
     * @return GeneratorInterface Self reference
99
     * @api
100
     */
101 1
    public function registerImageCandidate(string $file, string $descriptor = null): GeneratorInterface
102
    {
103 1
        $imageCandidate = ($descriptor === null) ?
104 1
            ImageCandidateFactory::createImageCandidateFromString($file) :
105 1
            ImageCandidateFactory::createImageCandidateFromFileAndDescriptor($file, $descriptor);
106
107
        // If the image candidate set doesn't exist yet
108 1
        if (!($this->imageCandidates instanceof ImageCandidateSetInterface)) {
0 ignored issues
show
introduced by
$this->imageCandidates is always a sub-type of Jkphl\Respimgcss\Applica...geCandidateSetInterface.
Loading history...
109 1
            $this->imageCandidates = new ImageCandidateSet($imageCandidate);
110
111 1
            return $this;
112
        }
113
114
        // Register the image candidate
115 1
        $this->imageCandidates[] = $imageCandidate;
116
117 1
        return $this;
118
    }
119
120
    /**
121
     * Return the registered image candidates
122
     *
123
     * @return ImageCandidateInterface[] Image candidates
124
     */
125 1
    public function getImageCandidates(): array
126
    {
127 1
        return ($this->imageCandidates instanceof ImageCandidateSetInterface) ?
0 ignored issues
show
introduced by
$this->imageCandidates is always a sub-type of Jkphl\Respimgcss\Applica...geCandidateSetInterface.
Loading history...
128 1
            $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 1
    public function make(array $densities = [1]): CssRulesetInterface
139
    {
140 1
        $cssRuleset = new \Jkphl\Respimgcss\Ports\CssRuleset();
141
142
        // If all necessary properties are given
143 1
        if (count($this->breakPoints) && count($this->imageCandidates) && count($densities)) {
144
            // Instantiate a CSS ruleset compiler service and compile for all densities
145 1
            $cssRulesetCompilerService = new CssRulesetCompilerService(
146 1
                $cssRuleset,
147 1
                $this->breakPoints,
148 1
                $this->imageCandidates
149
            );
150 1
            $cssRuleset                = $cssRulesetCompilerService->compile($densities);
151
        }
152
153 1
        return $cssRuleset;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $cssRuleset could return the type Jkphl\Respimgcss\Domain\...act\CssRulesetInterface which includes types incompatible with the type-hinted return Jkphl\Respimgcss\Ports\CssRulesetInterface. Consider adding an additional type-check to rule them out.
Loading history...
154
    }
155
}