Passed
Push — master ( 3242af...43b283 )
by Richard
17:32 queued 10s
created

ImageTransformation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace Riclep\Storyblok\Support;
5
6
7
use Riclep\Storyblok\Fields\Image;
8
9
class ImageTransformation
10
{
11
	private $filename;
12
	private $focus;
13
	protected $transformations = [];
14
	private $image;
15
16
	public function __construct(Image $image)
17
	{
18
		$this->filename = $image->filename;
0 ignored issues
show
Bug Best Practice introduced by
The property filename does not exist on Riclep\Storyblok\Fields\Image. Since you implemented __get, consider adding a @property annotation.
Loading history...
19
		$this->focus = $image->focus;
0 ignored issues
show
Bug Best Practice introduced by
The property focus does not exist on Riclep\Storyblok\Fields\Image. Since you implemented __get, consider adding a @property annotation.
Loading history...
20
		$this->image = $image;
21
	}
22
23
	public function width() {
24
		return $this->transformations['width'] ?? $this->image->width();
25
	}
26
27
	public function height() {
28
		return $this->transformations['height'] ?? $this->image->height();
29
	}
30
31
	public function type() {
32
		if (array_key_exists('format', $this->transformations)) {
33
			return 'image/' . $this->transformations['format'];
34
		}
35
36
		return $this->image->type();
37
	}
38
39
	public function resize($width = 0, $height = 0, $focus = null)
40
	{
41
		$this->transformations = array_merge($this->transformations, [
42
			'width' => $width,
43
			'height' => $height,
44
		]);
45
46
		if ($focus) {
47
			$this->transformations = array_merge($this->transformations, [
48
				'focus' => $focus,
49
			]);
50
		}
51
52
		return $this;
53
	}
54
55
	public function format($format, $quality = null)
56
	{
57
		$this->transformations = array_merge($this->transformations, [
58
			'format' => $format,
59
		]);
60
61
		if ($quality) {
62
			$this->transformations = array_merge($this->transformations, [
63
				'quality' => $quality,
64
			]);
65
		}
66
67
		return $this;
68
	}
69
70
	public function fitIn($width = 0, $height = 0, $fill = 'transparent')
71
	{
72
		$this->transformations = array_merge($this->transformations, [
73
			'width' => $width,
74
			'height' => $height,
75
			'fill' => $fill,
76
			'fit-in' => true,
77
		]);
78
79
		// has to be an image that supports transparency
80
		if ($fill === 'transparent') {
81
			$this->format('png');
82
		}
83
84
		return $this;
85
	}
86
87
	/**
88
	 * Transforms the image using the Storyblok image service
89
	 * See: https://www.storyblok.com/docs/image-service
90
	 *
91
	 * @param $options
92
	 * @return string
93
	 */
94
	public function createUrl($options): string
95
	{
96
		$resource = str_replace(['https:', '//a.storyblok.com'], '', $this->filename);
97
		return '//img2.storyblok.com' . $options . $resource;
98
	}
99
100
	public function getTransformations() {
101
		return $this->transformations;
102
	}
103
104
	public function __toString()
105
	{
106
		$transforms = '';
107
108
		if (array_key_exists('fit-in', $this->transformations)) {
109
			$transforms .= '/fit-in';
110
		}
111
112
		if (array_key_exists('width', $this->transformations)) {
113
			$transforms .= '/' . $this->transformations['width'] . 'x' . $this->transformations['height'];
114
		}
115
116
		if (array_key_exists('focus', $this->transformations) && $this->transformations['focus'] === 'smart') {
117
			$transforms .= '/smart';
118
		}
119
120
		// filters
121
		if (array_key_exists('format', $this->transformations) || array_key_exists('quality', $this->transformations) || array_key_exists('fill', $this->transformations) || (array_key_exists('focus', $this->transformations) && $this->transformations['focus'] === 'focal-point')) {
122
			$transforms .= '/filters';
123
124
			if (array_key_exists('format', $this->transformations)) {
125
				$transforms .= ':format(' . $this->transformations['format'] . ')';
126
			}
127
128
			if (array_key_exists('quality', $this->transformations)) {
129
				$transforms .= ':quality(' . $this->transformations['quality'] . ')';
130
			}
131
132
			if (array_key_exists('fill', $this->transformations)) {
133
				$transforms .= ':fill(' . $this->transformations['fill'] . ')';
134
			}
135
136
			if (array_key_exists('focus', $this->transformations) && $this->transformations['focus'] === 'focal-point' && $this->focus) {
137
				$transforms .= ':focal(' . $this->focus . ')';
0 ignored issues
show
Bug introduced by
Are you sure $this->focus of type mixed|string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
				$transforms .= ':focal(' . /** @scrutinizer ignore-type */ $this->focus . ')';
Loading history...
138
			}
139
		}
140
141
		return $this->createUrl($transforms);
142
	}
143
}