ImageCandidateSet   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 135
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validateImageCandidateValue() 0 7 2
A validateImageCandidateType() 0 7 2
A sortImageCandidates() 0 6 3
A offsetSet() 0 11 1
A toArray() 0 3 1
A validateImageCandidate() 0 6 2
A getType() 0 3 1
1
<?php
2
3
/**
4
 * responsive-images-css
5
 *
6
 * @category   Jkphl
7
 * @package    Jkphl\Respimgcss
8
 * @subpackage Jkphl\Respimgcss\Application
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\Application\Model;
38
39
use Jkphl\Respimgcss\Application\Contract\ImageCandidateSetInterface;
40
use Jkphl\Respimgcss\Application\Exceptions\InvalidArgumentException;
41
use Jkphl\Respimgcss\Domain\Contract\ImageCandidateInterface;
42
43
/**
44
 * Typed and validating image candidate set
45
 *
46
 * @package    Jkphl\Respimgcss
47
 * @subpackage Jkphl\Respimgcss\Application
48
 */
49
class ImageCandidateSet extends \Jkphl\Respimgcss\Domain\Model\ImageCandidateSet implements ImageCandidateSetInterface
50
{
51
    /**
52
     * Image candidate set type
53
     *
54
     * @var string
55
     */
56
    protected $type = null;
57
    /**
58
     * Image candidate values
59
     *
60
     * @var array
61
     */
62
    protected $values = [];
63
64
    /**
65
     * Image candidate set constructor
66
     *
67
     * @param ImageCandidateInterface $imageCandidate Image candidate
68
     */
69 19
    public function __construct(ImageCandidateInterface $imageCandidate)
70
    {
71 19
        $this->type                                = $imageCandidate->getType();
72 19
        $this->imageCandidates[]                   = $imageCandidate;
73 19
        $this->values[$imageCandidate->getValue()] = true;
74 19
    }
75
76
    /**
77
     * Offset to set
78
     *
79
     * @param int $offset  Offset
80
     * @param mixed $value Image candidate
81
     *
82
     * @return void
83
     */
84 13
    public function offsetSet($offset, $value): void
85
    {
86 13
        $this->validateImageCandidate($value);
87 12
        $this->validateImageCandidateType($value);
88 12
        $this->validateImageCandidateValue($value);
89
90 11
        parent::offsetSet($offset, $value);
91 11
        $this->values[$value->getValue()] = true;
92
93
        // Sort the image candidates by value
94 11
        usort($this->imageCandidates, [$this, 'sortImageCandidates']);
95 11
    }
96
97
    /**
98
     * Validate a value that should be added as image candidate
99
     *
100
     * @param mixed $value Image candidate
101
     *
102
     * @throws InvalidArgumentException If the value given is not a valid image candidate
103
     */
104 13
    protected function validateImageCandidate($value)
105
    {
106 13
        if (!($value instanceof ImageCandidateInterface)) {
107 1
            throw new InvalidArgumentException(
108 1
                InvalidArgumentException::INVALID_IMAGE_CANDIDATE_STR,
109 1
                InvalidArgumentException::INVALID_IMAGE_CANDIDATE
110
            );
111
        }
112 12
    }
113
114
    /**
115
     * Validate a value that should be added as image candidate
116
     *
117
     * @param mixed $value Image candidate
118
     *
119
     * @throws InvalidArgumentException If the image candidate types are inconsistent
120
     */
121 12
    protected function validateImageCandidateType(ImageCandidateInterface $value)
122
    {
123
        // Test if the image candidate type matches the current set type
124 12
        if ($value->getType() !== $this->type) {
125 1
            throw new InvalidArgumentException(
126 1
                InvalidArgumentException::INCONSISTENT_IMAGE_CANDIDATE_TYPES_STR,
127 1
                InvalidArgumentException::INCONSISTENT_IMAGE_CANDIDATE_TYPES
128
            );
129
        }
130 12
    }
131
132
    /**
133
     * Validate a value that should be added as image candidate
134
     *
135
     * @param mixed $value Image candidate
136
     *
137
     * @throws InvalidArgumentException If the image candidate value isn't unique
138
     */
139 12
    protected function validateImageCandidateValue(ImageCandidateInterface $value)
140
    {
141
        // If the image candidate value isn't unique
142 12
        if (array_key_exists($value->getValue(), $this->values)) {
143 1
            throw new InvalidArgumentException(
144 1
                sprintf(InvalidArgumentException::OVERLAPPING_IMAGE_CANDIDATE_VALUE_STR, $value),
145 1
                InvalidArgumentException::OVERLAPPING_IMAGE_CANDIDATE_VALUE
146
            );
147
        }
148 11
    }
149
150
    /**
151
     * Return all image candidates as an array
152
     *
153
     * @return array Image candidates
154
     */
155 9
    public function toArray(): array
156
    {
157 9
        return $this->imageCandidates;
158
    }
159
160
    /**
161
     * Return the image candidate set type
162
     *
163
     * @return string|null Image candidate set type
164
     */
165 14
    public function getType(): string
166
    {
167 14
        return $this->type;
168
    }
169
170
    /**
171
     * Compare and sort two image candidates by value
172
     *
173
     * @param ImageCandidateInterface $image1 Image candidate 1
174
     * @param ImageCandidateInterface $image2 Image candidate 2
175
     *
176
     * @return int
177
     */
178 11
    protected function sortImageCandidates(ImageCandidateInterface $image1, ImageCandidateInterface $image2): int
179
    {
180 11
        $imageValue1 = $image1->getValue();
181 11
        $imageValue2 = $image2->getValue();
182
183 11
        return ($imageValue1 === $imageValue2) ? 0 : (($imageValue1 > $imageValue2) ? 1 : -1);
184
    }
185
}
186