Passed
Push — master ( 31f905...fdb5c0 )
by Joshua
02:40
created

ImageBuilder::boxed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
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
    protected const ASSET_BASE_URL_CDN = "https://assets.seams-cms.com";
27
    protected const ASSET_BASE_URL = "https://assets-nocdn.seams-cms.com";
28
29
    public const CROP_TOP_LEFT = "topleft";
30
    public const CROP_TOP = "top";
31
    public const CROP_TOP_RIGHT = "topright";
32
    public const CROP_LEFT = "left";
33
    public const CROP_CENTER = "center";
34
    public const CROP_RIGHT = "right";
35
    public const CROP_BOTTOM_LEFT = "bottomleft";
36
    public const CROP_BOTTOM = "bottom";
37
    public const CROP_BOTTOM_RIGHT = "bottomright";
38
39
    public const FLIP_HORIZONTAL = "horizontal";
40
    public const FLIP_VERTICAL = "vertical";
41
    public 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
55
    /**
56
     * @param Asset $asset
57
     * @return ImageBuilder
58
     */
59 4
    public static function fromAsset(Asset $asset)
60
    {
61 4
        return new self($asset->getWorkspace(), $asset->getPath());
62
    }
63
64
    /**
65
     * @param string $workspace
66
     * @param string $path
67
     * @return ImageBuilder
68
     */
69 1
    public static function fromPath(string $workspace, string $path)
70
    {
71 1
        return new self($workspace, $path);
72
    }
73
74
    /**
75
     * ImageBuilder constructor.
76
     *
77
     * @param string $workspace
78
     * @param string $path
79
     */
80 4
    protected function __construct(string $workspace, string $path)
81
    {
82 4
        $this->workspace = $workspace;
83 4
        $this->path = $path;
84
85 4
        $this->cdn = true;
86 4
    }
87
88
    /**
89
     * Skips the CDN host and uses the direct asset API.
90
     *
91
     * Note that this should not be used for production purposes.
92
     *
93
     * @return ImageBuilder
94
     */
95 3
    public function skipCdn(): ImageBuilder
96
    {
97 3
        $this->cdn = false;
98
99 3
        return $this;
100
    }
101
102
    /**
103
     * Uses the CDN host.
104
     *
105
     * @return self
106
     */
107 2
    public function useCdn(): self
108
    {
109 2
        $this->cdn = true;
110
111 2
        return $this;
112
    }
113
114
    /**
115
     * @return ImageBuilder
116
     */
117 2
    public function blur(): self
118
    {
119 2
        $this->filters[] = 'blur';
120
121 2
        return $this;
122
    }
123
124
    /**
125
     * @param int $width
126
     * @param int $height
127
     * @return ImageBuilder
128
     */
129
    public function boxed(int $width = 100, int $height = 100): self
130
    {
131
        $this->filters[] = sprintf('boxed(%d,%d)', $width, $height);
132
133
        return $this;
134
    }
135
136
    /**
137
     * @param int $red
138
     * @param int $green
139
     * @param int $blue
140
     * @param int $alpha
141
     * @return ImageBuilder
142
     */
143 2
    public function colorize(int $red, int $green, int $blue, int $alpha): self
144
    {
145 2
        $this->filters[] = sprintf('colorize(%d,%d,%d,%d)', $red, $green, $blue, $alpha);
146
147 2
        return $this;
148
    }
149
150
    /**
151
     * @param string $position One of the CROP_* constants
152
     * @param int $width
153
     * @param int $height
154
     * @return ImageBuilder
155
     */
156 1
    public function crop(string $position = self::CROP_CENTER, int $width = 100, int $height = 100): self
157
    {
158 1
        $this->filters[] = sprintf('crop(%s,%d,%d)', $position, $width, $height);
159
160 1
        return $this;
161
    }
162
163
    /**
164
     * @return ImageBuilder
165
     */
166 1
    public function cropsides(): self
167
    {
168 1
        $this->filters[] = 'cropsides';
169
170 1
        return $this;
171
    }
172
173
    /**
174
     * @param string $direction One of the FLIP_* constants
175
     * @return ImageBuilder
176
     */
177 1
    public function flip(string $direction = self::FLIP_HORIZONTAL): self
178
    {
179 1
        $this->filters[] = sprintf('flip(%s)', $direction);
180
181 1
        return $this;
182
    }
183
184
    /**
185
     * @return ImageBuilder
186
     */
187 2
    public function gray(): self
188
    {
189 2
        $this->filters[] = 'gray';
190
191 2
        return $this;
192
    }
193
194
    /**
195
     * @param int $height
196
     * @return ImageBuilder
197
     */
198 2
    public function height(int $height): self
199
    {
200 2
        $this->filters[] = sprintf('height(%d)', $height);
201
202 2
        return $this;
203
    }
204
205
    /**
206
     * @return ImageBuilder
207
     */
208 2
    public function negate(): self
209
    {
210 2
        $this->filters[] = 'negate';
211
212 2
        return $this;
213
    }
214
215
    /**
216
     * @param int $angle
217
     * @return ImageBuilder
218
     */
219 2
    public function rotate(int $angle): self
220
    {
221 2
        $this->filters[] = sprintf('rotate(%d)', $angle);
222
223 2
        return $this;
224
    }
225
226
    /**
227
     * @param int $width
228
     * @return ImageBuilder
229
     */
230 1
    public function width(int $width): self
231
    {
232 1
        $this->filters[] = sprintf('width(%d)', $width);
233
234 1
        return $this;
235
    }
236
237
    /**
238
     * Returns the actual URL to the image.
239
     *
240
     * @return string
241
     */
242 4
    public function getSourceUrl(): string
243
    {
244 4
        $url = sprintf("%s/%s", $this->workspace, $this->path);
245
246
        // Add filters if any are found
247 4
        if (count($this->filters) > 0) {
248 2
            $sortedFilters = $this->filters;
249 2
            sort($sortedFilters);
250
251 2
            $url = sprintf('p/%s/%s', join('/', $sortedFilters), $url);
252
        }
253
254 4
        return sprintf("%s/%s", $this->cdn ? self::ASSET_BASE_URL_CDN : self::ASSET_BASE_URL, $url);
255
    }
256
}
257