Passed
Push — master ( 91a8e4...a6877f )
by Joschi
02:16
created

Generator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 99
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

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