Passed
Push — master ( 39d166...21d5d6 )
by Richard
03:58 queued 13s
created

Image::focalPointAlignment()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 11
nop 2
dl 0
loc 31
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
4
namespace Riclep\Storyblok\Fields;
5
6
use Riclep\Storyblok\Support\ImageTransformation;
7
8
class Image extends Asset
9
{
10
	/**
11
	 * Stores the transformations to be applied
12
	 *
13
	 * @var array
14
	 */
15
	public $transformations = [];
16
17
	/**
18
	 * The transformer used for this image
19
	 *
20
	 * @var mixed
21
	 */
22
	protected $transformer;
23
24
	/**
25
	 * The transformer class used for transformations
26
	 *
27
	 * @var mixed
28
	 */
29
	protected $transformerClass;
30
31
	/**
32
	 * Constructs the image image field
33
	 *
34
	 * @param $content
35
	 * @param $block
36
	 */
37
	public function __construct($content, $block)
38
	{
39
		if (is_string($content)) {
40
			$this->upgradeStringFields($content);
41
			parent::__construct($this->content, $block);
42
		} else {
43
			parent::__construct($content, $block);
44
		}
45
46
		$this->transformerClass = config('storyblok.image_transformer');
47
48
		$transformerClass = $this->transformerClass;
49
		$this->transformer = new $transformerClass($this);
50
51
		if (method_exists($this->transformer, 'init')) {
52
			$this->transformer->init();
53
		}
54
55
		if (method_exists($this, 'transformations')) {
56
			$this->transformations();
57
		}
58
	}
59
60
	/**
61
	 * Get the width of the image or transformed image
62
	 *
63
	 * @param $original
64
	 * @return mixed
65
	 */
66
	public function width($original = false) {
67
		return $this->transformer->width($original);
68
	}
69
70
	/**
71
	 * Get the height of the image or transformed image
72
	 *
73
	 * @param $original
74
	 * @return mixed
75
	 */
76
	public function height($original = false) {
77
		return $this->transformer->height($original);
78
	}
79
80
	/**
81
	 * Get the mime of the image or transformed image
82
	 *
83
	 * @param $original
84
	 * @return mixed
85
	 */
86
	public function mime($original = false) {
87
		return $this->transformer->mime($original);
88
	}
89
90
	/**
91
	 * Create a new or get a transformation of the image
92
	 *
93
	 * @param $tranformation
94
	 * @return mixed
95
	 */
96
	public function transform($tranformation = null) {
97
		if ($tranformation) {
98
			if (array_key_exists($tranformation, $this->transformations) ) {
99
				return new ImageTransformation($this->transformations[$tranformation]);
100
			}
101
			return false;
102
		}
103
104
		$transformerClass = $this->transformerClass;
105
		$this->transformer = new $transformerClass($this);
106
107
		if (method_exists($this->transformer, 'init')) {
108
			$this->transformer->init();
109
		}
110
111
		return $this->transformer;
112
	}
113
114
	/**
115
	 * Set the driver to use for transformations
116
	 *
117
	 * @param $transformer
118
	 * @return mixed
119
	 */
120
	public function transformer($transformer) {
121
		$this->transformerClass = $transformer;
122
123
		return $this;
124
	}
125
126
127
	/**
128
	 * Returns a picture element tag for this image and
129
	 * ant transforms defined on the image class
130
	 *
131
	 * @param $alt
132
	 * @param $default
133
	 * @param $attributes
134
	 * @param $view
135
	 * @param $reverse
136
	 * @return string
137
	 */
138
	public function picture($alt = '', $default = null, $attributes = [], $view = 'laravel-storyblok::picture-element', $reverse = false) {
139
		if ($default) {
140
			$imgSrc = (string) $this->transformations[$default]['src'];
141
		} else {
142
			$imgSrc = $this->filename;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->filename can also be of type boolean. However, the property $filename is declared as type false|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
143
		}
144
145
		// srcset seems to work the opposite way to picture elements when working out sizes
146
		if ($reverse) {
147
			$transformations = array_reverse($this->transformations);
148
		} else {
149
			$transformations = $this->transformations;
150
		}
151
152
		return view($view, [
153
			'alt' => $alt,
154
			'attributes' => $attributes,
155
			'default' => $default,
156
			'imgSrc' => $imgSrc,
157
			'transformations' => $transformations,
158
		])->render();
159
	}
160
161
	/**
162
	 * Returns an image tag with srcset attribute
163
	 *
164
	 * @param $alt
165
	 * @param $default
166
	 * @param $attributes
167
	 * @param $view
168
	 * @return string
169
	 */
170
	public function srcset($alt = '', $default = null, $attributes = [], $view = 'laravel-storyblok::srcset') {
0 ignored issues
show
Unused Code introduced by
The parameter $view is not used and could be removed. ( Ignorable by Annotation )

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

170
	public function srcset($alt = '', $default = null, $attributes = [], /** @scrutinizer ignore-unused */ $view = 'laravel-storyblok::srcset') {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
171
		return $this->picture($alt, $default, $attributes, 'laravel-storyblok::srcset', true);
172
	}
173
174
	/**
175
	 * Allows setting of new transformations on this image. Optionally
176
	 * return a new image so the original is not mutated
177
	 *
178
	 * @param $transformations
179
	 * @param $mutate
180
	 * @return $this|Image
181
	 */
182
	public function setTransformations($transformations, $mutate = true) {
183
		if ($mutate) {
184
			$this->transformations = $transformations;
185
186
			return $this;
187
		}
188
189
		$class = get_class($this); // don’t mutate original object
190
		$image = new $class($this->content, $this->block);
191
		$image->transformations = $transformations;
192
193
		return $image;
194
	}
195
196
	/**
197
	 * Reads the focus property if available and returns a string that can be used for CSS
198
	 * object-position or background-position. The default should be any valid value for
199
	 * the CSS property being used. Rigid will use hard alignments to edges.
200
	 *
201
	 * @param $default
202
	 * @param $rigid
203
	 * @return string
204
	 */
205
	public function focalPointAlignment($default = 'center', $rigid = false) {
206
		if (!$this->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...
207
			return $default;
208
		}
209
210
		preg_match_all('/\d+/', $this->focus, $matches);
211
212
		$leftPercent = round(($matches[0][0] / $this->width()) * 100);
213
		$topPercent = round(($matches[0][1] / $this->height()) * 100);
214
215
		if ($rigid) {
216
			if ($leftPercent > 66) {
217
				$horizontalAlignment = 'right';
218
			} else if ($leftPercent > 33) {
219
				$horizontalAlignment = 'center';
220
			} else {
221
				$horizontalAlignment = 'left';
222
			}
223
224
			if ($topPercent > 66) {
225
				$verticalAlignment = 'bottom';
226
			} else if ($topPercent > 33) {
227
				$verticalAlignment = 'center';
228
			} else {
229
				$verticalAlignment = 'top';
230
			}
231
232
			return $horizontalAlignment . ' ' . $verticalAlignment;
233
		}
234
235
		return $leftPercent . '% ' . $topPercent . '%';
236
	}
237
238
	/**
239
	 * Converts string fields into full image fields
240
	 *
241
	 * @param $content
242
	 * @return void
243
	 */
244
	protected function upgradeStringFields($content) {
245
		$this->content = [
246
			'filename' => $content,
247
			'alt' => null,
248
			'copyright' => null,
249
			'fieldtype' => 'asset',
250
			'focus' => null,
251
			'name' => '',
252
			'title' => null,
253
		];
254
	}
255
}