Passed
Push — master ( 0c62f4...c698e9 )
by Richard
03:12 queued 11s
created

Image::srcset()   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 4
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Riclep\Storyblok\Fields;
5
6
7
use Illuminate\Support\Str;
8
use Riclep\Storyblok\Support\ImageTransformation;
9
10
class Image extends Asset
11
{
12
	protected $transformations = [];
13
14
	public function __construct($content, $block)
15
	{
16
17
		if (is_string($content)) {
18
			$this->upgradeOldFields($content);
19
			parent::__construct($this->content, $block);
20
		} else {
21
			parent::__construct($content, $block);
22
		}
23
24
		if ($this->hasFile()) {
25
			$this->extractMetaDetails();
26
27
			if (method_exists($this, 'transformations')) {
28
				$this->transformations();
29
			}
30
		}
31
	}
32
33
	public function transform($name = null) {
34
		if ($name) {
35
			if (array_key_exists($name, $this->transformations) ) {
36
				return $this->transformations[$name];
37
			}
38
			return false;
39
		}
40
41
		return new ImageTransformation($this);
42
	}
43
44
	public function width() {
45
		return $this->meta('width');
46
	}
47
48
	public function height() {
49
		return $this->meta('height');
50
	}
51
52
	public function type() {
53
		$extension = $this->meta('extension');
54
55
		if ($extension === 'jpg') {
56
			$extension = 'jpeg';
57
		}
58
59
		return 'image/' . $extension;
0 ignored issues
show
Bug introduced by
Are you sure $extension of type array|string 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

59
		return 'image/' . /** @scrutinizer ignore-type */ $extension;
Loading history...
60
	}
61
62
	public function setTransformations($transformations) {
63
		$this->transformations = $transformations;
64
65
		return $this;
66
	}
67
68
	public function picture($alt = '', $default = null, $attributes = [], $view = 'laravel-storyblok::picture-element', $reverse = false) {
69
		if ($default) {
70
			$imgSrc = (string) $this->transformations[$default]['src'];
71
		} else {
72
			$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...
73
		}
74
75
		// srcset seems to work the opposite way to picture elements when working out sizes
76
		if ($reverse) {
77
			$transformations = array_reverse($this->transformations);
78
		} else {
79
			$transformations = $this->transformations;
80
		}
81
82
		return view($view, [
83
			'alt' => $alt,
84
			'attributes' => $attributes,
85
			'default' => $default,
86
			'imgSrc' => $imgSrc,
87
			'transformations' => $transformations,
88
		])->render();
89
	}
90
91
	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

91
	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...
92
		return $this->picture($alt, $default, $attributes, 'laravel-storyblok::srcset', true);
93
	}
94
95
	public function cssVars() {
96
		if ($this->transformations) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->transformations of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
97
			$vars = '';
98
99
			foreach ($this->transformations as $key => $transformation) {
100
				if (Str::endsWith($this->filename, 'svg')) {
101
					$vars .= '--' . $key . ': url("' . $this->filename . '"); ';
102
				} else {
103
					$vars .= '--' . $key . ': url("https:' . (string) $transformation['src'] . '"); ';
104
				}
105
			}
106
107
			return $vars;
108
		}
109
110
		return false;
111
	}
112
113
	protected function getOriginalFilenameAttribute() {
114
		return $this->content['filename'];
115
	}
116
117
	protected function extractMetaDetails() {
118
		$path = $this->content['filename'];
119
120
		preg_match_all('/(?<width>\d+)x(?<height>\d+).+\.(?<extension>[a-z]{3,4})/mi', $path, $dimensions, PREG_SET_ORDER, 0);
121
122
		if (Str::endsWith(strtolower($this->content['filename']), '.svg')) {
123
			$this->addMeta([
124
				'height' => false,
125
				'width' => false,
126
				'extension' => 'svg',
127
			]);
128
		} else {
129
			$this->addMeta([
130
				'height' => $dimensions[0]['height'],
131
				'width' => $dimensions[0]['width'],
132
				'extension' => strtolower($dimensions[0]['extension']),
133
			]);
134
		}
135
	}
136
137
	private function upgradeOldFields($content) {
138
		$this->content = [
139
			'filename' => $content,
140
			'alt' => null,
141
			'copyright' => null,
142
			'fieltype' => 'asset',
143
			'focus' => null,
144
			'name' => '',
145
			'title' => null,
146
		];
147
	}
148
}