Total Complexity | 67 |
Total Lines | 338 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Thumbnails often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Thumbnails, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Thumbnails |
||
37 | { |
||
38 | public $_imgName = 'blank.gif'; |
||
39 | public $_img_path = 'uploads'; |
||
40 | public $_img_savepath = 'thumbs'; |
||
41 | public $_source_path = ''; |
||
42 | public $_save_path = ''; |
||
43 | public $_source_url = ''; |
||
44 | public $_source_image = ''; |
||
45 | public $_save_image = ''; |
||
46 | public $_usethumbs = 0; |
||
47 | public $_image_type = 'gd2'; |
||
48 | public $_return_fullpath = 0; |
||
49 | public $img_width = 100; |
||
50 | public $img_height = 100; |
||
51 | public $img_quality = 100; |
||
52 | public $img_update = 1; |
||
53 | public $img_aspect = 1; |
||
54 | // @access private |
||
55 | public $_img_info = []; |
||
56 | |||
57 | /** |
||
58 | * Constructor |
||
59 | * |
||
60 | * @param null $img_name |
||
|
|||
61 | * @param null $img_path |
||
62 | * @param null $img_savepath |
||
63 | * |
||
64 | * @internal param string $_imgName |
||
65 | * @internal param string $_img_path |
||
66 | * @internal param string $_img_savepath |
||
67 | */ |
||
68 | public function __construct($img_name = null, $img_path = null, $img_savepath = null) |
||
69 | { |
||
70 | if (!\preg_match('/\.(jpg|gif|png|jpeg)$/i', $img_name)) { |
||
71 | return false; |
||
72 | } |
||
73 | |||
74 | // The actual image we will be processing |
||
75 | if (!null === $img_name) { |
||
76 | $this->_imgName = \trim($img_name); |
||
77 | } |
||
78 | |||
79 | // The image path |
||
80 | if (!null === $img_path) { |
||
81 | $this->_img_path = \trim($img_path); |
||
82 | } |
||
83 | |||
84 | // The image save path |
||
85 | if (!null === $img_savepath) { |
||
86 | $this->_img_savepath = \trim($img_savepath); |
||
87 | } |
||
88 | |||
89 | $path_to_check = XOOPS_ROOT_PATH . "/$img_path/$img_savepath"; |
||
90 | |||
91 | if (!\is_dir($path_to_check)) { |
||
92 | if (!\mkdir($path_to_check, 0777) && !\is_dir($path_to_check)) { |
||
93 | return false; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return null; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * wfThumbsNails::setUseThumbs() |
||
102 | * |
||
103 | * @param int $value |
||
104 | */ |
||
105 | public function setUseThumbs($value = 1) |
||
106 | { |
||
107 | $this->_usethumbs = $value; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * XtubeThumbsNails::setImageType() |
||
112 | * |
||
113 | * @param string $value |
||
114 | */ |
||
115 | public function setImageType($value = 'gd2') |
||
116 | { |
||
117 | $this->_image_type = $value; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * ThumbsNails::createThumbnail() |
||
122 | * |
||
123 | * @param int $img_width |
||
124 | * @param int $img_height |
||
125 | * @param int $img_quality |
||
126 | * @param int $img_update |
||
127 | * @param int $img_aspect |
||
128 | * |
||
129 | * @return bool|string |
||
130 | */ |
||
131 | public function createThumbnail( |
||
132 | $img_width = null, |
||
133 | $img_height = null, |
||
134 | $img_quality = null, |
||
135 | $img_update = null, |
||
136 | $img_aspect = null |
||
137 | ) { |
||
138 | $this->_source_path = XOOPS_ROOT_PATH . "/{$this->_img_path}"; |
||
139 | $this->_save_path = XOOPS_ROOT_PATH . "/{$this->_img_path}/{$this->_img_savepath}"; |
||
140 | $this->_source_url = XOOPS_URL . "/{$this->_img_path}"; |
||
141 | $this->_source_image = "{$this->_source_path}/{$this->_imgName}"; |
||
142 | |||
143 | if (isset($img_width) && !null === $img_width) { |
||
144 | $this->img_width = (int)$img_width; |
||
145 | } |
||
146 | |||
147 | if (isset($img_height) && !null === $img_height) { |
||
148 | $this->img_height = (int)$img_height; |
||
149 | } |
||
150 | |||
151 | if (isset($img_quality) && !null === $img_quality) { |
||
152 | $this->img_quality = (int)$img_quality; |
||
153 | } |
||
154 | |||
155 | if (isset($img_update) && !null === $img_update) { |
||
156 | $this->img_update = (int)$img_update; |
||
157 | } |
||
158 | |||
159 | if (isset($img_aspect) && !null === $img_aspect) { |
||
160 | $this->img_aspect = (int)$img_aspect; |
||
161 | } |
||
162 | |||
163 | // Return false if we are not using thumbnails |
||
164 | if (!$this->useThumbs()) { |
||
165 | return $this->_source_url . '/' . $this->_imgName; |
||
166 | } |
||
167 | // Return false if the server does not have gd lib installed or activated |
||
168 | if (!$this->checkGdLibrary()) { |
||
169 | return $this->_source_url . '/' . $this->_imgName; |
||
170 | } |
||
171 | |||
172 | // Return false if the paths to the file are wrong |
||
173 | if (!$this->checkPaths()) { |
||
174 | return DEFAULT_PATH; |
||
175 | } |
||
176 | |||
177 | if (!$this->checkImage()) { |
||
178 | return DEFAULT_PATH; |
||
179 | } |
||
180 | |||
181 | $image = $this->resizeThumbnail(); |
||
182 | |||
183 | return false === $image ? DEFAULT_PATH : $image; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param $value |
||
188 | */ |
||
189 | public function setImageName($value) |
||
190 | { |
||
191 | $this->_imgName = \trim($value); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param $value |
||
196 | */ |
||
197 | public function setImagePath($value) |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param $value |
||
204 | */ |
||
205 | public function setImgSavePath($value) |
||
206 | { |
||
207 | $this->_img_savepath = \trim($value); |
||
208 | } |
||
209 | |||
210 | // ThumbsNails::resizeThumbnail() |
||
211 | // @return |
||
212 | |||
213 | /** |
||
214 | * @return bool|string |
||
215 | */ |
||
216 | public function resizeThumbnail() |
||
305 | } |
||
306 | // return FALSE; |
||
307 | } |
||
308 | |||
309 | // ThumbsNails::checkPaths() |
||
310 | // @return |
||
311 | |||
312 | /** |
||
313 | * @return bool |
||
314 | */ |
||
315 | public function checkPaths() |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * @return bool |
||
329 | */ |
||
330 | public function checkImage() |
||
331 | { |
||
332 | $this->_img_info = \getimagesize($this->_source_image, $imageinfo); |
||
333 | if (null === $this->_img_info) { |
||
334 | return false; |
||
335 | } |
||
336 | |||
337 | return true; |
||
338 | } |
||
339 | |||
340 | // wfsThumbs::checkGdLibrary() |
||
341 | // Private function |
||
342 | // @return false if gd lib not found on the system |
||
343 | |||
344 | /** |
||
345 | * @return array|bool |
||
346 | */ |
||
347 | public function checkGdLibrary() |
||
358 | } |
||
359 | |||
360 | // ThumbsNails::useThumbs() |
||
361 | // |
||
362 | // @return |
||
363 | |||
364 | /** |
||
365 | * @return bool |
||
366 | */ |
||
367 | public function useThumbs() |
||
374 | } |
||
375 | } |
||
376 |