Passed
Push — master ( 5649e5...cd00c2 )
by Joshua
05:29 queued 03:33
created

ImageBuilder::crop()   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
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the -SeamsCMSDeliverySdk 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
    /** @var Asset */
47
    protected $asset;
48
49
    /** @var bool */
50
    protected $cdn = false;
51
52
    /**
53
     * @param Asset $asset
54
     * @return ImageBuilder
55
     */
56 4
    public static function fromAsset(Asset $asset)
57
    {
58 4
        return new self($asset);
59
    }
60
61
    /**
62
     * ImageBuilder constructor.
63
     *
64
     * @param Asset $asset
65
     */
66 4
    protected function __construct(Asset $asset)
67
    {
68 4
        $this->asset = $asset;
69
70 4
        $this->cdn = true;
71 4
    }
72
73
    /**
74
     * Skips the CDN host and uses the direct asset API.
75
     *
76
     * Note that this should not be used for production purposes.
77
     *
78
     * @return ImageBuilder
79
     */
80 3
    public function skipCdn(): ImageBuilder
81
    {
82 3
        $this->cdn = false;
83
84 3
        return $this;
85
    }
86
87
    /**
88
     * Uses the CDN host.
89
     *
90
     * @return self
91
     */
92 2
    public function useCdn(): self
93
    {
94 2
        $this->cdn = true;
95
96 2
        return $this;
97
    }
98
99
    /**
100
     * @return ImageBuilder
101
     */
102 2
    public function blur(): self
103
    {
104 2
        $this->filters[] = 'blur';
105
106 2
        return $this;
107
    }
108
109
    /**
110
     * @param int $red
111
     * @param int $green
112
     * @param int $blue
113
     * @param int $alpha
114
     * @return ImageBuilder
115
     */
116 2
    public function colorize(int $red, int $green, int $blue, int $alpha): self
117
    {
118 2
        $this->filters[] = sprintf('colorize(%d,%d,%d,%d)', $red, $green, $blue, $alpha);
119
120 2
        return $this;
121
    }
122
123
    /**
124
     * @param string $position One of the CROP_* constants
125
     * @param int $width
126
     * @param int $height
127
     * @return ImageBuilder
128
     */
129 1
    public function crop(string $position = self::CROP_CENTER, int $width = 100, int $height = 100): self
130
    {
131 1
        $this->filters[] = sprintf('crop(%s,%d,%d)', $position, $width, $height);
132
133 1
        return $this;
134
    }
135
136
    /**
137
     * @return ImageBuilder
138
     */
139 1
    public function cropsides(): self
140
    {
141 1
        $this->filters[] = 'cropsides';
142
143 1
        return $this;
144
    }
145
146
    /**
147
     * @param string $direction One of the FLIP_* constants
148
     * @return ImageBuilder
149
     */
150 1
    public function flip(string $direction = self::FLIP_HORIZONTAL): self
151
    {
152 1
        $this->filters[] = sprintf('flip(%s)', $direction);
153
154 1
        return $this;
155
    }
156
157
    /**
158
     * @return ImageBuilder
159
     */
160 2
    public function gray(): self
161
    {
162 2
        $this->filters[] = 'gray';
163
164 2
        return $this;
165
    }
166
167
    /**
168
     * @param int $height
169
     * @return ImageBuilder
170
     */
171 2
    public function height(int $height): self
172
    {
173 2
        $this->filters[] = sprintf('height(%d)', $height);
174
175 2
        return $this;
176
    }
177
178
    /**
179
     * @return ImageBuilder
180
     */
181 2
    public function negate(): self
182
    {
183 2
        $this->filters[] = 'negate';
184
185 2
        return $this;
186
    }
187
188
    /**
189
     * @param int $angle
190
     * @return ImageBuilder
191
     */
192 2
    public function rotate(int $angle): self
193
    {
194 2
        $this->filters[] = sprintf('rotate(%d)', $angle);
195
196 2
        return $this;
197
    }
198
199
    /**
200
     * @param int $width
201
     * @return ImageBuilder
202
     */
203 1
    public function width(int $width): self
204
    {
205 1
        $this->filters[] = sprintf('width(%d)', $width);
206
207 1
        return $this;
208
    }
209
210
    /**
211
     * Returns the actual URL to the image.
212
     *
213
     * @return string
214
     */
215 4
    public function getSourceUrl(): string
216
    {
217 4
        $url = sprintf("%s/%s", $this->asset->getWorkspace(), $this->asset->getPath());
218
219
        // Add filters if any are found
220 4
        if (count($this->filters) > 0) {
221 2
            $sortedFilters = $this->filters;
222 2
            sort($sortedFilters);
223
224 2
            $url = sprintf('p/%s/%s', join('/', $sortedFilters), $url);
225
        }
226
227 4
        return sprintf("%s/%s", $this->cdn ? self::ASSET_BASE_URL_CDN : self::ASSET_BASE_URL, $url);
228
    }
229
}
230