Complex classes like Gravatar 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Gravatar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | 1 | final class Gravatar |
|
37 | { |
||
38 | /** |
||
39 | * Implement nette smart magic |
||
40 | */ |
||
41 | 1 | use Nette\SmartObject; |
|
42 | |||
43 | /** |
||
44 | * @var string - URL constants for the avatar images |
||
45 | */ |
||
46 | const HTTP_URL = 'http://www.gravatar.com/avatar/'; |
||
47 | const HTTPS_URL = 'https://secure.gravatar.com/avatar/'; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | private $expiration = 172800; // two days |
||
53 | |||
54 | /** |
||
55 | * The size to use for avatars. |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | private $size = 80; |
||
60 | |||
61 | /** |
||
62 | * The default image to use |
||
63 | * Either a string of the gravatar-recognized default image "type" to use, a URL, or FALSE if using the...default gravatar default image (hah) |
||
64 | * |
||
65 | * @var mixed |
||
66 | */ |
||
67 | private $defaultImage = FALSE; |
||
68 | |||
69 | /** |
||
70 | * The maximum rating to allow for the avatar. |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private $maxRating = 'g'; |
||
75 | |||
76 | /** |
||
77 | * Should we use the secure (HTTPS) URL base? |
||
78 | * |
||
79 | * @var bool |
||
80 | */ |
||
81 | private $useSecureUrl = FALSE; |
||
82 | |||
83 | /** |
||
84 | * @var Utils\Image |
||
85 | */ |
||
86 | private $image; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | private $type; |
||
92 | |||
93 | /** |
||
94 | * @var bool |
||
95 | */ |
||
96 | private $hashEmail = TRUE; |
||
97 | |||
98 | /** |
||
99 | * @var Caching\Cache |
||
100 | */ |
||
101 | private $cache; |
||
102 | |||
103 | /** |
||
104 | * @param Http\Request $httpRequest |
||
105 | * @param Caching\Cache $cache |
||
106 | */ |
||
107 | public function __construct( |
||
116 | |||
117 | /** |
||
118 | * Get the email hash to use (after cleaning the string) |
||
119 | * |
||
120 | * @param string|NULL $email |
||
121 | * |
||
122 | * @return string - The hashed form of the email, post cleaning |
||
123 | */ |
||
124 | public function getEmailHash(string $email = NULL) : string |
||
138 | |||
139 | /** |
||
140 | * Set the avatar size to use |
||
141 | * |
||
142 | * @param int $size - The avatar size to use, must be less than 512 and greater than 0 |
||
143 | * |
||
144 | * @return void |
||
145 | */ |
||
146 | public function setSize(int $size) : void |
||
152 | |||
153 | /** |
||
154 | * Get the currently set avatar size |
||
155 | * |
||
156 | * @return int |
||
157 | */ |
||
158 | public function getSize() : int |
||
162 | |||
163 | /** |
||
164 | * @param int $size |
||
165 | * |
||
166 | * @return bool |
||
167 | * |
||
168 | * @throws Exceptions\InvalidArgumentException |
||
169 | */ |
||
170 | public function isSizeValid(int $size) : bool |
||
178 | |||
179 | /** |
||
180 | * Set image cache expiration |
||
181 | * |
||
182 | * @param int $expiration |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | public function setExpiration(int $expiration) : void |
||
190 | |||
191 | /** |
||
192 | * Set the default image to use for avatars |
||
193 | * |
||
194 | * @param mixed $image - The default image to use. Use boolean FALSE for the gravatar default, a string containing a valid image URL, or a string specifying a recognized gravatar "default". |
||
195 | * |
||
196 | * @return void |
||
197 | * |
||
198 | * @throws Exceptions\InvalidArgumentException |
||
199 | */ |
||
200 | public function setDefaultImage($image) : void |
||
224 | |||
225 | /** |
||
226 | * Get the current default image setting |
||
227 | * |
||
228 | * @param string|NULL $defaultImage |
||
229 | * |
||
230 | * @return mixed - False if no default image set, string if one is set |
||
231 | */ |
||
232 | public function getDefaultImage(string $defaultImage = NULL) : ?string |
||
244 | |||
245 | /** |
||
246 | * Set the maximum allowed rating for avatars. |
||
247 | * |
||
248 | * @param string $rating - The maximum rating to use for avatars ('g', 'pg', 'r', 'x') |
||
249 | * |
||
250 | * @return void |
||
251 | * |
||
252 | * @throws Exceptions\InvalidArgumentException |
||
253 | */ |
||
254 | public function setMaxRating(string $rating) : void |
||
264 | |||
265 | /** |
||
266 | * Get the current maximum allowed rating for avatars |
||
267 | * |
||
268 | * @param string|NULL $maxRating |
||
269 | * |
||
270 | * @return string - The string representing the current maximum allowed rating ('g', 'pg', 'r', 'x'). |
||
271 | */ |
||
272 | public function getMaxRating(string $maxRating = NULL) : string |
||
280 | |||
281 | /** |
||
282 | * Returns the Nette\Image instance |
||
283 | * |
||
284 | * @return Utils\Image |
||
285 | */ |
||
286 | public function getImage() : Utils\Image |
||
290 | |||
291 | /** |
||
292 | * Returns the type of a image |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getImageType() : string |
||
300 | |||
301 | /** |
||
302 | * Check if we are using the secure protocol for the image URLs |
||
303 | * |
||
304 | * @return bool - Are we supposed to use the secure protocol? |
||
305 | */ |
||
306 | public function usingSecureImages() : bool |
||
310 | |||
311 | /** |
||
312 | * Enable the use of the secure protocol for image URLs |
||
313 | * |
||
314 | * @return void |
||
315 | */ |
||
316 | public function enableSecureImages() : void |
||
320 | |||
321 | /** |
||
322 | * Disable the use of the secure protocol for image URLs |
||
323 | * |
||
324 | * @return void |
||
325 | */ |
||
326 | public function disableSecureImages() : void |
||
330 | |||
331 | /** |
||
332 | * Create gravatar image |
||
333 | * |
||
334 | * @param string|NULL $email |
||
335 | * @param int|NULL $size |
||
336 | * |
||
337 | * @return Utils\Image |
||
338 | * |
||
339 | * @throws Exceptions\InvalidArgumentException |
||
340 | */ |
||
341 | public function get(string $email = NULL, int $size = NULL) : Utils\Image |
||
368 | |||
369 | /** |
||
370 | * Build the avatar URL based on the provided email address |
||
371 | * |
||
372 | * @param string|NULL $email |
||
373 | * @param int|NULL $size |
||
374 | * @param string|NULL $maxRating |
||
375 | * @param string|NULL $defaultImage |
||
376 | * |
||
377 | * @return string |
||
378 | * |
||
379 | * @throws Exceptions\InvalidArgumentException |
||
380 | */ |
||
381 | public function buildUrl(string $email = NULL, int $size = NULL, string $maxRating = NULL, string $defaultImage = NULL) : string |
||
394 | |||
395 | /** |
||
396 | * Checks if a gravatar exists for the email. It does this by checking for the presence of 404 in the header |
||
397 | * returned. Will return null if fsockopen fails, for example when the hostname cannot be resolved. |
||
398 | * |
||
399 | * @param string $email |
||
400 | * |
||
401 | * @return bool|NULL Boolean if we could connect, null if no connection to gravatar.com |
||
402 | */ |
||
403 | public function exists(string $email) : ?bool |
||
417 | |||
418 | /** |
||
419 | * @return Templating\Helpers |
||
420 | */ |
||
421 | public function createTemplateHelpers() : Templating\Helpers |
||
425 | |||
426 | /** |
||
427 | * @param string $email |
||
428 | * @param int|NULL $size |
||
429 | * @param string|NULL $maxRating |
||
430 | * @param string|NULL $defaultImage |
||
431 | * |
||
432 | * @return Http\Url |
||
433 | */ |
||
434 | private function createUrl(string $email, int $size = NULL, string $maxRating = NULL, string $defaultImage = NULL) : Http\Url |
||
459 | } |
||
460 |