Completed
Push — master ( 4f1df0...4182a1 )
by Peter
07:50
created

Image::get()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 6.0359

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 55
ccs 27
cts 30
cp 0.9
rs 8.7752
c 1
b 0
f 0
cc 6
eloc 31
nc 5
nop 1
crap 6.0359

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Model;
15
16
use Maslosoft\Mangan\Events\Event;
17
use Maslosoft\Mangan\Events\ImageEvent;
18
use Maslosoft\Mangan\Exceptions\FileNotFoundException;
19
use Maslosoft\Mangan\Helpers\ImageThumb;
20
use MongoGridFSFile;
21
22
/**
23
 * Class for storing embedded images
24
 * @since 2.0.1
25
 * @author Piotr
26
 */
27
class Image extends File
28
{
29
	const EventBeforeResize = 'beforeResize';
30
31
	const EventAfterResize = 'afterResize';
32
33
	/**
34
	 * Image width
35
	 * @var int
36
	 */
37
	public $width = 0;
38
39
	/**
40
	 * Image height
41
	 * @var int
42
	 */
43
	public $height = 0;
44
45
	/**
46
	 * Get resized image
47
	 * @param ImageParams $params
48
	 * @return MongoGridFSFile
49
	 */
50 2
	public function get(ImageParams $params = null)
51
	{
52
		// Get original image or file if it is not image
53 2
		if (!$params || !$this->isImage($this->filename))
54
		{
55 2
			return $this->_get();
56
		}
57
58
		// Get resized image
59 2
		$params->isTemp = true;
60 2
		$result = $this->_get($params->toArray());
61
62
		// Resize and store if not found
63 2
		if (!$result)
64
		{
65 2
			$result = $this->_get();
66 2
			if (!$result)
67
			{
68
				throw new FileNotFoundException('File not found');
69
			}
70
71 2
			$originalFilename = $result->file['filename'];
72 2
			$fileName = tempnam('/tmp/', str_replace('\\', '.', __CLASS__));
73 2
			$result->write($fileName);
74
75 2
			$ie = new ImageEvent;
76 2
			$ie->sender = $this;
77 2
			$ie->source = $this;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this of type this<Maslosoft\Mangan\Model\Image> is incompatible with the declared type string of property $source.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78 2
			$ie->path = $fileName;
79
80 2
			Event::trigger($this, self::EventBeforeResize, $ie);
81
82 2
			$image = new ImageThumb($fileName);
83 2
			if ($params->adaptive)
84
			{
85
				$image->adaptiveResize($params->width, $params->height)->save($fileName);
86
			}
87
			else
88
			{
89 2
				$image->resize($params->width, $params->height)->save($fileName);
90
			}
91
92 2
			$ie = new ImageEvent;
93 2
			$ie->sender = $this;
94 2
			$ie->source = $this;
95 2
			$ie->path = $fileName;
96
97 2
			Event::trigger($this, self::EventAfterResize, $ie);
98
99 2
			$this->_set($fileName, $originalFilename, $params->toArray());
100 2
			unlink($fileName);
101 2
			return $this->_get($params->toArray());
102
		}
103
		return $result;
104
	}
105
106
	/**
107
	 * Return true if it is really image
108
	 * @return bool
109
	 */
110 2
	public function isImage($name)
111
	{
112 2
		return in_array(strtolower(pathinfo($name, PATHINFO_EXTENSION)), ['jpg', 'jpeg', 'gif', 'png']);
113
	}
114
115 2
	protected function _set($tempName, $fileName, $params = [])
116
	{
117 2
		if ($this->isImage($fileName))
118
		{
119 2
			$thumb = new ImageThumb($tempName);
120 2
			$dimensions = (object) $thumb->getCurrentDimensions();
121 2
			$this->width = $dimensions->width;
122 2
			$this->height = $dimensions->height;
123
		}
124 2
		parent::_set($tempName, $fileName, $params);
125 2
	}
126
127
	/**
128
	 * Send file to browser
129
	 */
130
	public function send(ImageParams $params = null)
131
	{
132
		$this->_send($this->get($params));
133
	}
134
135
	/**
136
	 * Stream file to browser
137
	 */
138
	public function stream(ImageParams $params = null)
139
	{
140
		$this->_stream($this->get($params));
141
	}
142
143
}
144