Completed
Push — master ( a0b85b...774d12 )
by Wanderson
20:21
created

Image::showThumb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace Win\File;
4
5
use \PhpThumbFactory;
6
7
/**
8
 * Arquivos de Imagem
9
 *
10
 */
11
class Image extends File {
12
13
	static $phpThumbLib = '/../../../../lib/thumb/ThumbLib.inc.php';
14
15
	const QUALITY = 70;
16
	const MAX_HEIGHT = 900;
17
	const MAX_WIDTH = 1200;
18
19
	protected static $validExtensions = ['jpg', 'jpeg', 'gif', 'png'];
20
21
	/* @overwrite */
22
23
	function upload($newName = '') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
		$error = parent::upload($newName);
25
		if ($this->uploadPrepared and is_null($error)) {
26
			$error = $this->saveThumb(self::MAX_WIDTH, self::MAX_HEIGHT);
0 ignored issues
show
Unused Code introduced by
The call to Image::saveThumb() has too many arguments starting with self::MAX_WIDTH.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
27
		}
28
		return $error;
29
	}
30
31
	/**
32
	 * Gera a miniatura
33
	 * @return string Retorna o caminho da thumb
34
	 * @param int $width largura
35
	 * @param int $height altura
36
	 * @param boolean $mode modo de corte
37
	 */
38
	function showThumb($width = 100, $height = 100, $mode = 'normal') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
		return "lib/thumb/" . $mode . '/' . $width . '/' . $height . '/' . $this->__toString();
40
	}
41
42
	/**
43
	 * Cria uma miniatura da imagem 
44
	 * @return string
45
	 */
46
	function saveThumb() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
47
		$this->includeLibrary();
48
49
		$thumb = PhpThumbFactory::create($this->getFullName());
50
		$thumb->resize(self::MAX_WIDTH, self::MAX_HEIGHT);
51
		$this->remove();
52
		$thumb->save($this->getFullName());
53
54
		return null;
55
	}
56
57
	/**
58
	 * Inclui bibliotecas necessárias
59
	 * @return string
60
	 */
61
	protected function includeLibrary() {
62
		if (!class_exists('PhpThumbFactory')) {
63
			$phpThumbDir = dirname(realpath(__FILE__)) . static::$phpThumbLib;
0 ignored issues
show
Bug introduced by
Since $phpThumbLib is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $phpThumbLib to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
64
			if (file_exists($phpThumbDir)) {
65
				include_once $phpThumbDir;
66
			} else {
67
				return 'PhpThumbFactory não incluído!';
68
			}
69
		}
70
	}
71
72
	public function __toString() {
73
		if ($this->getName() != '') {
74
			return parent::__toString();
75
		} else {
76
			return $this->getFullName();
77
		}
78
	}
79
80
	public function getFullName() {
81
		if ($this->getName() != '') {
82
			return parent::getFullName();
83
		} else {
84
			return $this->getDirectory() . 'default.png';
85
		}
86
	}
87
88
	public function removeOld() {
89
		$this->clearCache();
90
		parent::removeOld();
91
	}
92
93
	/** Limpa imagens em cache */
94
	public function clearCache() {
95
		$cacheDirectory = new Directory('data/cache/thumb/');
96
		$cacheDirectory->remove();
97
	}
98
99
}
100