6
6
7
7
class Common
8
8
{
9
+ /**
10
+ * @var string
11
+ */
9
12
protected $ binaryPath = '' ;
13
+
14
+ /**
15
+ * @var string
16
+ */
10
17
protected $ imagePath = '' ;
18
+
19
+ /**
20
+ * @var string
21
+ */
11
22
protected $ outputPath = '' ;
12
- protected $ originalFileSize = '' ;
13
- protected $ finalFileSize = '' ;
23
+
24
+ /**
25
+ * @var int
26
+ */
27
+ protected $ originalFileSize = 0 ;
28
+
29
+ /**
30
+ * @var int
31
+ */
32
+ protected $ finalFileSize = 0 ;
33
+
34
+ /**
35
+ * @var int
36
+ */
14
37
protected $ optimisationLevel = 1 ;
38
+
39
+ /**
40
+ * @var bool
41
+ */
15
42
protected $ stopIfFail = true ;
16
43
17
44
/**
18
45
* @param string $binaryPath
19
46
* @return $this
20
47
* @throws Exception
21
48
*/
22
- public function setBinaryPath ($ binaryPath = '' )
49
+ public function setBinaryPath (string $ binaryPath = '' ): Common
23
50
{
24
51
if (!file_exists ($ binaryPath )) {
25
52
throw new Exception ('Unable to locate binary file ' );
@@ -30,22 +57,21 @@ public function setBinaryPath($binaryPath = '')
30
57
}
31
58
32
59
/**
33
- * @param string $stopIfFail
60
+ * @param bool $stopIfFail
34
61
* @return $this
35
- * @throws Exception
36
62
*/
37
- public function setStopIfFail ($ stopIfFail )
63
+ public function setStopIfFail (bool $ stopIfFail ): Common
38
64
{
39
- $ this ->stopIfFail = boolval ( $ stopIfFail) ;
65
+ $ this ->stopIfFail = $ stopIfFail ;
40
66
return $ this ;
41
67
}
42
68
43
69
/**
44
- * @param $imagePath
45
- * @return $this
70
+ * @param string $imagePath
71
+ * @return Common
46
72
* @throws Exception
47
73
*/
48
- public function setImagePath ($ imagePath )
74
+ public function setImagePath (string $ imagePath ): Common
49
75
{
50
76
if (!file_exists ($ imagePath )) {
51
77
throw new Exception ('Invald image path ' );
@@ -64,7 +90,7 @@ public function setImagePath($imagePath)
64
90
* @return $this
65
91
* @throws Exception
66
92
*/
67
- public function setOptimisationLevel ($ level = 2 )
93
+ public function setOptimisationLevel (int $ level = 2 ): Common
68
94
{
69
95
if (!is_int ($ level )) {
70
96
throw new Exception ('Invalid Optimisation Level ' );
@@ -82,20 +108,34 @@ public function setOptimisationLevel($level = 2)
82
108
}
83
109
84
110
/**
85
- * @return $this
111
+ * @return Common
112
+ * @throws Exception
86
113
*/
87
- public function determinePreOptimisedFileSize ()
114
+ public function determinePreOptimisedFileSize (): Common
88
115
{
89
- $ this ->originalFileSize = filesize ($ this ->imagePath );
116
+ $ fileSize = filesize ($ this ->imagePath );
117
+
118
+ if ($ fileSize === false ) {
119
+ throw new \Exception ('Unable to determine pre-optimised fileSize ' );
120
+ }
121
+
122
+ $ this ->originalFileSize = $ fileSize ;
90
123
return $ this ;
91
124
}
92
125
93
126
/**
94
- * @return $this
127
+ * @return Common
128
+ * @throws Exception
95
129
*/
96
- public function determinePostOptimisedFileSize ()
130
+ public function determinePostOptimisedFileSize (): Common
97
131
{
98
- $ this ->finalFileSize = filesize ($ this ->imagePath );
132
+ $ fileSize = filesize ($ this ->imagePath );
133
+
134
+ if ($ fileSize === false ) {
135
+ throw new \Exception ('Unable to determine post-optimised fileSize ' );
136
+ }
137
+
138
+ $ this ->finalFileSize = $ fileSize ;
99
139
return $ this ;
100
140
}
101
141
}
0 commit comments