Passed
Push — master ( d74f71...c443bf )
by Richard
09:47 queued 12s
created

Image::extractMetaDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Riclep\Storyblok\Fields;
5
6
7
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
		$this->extractMetaDetails();
25
26
		if (method_exists($this, 'transformations')) {
27
			$this->transformations();
28
		}
29
	}
30
31
	public function transform($name = null) {
32
		if ($name) {
33
			if (array_key_exists($name, $this->transformations) ) {
34
				return $this->transformations[$name];
35
			}
36
			return false;
37
		}
38
39
		return new ImageTransformation($this);
40
	}
41
42
	public function width() {
43
		return $this->meta('width');
44
	}
45
46
	public function height() {
47
		return $this->meta('height');
48
	}
49
50
	public function type() {
51
		$extension = $this->meta('extension');
52
53
		if ($extension === 'jpg') {
54
			$extension = 'jpeg';
55
		}
56
57
		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

57
		return 'image/' . /** @scrutinizer ignore-type */ $extension;
Loading history...
58
	}
59
60
	public function picture($alt = '', $default = null, $attributes = [], $view = 'laravel-storyblok::picture-element') {
61
		if ($default) {
62
			$imgSrc = (string) $this->transformations[$default]['src'];
63
		} else {
64
			$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...
65
		}
66
67
		return view($view, [
68
			'alt' => $alt,
69
			'attributes' => $attributes,
70
			'default' => $default,
71
			'imgSrc' => $imgSrc,
72
			'transformations' => $this->transformations,
73
		])->render();
74
	}
75
76
	protected function getOriginalFilenameAttribute() {
77
		return $this->content['filename'];
78
	}
79
80
	protected function extractMetaDetails() {
81
		$path = $this->content['filename'];
82
83
		preg_match_all('/(?<width>\d+)x(?<height>\d+).+\.(?<extension>[a-z]{3,4})/m', $path, $dimensions, PREG_SET_ORDER, 0);
84
85
		$this->addMeta([
86
			'height' => $dimensions[0]['height'],
87
			'width' => $dimensions[0]['width'],
88
			'extension' => $dimensions[0]['extension'],
89
		]);
90
	}
91
92
	private function upgradeOldFields($content) {
93
		$this->content = [
94
			'filename' => $content,
95
			'alt' => null,
96
			'copyright' => null,
97
			'fieltype' => 'asset',
98
			'focus' => null,
99
			'name' => '',
100
			'title' => null,
101
		];
102
	}
103
}