Passed
Push — master ( 1d9bc6...1d1109 )
by Joshua
05:00 queued 02:49
created

ImageBuilder::asWebp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Seams-CMS Delivery SDK package.
7
 *
8
 * (c) Seams-CMS.com
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace SeamsCMS\Delivery;
15
16
use SeamsCMS\Delivery\Model\Asset;
17
18
/**
19
 * Class ImageBuilder
20
 * @package SeamsCMS\Delivery
21
 *
22
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
23
 */
24
class ImageBuilder
25
{
26
    const ASSET_BASE_URL_CDN = "https://assets.seams-cms.com";
27
    const ASSET_BASE_URL = "https://assets-nocdn.seams-cms.com";
28
29
    const CROP_TOP_LEFT = "topleft";
30
    const CROP_TOP = "top";
31
    const CROP_TOP_RIGHT = "topright";
32
    const CROP_LEFT = "left";
33
    const CROP_CENTER = "center";
34
    const CROP_RIGHT = "right";
35
    const CROP_BOTTOM_LEFT = "bottomleft";
36
    const CROP_BOTTOM = "bottom";
37
    const CROP_BOTTOM_RIGHT = "bottomright";
38
39
    const FLIP_HORIZONTAL = "horizontal";
40
    const FLIP_VERTICAL = "vertical";
41
    const FLIP_BOTH = "both";
42
43
    /** @var array */
44
    protected $filters = array();
45
46
47
    /** @var string */
48
    protected $workspace;
49
    /** @var string */
50
    protected $path;
51
52
    /** @var bool */
53
    protected $cdn = false;
54
    /** @var bool */
55
    protected $webp = false;
56
57
    /**
58
     * @param Asset $asset
59
     * @return ImageBuilder
60
     */
61 4
    public static function fromAsset(Asset $asset)
62
    {
63 4
        return new self($asset->getWorkspace(), $asset->getPath());
64
    }
65
66
    /**
67
     * @param string $workspace
68
     * @param string $path
69
     * @return ImageBuilder
70
     */
71 2
    public static function fromPath(string $workspace, string $path)
72
    {
73 2
        return new self($workspace, $path);
74
    }
75
76
    /**
77
     * ImageBuilder constructor.
78
     *
79
     * @param string $workspace
80
     * @param string $path
81
     */
82 5
    protected function __construct(string $workspace, string $path)
83
    {
84 5
        $this->workspace = $workspace;
85 5
        $this->path = $path;
86
87 5
        $this->cdn = true;
88 5
        $this->webp = false;
89 5
    }
90
91
    /**
92
     * Skips the CDN host and uses the direct asset API.
93
     *
94
     * Note that this should not be used for production purposes.
95
     *
96
     * @return ImageBuilder
97
     */
98 3
    public function skipCdn(): ImageBuilder
99
    {
100 3
        $this->cdn = false;
101
102 3
        return $this;
103
    }
104
105
    /**
106
     * Uses the CDN host.
107
     *
108
     * @return self
109
     */
110 2
    public function useCdn(): self
111
    {
112 2
        $this->cdn = true;
113
114 2
        return $this;
115
    }
116
117
    /**
118
     * Return the image in WebP format
119
     *
120
     * @return self
121
     */
122 1
    public function asWebp(): self
123
    {
124 1
        $this->webp = true;
125
126 1
        return $this;
127
    }
128
129
    /**
130
     * @return ImageBuilder
131
     */
132 2
    public function blur(): self
133
    {
134 2
        $this->filters[] = 'blur';
135
136 2
        return $this;
137
    }
138
139
    /**
140
     * @param int $width
141
     * @param int $height
142
     * @return ImageBuilder
143
     */
144
    public function boxed(int $width = 100, int $height = 100): self
145
    {
146
        $this->filters[] = sprintf('boxed(%d,%d)', $width, $height);
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param int $red
153
     * @param int $green
154
     * @param int $blue
155
     * @param int $alpha
156
     * @return ImageBuilder
157
     */
158 2
    public function colorize(int $red, int $green, int $blue, int $alpha): self
159
    {
160 2
        $this->filters[] = sprintf('colorize(%d,%d,%d,%d)', $red, $green, $blue, $alpha);
161
162 2
        return $this;
163
    }
164
165
    /**
166
     * @param string $position One of the CROP_* constants
167
     * @param int $width
168
     * @param int $height
169
     * @return ImageBuilder
170
     */
171 1
    public function crop(string $position = self::CROP_CENTER, int $width = 100, int $height = 100): self
172
    {
173 1
        $this->filters[] = sprintf('crop(%s,%d,%d)', $position, $width, $height);
174
175 1
        return $this;
176
    }
177
178
    /**
179
     * @return ImageBuilder
180
     */
181 1
    public function cropsides(): self
182
    {
183 1
        $this->filters[] = 'cropsides';
184
185 1
        return $this;
186
    }
187
188
    /**
189
     * @param string $direction One of the FLIP_* constants
190
     * @return ImageBuilder
191
     */
192 1
    public function flip(string $direction = self::FLIP_HORIZONTAL): self
193
    {
194 1
        $this->filters[] = sprintf('flip(%s)', $direction);
195
196 1
        return $this;
197
    }
198
199
    /**
200
     * @return ImageBuilder
201
     */
202 2
    public function gray(): self
203
    {
204 2
        $this->filters[] = 'gray';
205
206 2
        return $this;
207
    }
208
209
    /**
210
     * @param int $height
211
     * @return ImageBuilder
212
     */
213 2
    public function height(int $height): self
214
    {
215 2
        $this->filters[] = sprintf('height(%d)', $height);
216
217 2
        return $this;
218
    }
219
220
    /**
221
     * @return ImageBuilder
222
     */
223 2
    public function negate(): self
224
    {
225 2
        $this->filters[] = 'negate';
226
227 2
        return $this;
228
    }
229
230
    /**
231
     * @param int $angle
232
     * @return ImageBuilder
233
     */
234 2
    public function rotate(int $angle): self
235
    {
236 2
        $this->filters[] = sprintf('rotate(%d)', $angle);
237
238 2
        return $this;
239
    }
240
241
    /**
242
     * @param int $width
243
     * @return ImageBuilder
244
     */
245 1
    public function width(int $width): self
246
    {
247 1
        $this->filters[] = sprintf('width(%d)', $width);
248
249 1
        return $this;
250
    }
251
252
    /**
253
     * Returns the actual URL to the image.
254
     *
255
     * @return string
256
     */
257 5
    public function getSourceUrl(): string
258
    {
259 5
        $url = sprintf("%s/%s%s", $this->workspace, $this->path, $this->webp ? ".webp" : "");
260
261
        // Add filters if any are found
262 5
        if (count($this->filters) > 0) {
263 2
            $sortedFilters = $this->filters;
264 2
            sort($sortedFilters);
265
266 2
            $url = sprintf('p/%s/%s', join('/', $sortedFilters), $url);
267
        }
268
269 5
        return sprintf("%s/%s", $this->cdn ? self::ASSET_BASE_URL_CDN : self::ASSET_BASE_URL, $url);
270
    }
271
}
272