Passed
Push — master ( a4e2d9...0bbf1f )
by Richard
03:41 queued 14s
created

Image::mime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Riclep\Storyblok\Fields;
5
6
class Image extends Asset
7
{
8
	/**
9
	 * Stores the transformations to be applied
10
	 *
11
	 * @var array
12
	 */
13
	public $transformations = [];
14
15
	/**
16
	 * The transformer used for this image
17
	 *
18
	 * @var mixed
19
	 */
20
	protected $transformer;
21
22
	/**
23
	 * The transformer class used for transformations
24
	 *
25
	 * @var mixed
26
	 */
27
	protected $transformerClass;
28
29
	/**
30
	 * Constructs the image image field
31
	 *
32
	 * @param $content
33
	 * @param $block
34
	 */
35
	public function __construct($content, $block)
36
	{
37
		if (is_string($content)) {
38
			$this->upgradeStringFields($content);
39
			parent::__construct($this->content, $block);
40
		} else {
41
			parent::__construct($content, $block);
42
		}
43
44
		$this->transformerClass = config('storyblok.image_transformer');
45
46
		$transformerClass = $this->transformerClass;
47
		$this->transformer = new $transformerClass($this);
48
49
		if (method_exists($this->transformer, 'init')) {
50
			$this->transformer->init();
51
		}
52
53
		if (method_exists($this, 'transformations')) {
54
			$this->transformations();
55
		}
56
	}
57
58
	/**
59
	 * Get the width of the image or transformed image
60
	 *
61
	 * @param $original
62
	 * @return mixed
63
	 */
64
	public function width($original = false) {
65
		return $this->transformer->width($original);
66
	}
67
68
	/**
69
	 * Get the height of the image or transformed image
70
	 *
71
	 * @param $original
72
	 * @return mixed
73
	 */
74
	public function height($original = false) {
75
		return $this->transformer->height($original);
76
	}
77
78
	/**
79
	 * Get the mime of the image or transformed image
80
	 *
81
	 * @param $original
82
	 * @return mixed
83
	 */
84
	public function mime($original = false) {
85
		return $this->transformer->mime($original);
86
	}
87
88
	/**
89
	 * Create a new or get a transformation of the image
90
	 *
91
	 * @param $tranformation
92
	 * @return mixed
93
	 */
94
	public function transform($tranformation = null) {
95
		if ($tranformation) {
96
			if (array_key_exists($tranformation, $this->transformations) ) {
97
				return $this->transformations[$tranformation];
98
			}
99
			return false;
100
		}
101
102
		$transformerClass = $this->transformerClass;
103
		$this->transformer = new $transformerClass($this);
104
105
		if (method_exists($this->transformer, 'init')) {
106
			$this->transformer->init();
107
		}
108
109
		return $this->transformer;
110
	}
111
112
	/**
113
	 * Set the driver to use for transformations
114
	 *
115
	 * @param $transformer
116
	 * @return mixed
117
	 */
118
	public function transformer($transformer) {
119
		$this->transformerClass = $transformer;
120
121
		return $this;
122
	}
123
124
125
	/**
126
	 * Returns a picture element tag for this image and
127
	 * ant transforms defined on the image class
128
	 *
129
	 * @param $alt
130
	 * @param $default
131
	 * @param $attributes
132
	 * @param $view
133
	 * @param $reverse
134
	 * @return string
135
	 */
136
	public function picture($alt = '', $default = null, $attributes = [], $view = 'laravel-storyblok::picture-element', $reverse = false) {
137
		if ($default) {
138
			$imgSrc = (string) $this->transformations[$default]['src'];
139
		} else {
140
			$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...
141
		}
142
143
		// srcset seems to work the opposite way to picture elements when working out sizes
144
		if ($reverse) {
145
			$transformations = array_reverse($this->transformations);
146
		} else {
147
			$transformations = $this->transformations;
148
		}
149
150
		return view($view, [
151
			'alt' => $alt,
152
			'attributes' => $attributes,
153
			'default' => $default,
154
			'imgSrc' => $imgSrc,
155
			'transformations' => $transformations,
156
		])->render();
157
	}
158
159
	/**
160
	 * Returns an image tag with srcset attribute
161
	 *
162
	 * @param $alt
163
	 * @param $default
164
	 * @param $attributes
165
	 * @param $view
166
	 * @return string
167
	 */
168
	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

168
	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...
169
		return $this->picture($alt, $default, $attributes, 'laravel-storyblok::srcset', true);
170
	}
171
172
	/**
173
	 * Allows setting of new transformations on this image. Optionally
174
	 * return a new image so the original is not mutated
175
	 *
176
	 * @param $transformations
177
	 * @param $mutate
178
	 * @return $this|Image
179
	 */
180
	public function setTransformations($transformations, $mutate = true) {
181
		if ($mutate) {
182
			$this->transformations = $transformations;
183
184
			return $this;
185
		}
186
187
		$class = get_class($this); // don’t mutate original object
188
		$image = new $class($this->content, $this->block);
189
		$image->transformations = $transformations;
190
191
		return $image;
192
	}
193
194
	/**
195
	 * Converts string fields into full image fields
196
	 *
197
	 * @param $content
198
	 * @return void
199
	 */
200
	protected function upgradeStringFields($content) {
201
		$this->content = [
202
			'filename' => $content,
203
			'alt' => null,
204
			'copyright' => null,
205
			'fieldtype' => 'asset',
206
			'focus' => null,
207
			'name' => '',
208
			'title' => null,
209
		];
210
	}
211
}