Completed
Push — master ( c7b167...faaa80 )
by Wanderson
02:08
created

Image   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 117
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSubtitle() 0 3 1
A getOrder() 0 3 1
A setSubtitle() 0 3 1
A setOrder() 0 3 1
A upload() 0 7 3
A showThumb() 0 3 1
A saveThumb() 0 10 1
A includeLibrary() 0 10 3
A __toString() 0 7 2
A getFullName() 0 7 2
A removeOld() 0 4 1
A clearCache() 0 4 1
1
<?php
2
3
namespace Win\File;
4
5
use \PhpThumbFactory;
6
/**
7
 * Arquivos
8
 *
9
 */
10
class Image extends File {
11
12
	static $phpThumbLib = '/../../../../lib/thumb/ThumbLib.inc.php';
13
14
	const QUALITY = 70;
15
	const MAX_HEIGHT = 900;
16
	const MAX_WIDTH = 1200;
17
18
	private $subtitle;
19
	private $order;
20
	protected static $validExtensions = ['jpg', 'jpeg', 'gif', 'png'];
21
22
	/* @overwrite */
23
24
	public function __construct($name = '') {
25
		parent::__construct($name);
26
		$this->subtitle = '';
27
		$this->order = 0;
28
	}
29
30
	/* METODOS DE ACESSO */
31
32
	public function getSubtitle() {
33
		return $this->subtitle;
34
	}
35
36
	public function getOrder() {
37
		return $this->order;
38
	}
39
40
	public function setSubtitle($subtitle) {
41
		$this->subtitle = $subtitle;
42
	}
43
44
	public function setOrder($order) {
45
		$this->order = $order;
46
	}
47
48
	/* @overwrite */
49
50
	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...
51
		$error = parent::upload($newName);
52
		if ($this->uploadPrepared and is_null($error)) {
53
			$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...
54
		}
55
		return $error;
56
	}
57
58
	/**
59
	 * Gera a miniatura
60
	 * @return string Retorna o caminho da thumb
61
	 * @param int $width largura
62
	 * @param int $height altura
63
	 * @param boolean $mode modo de corte
64
	 */
65
	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...
66
		return "lib/thumb/" . $mode . '/' . $width . '/' . $height . '/' . $this->__toString();
67
	}
68
69
	/**
70
	 * Cria uma miniatura da imagem 
71
	 * @return string
72
	 */
73
	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...
74
		$this->includeLibrary();
75
76
		$thumb = PhpThumbFactory::create($this->getFullName());
77
		$thumb->resize(self::MAX_WIDTH, self::MAX_HEIGHT);
78
		$this->remove();
79
		$thumb->save($this->getFullName());
80
81
		return null;
82
	}
83
84
	/**
85
	 * Inclui bibliotecas necessárias
86
	 * @return string
87
	 */
88
	protected function includeLibrary() {
89
		if (!class_exists('PhpThumbFactory')) {
90
			$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...
91
			if (file_exists($phpThumbDir)) {
92
				include_once $phpThumbDir;
93
			} else {
94
				return 'PhpThumbFactory não incluído!';
95
			}
96
		}
97
	}
98
99
	public function __toString() {
100
		if ($this->getName() != '') {
101
			return parent::__toString();
102
		} else {
103
			return $this->getFullName();
104
		}
105
	}
106
107
	public function getFullName() {
108
		if ($this->getName() != '') {
109
			return parent::getFullName();
110
		} else {
111
			return $this->getDirectory() . 'default.png';
112
		}
113
	}
114
115
	public function removeOld() {
116
		$this->clearCache();
117
		parent::removeOld();
118
	}
119
120
	/** Limpa imagens em cache */
121
	public function clearCache() {
122
		$cacheDirectory = new Directory('data/cache/thumb/');
123
		$cacheDirectory->remove();
124
	}
125
126
}
127