Completed
Push — master ( 8ac30c...a5f7c5 )
by Yann
02:01
created

Icon::setIcon()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 8
nop 4
1
<?php
2
3
namespace OpenMindParser\Models;
4
5
use OpenMindParser\Exceptions\UnexistentFileException;
6
7
/*Object that represent an Icon.*/
8
class Icon 
9
{
10
	/**
11
	 * @var String $name : The name of the icon without the path or the extension.
12
	 */
13
	private $name;
14
	/**
15
	 * @var String $filePath : The file path of the icon understandable by HTPP protocol. If it is a builtin icon, then it is in the 'img' directory of the project.
16
	 */
17
	private $filePath;
18
	/**
19
	 * @var int $size : The size of the file.
20
	 */
21
	private $size;
22
	/**
23
	 * @var String $extension : The extension of the icon file without the leading dot. If it is a builtin icon, then it is 'png'.
24
	 */
25
	private $extension;
26
	
27
	/**
28
	 * The constructor. Use setIcon method.
29
	 * 
30
	 * @param String $name : The name of the icon without the path or the extension.
31
	 * @param String $filePath : The file path of the icon. If it is a builtin icon, then it is in the 'img' directory of the project.
32
	 * @param int $size : The size of the file.
33
	 * @param String $extension : The extension of the icon file without the leading dot. If it is a builtin icon, then it is 'png'.
34
	 */ 
35
	public function __construct($name = null, $extension = null, $filePath = null, $size = null) {
36
		if(!empty($name)) {
37
			$this->setIcon($name, $extension, $filePath, $size);
38
		}
39
	}
40
	
41
	/**
42
	 * Fill all the instance attributes according to the given parameters.
43
	 * If only the name is given , then it will assume that a builtin icon is beeing used so the aother attributes will be filled following this logic.
44
	 * 
45
	 * @param String $name : The name of the icon without the path or the extension.
46
	 * @param String $filePath : The file path of the icon. If it is a builtin icon, then it is in the 'img' directory of the project.
47
	 * @param int $size : The size of the file.
48
	 * @param String $extension : The extension of the icon file without the leading dot. If it is a builtin icon, then it is 'png'.
49
	 */ 
50
	public function setIcon($name, $extension = null, $filePath = null, $size = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $size is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
		$this->name = $name;
52
		$this->extension = $extension ?: 'png';
53
		$this->filePath = $filePath ?: realpath(__DIR__.'/../../img/'.$this->name.'.'.$this->extension);
54
		
55
		if(!file_exists($this->filePath)) {
56
			throw new UnexistentFileException('The file '.$this->filePath.' does not exist !');
57
		}
58
		
59
		$this->size = filesize($this->filePath);
60
	}
61
	
62
	/**
63
	 * Return icon name.
64
	 * 
65
	 * @return String : the icon name.
66
	 */
67
	public function getName() {
68
		return $this->name;
69
	}
70
	
71
	/**
72
	 * Return icon extension.
73
	 * 
74
	 * @return String : the icon extension.
75
	 */
76
	public function getExtension() {
77
		return $this->extension;
78
	}
79
	
80
	
81
	/**
82
	 * Return icon file path.
83
	 * 
84
	 * @return String : the icon path.
85
	 */
86
	 public function getFilePath() {
87
	 	return $this->filePath;
88
	 }
89
	
90
	/**
91
	 * Return icon name.
92
	 * 
93
	 * @return int : the icon size.
94
	 */
95
	public function getSize() {
96
		return $this->size;
97
	}
98
	
99
	/**
100
	 * Magic to String method.
101
	 * 
102
	 * @return String
103
	 */
104
	public function __toString() {
105
		return $this->getFilePath();
106
	}
107
}