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

BaseTransformer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 2 1
A setMime() 0 2 2
A width() 0 6 2
A height() 0 6 2
A __construct() 0 5 2
A mime() 0 6 2
1
<?php
2
3
namespace Riclep\Storyblok\Support\ImageTransformers;
4
5
use Riclep\Storyblok\Fields\Image;
6
7
abstract class BaseTransformer
8
{
9
	/**
10
	 * Stores basic details about the image
11
	 *
12
	 * @var
13
	 */
14
	protected $meta = [
15
		'height' => null,
16
		'width' => null,
17
		'extension' => null,
18
		'mime' => null,
19
	];
20
21
	/**
22
	 * Stores all the transformations
23
	 *
24
	 * @var array
25
	 */
26
	protected $transformations = [];
27
28
	protected Image $image;
29
30
	/**
31
	 * @return string The transformed image URL
32
	 */
33
	abstract public function buildUrl();
34
35
36
	/**
37
	 * Extracts meta details for the current image such as width
38
	 * and height, mime and anything else of use
39
	 *
40
	 * @return null
41
	 */
42
	abstract protected function extractMetaDetails();
43
44
45
	/**
46
	 * @param Image $image
47
	 */
48
	public function __construct(Image $image) {
49
		$this->image = $image;
50
51
		if (method_exists('preprocess', $this)) {
52
			$this->preprocess();
0 ignored issues
show
Bug introduced by
The method preprocess() does not exist on Riclep\Storyblok\Support...formers\BaseTransformer. ( Ignorable by Annotation )

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

52
			$this->/** @scrutinizer ignore-call */ 
53
          preprocess();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
		}
54
	}
55
56
	/**
57
	 * Returns the width of the transformed image. Optionally you
58
	 * can request the original width
59
	 *
60
	 * @param $original
61
	 * @return int
62
	 */
63
	public function width($original = false) {
64
		if ($original) {
65
			return $this->meta['width'];
66
		}
67
68
		return $this->transformations['width'] ?? $this->meta['width'];
69
	}
70
71
	/**
72
	 * Returns the height of the transformed image. Optionally you
73
	 * can request the original height
74
	 *
75
	 * @param $original
76
	 * @return mixed
77
	 */
78
	public function height($original = false) {
79
		if ($original) {
80
			return $this->meta['height'];
81
		}
82
83
		return $this->transformations['height'] ?? $this->meta['height'];
84
	}
85
86
	/**
87
	 * Returns the mime of the transformed image. Optionally you
88
	 * can request the original mime
89
	 *
90
	 * @param $original
91
	 * @return mixed
92
	 */
93
	public function mime($original = false) {
94
		if ($original) {
95
			return $this->meta['mime'];
96
		}
97
98
		return $this->transformations['mime'] ?? $this->meta['mime'];
99
	}
100
101
	/**
102
	 * Returns the mime from a particular file extension
103
	 *
104
	 * @param $extension
105
	 * @return string
106
	 */
107
	protected function setMime($extension) {
108
		return $extension === 'jpg' ? 'image/jpeg' : 'image/' . $extension;
109
	}
110
111
	/**
112
	 * Casts the image transformation as a sting using the
113
	 * buildUrl method
114
	 *
115
	 * @return string
116
	 */
117
	public function __toString() {
118
		return $this->buildUrl();
119
	}
120
}