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 | 1 | use Nette\SmartObject; |
|
39 | |||
40 | /** |
||
41 | * @var string - URL constants for the avatar images |
||
42 | */ |
||
43 | const HTTP_URL = 'http://www.gravatar.com/avatar/'; |
||
44 | const HTTPS_URL = 'https://secure.gravatar.com/avatar/'; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $expiration = 172800; // two days |
||
50 | |||
51 | /** |
||
52 | * The size to use for avatars. |
||
53 | * |
||
54 | * @var int |
||
55 | */ |
||
56 | private $size = 80; |
||
57 | |||
58 | /** |
||
59 | * The default image to use |
||
60 | * Either a string of the gravatar-recognized default image "type" to use, a URL, or FALSE if using the...default gravatar default image (hah) |
||
61 | * |
||
62 | * @var mixed |
||
63 | */ |
||
64 | private $defaultImage = FALSE; |
||
65 | |||
66 | /** |
||
67 | * The maximum rating to allow for the avatar. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | private $maxRating = 'g'; |
||
72 | |||
73 | /** |
||
74 | * Should we use the secure (HTTPS) URL base? |
||
75 | * |
||
76 | * @var bool |
||
77 | */ |
||
78 | private $useSecureUrl = FALSE; |
||
79 | |||
80 | /** |
||
81 | * @var Utils\Image |
||
82 | */ |
||
83 | private $image; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | */ |
||
88 | private $type; |
||
89 | |||
90 | /** |
||
91 | * @var bool |
||
92 | */ |
||
93 | private $hashEmail = TRUE; |
||
94 | |||
95 | /** |
||
96 | * @var Caching\Cache |
||
97 | */ |
||
98 | private $cache; |
||
99 | |||
100 | /** |
||
101 | * @param Http\Request $httpRequest |
||
102 | * @param Caching\Cache $cache |
||
103 | */ |
||
104 | public function __construct( |
||
113 | |||
114 | /** |
||
115 | * Get the email hash to use (after cleaning the string) |
||
116 | * |
||
117 | * @param string|NULL $email |
||
118 | * |
||
119 | * @return string - The hashed form of the email, post cleaning |
||
120 | */ |
||
121 | public function getEmailHash(string $email = NULL) : string |
||
135 | |||
136 | /** |
||
137 | * Set the avatar size to use |
||
138 | * |
||
139 | * @param int $size - The avatar size to use, must be less than 512 and greater than 0 |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function setSize(int $size) |
||
149 | |||
150 | /** |
||
151 | * Get the currently set avatar size |
||
152 | * |
||
153 | * @return int |
||
154 | */ |
||
155 | public function getSize() : int |
||
159 | |||
160 | /** |
||
161 | * @param int $size |
||
162 | * |
||
163 | * @return bool |
||
164 | * |
||
165 | * @throws Exceptions\InvalidArgumentException |
||
166 | */ |
||
167 | public function isSizeValid(int $size) : bool |
||
175 | |||
176 | /** |
||
177 | * Set image cache expiration |
||
178 | * |
||
179 | * @param int $expiration |
||
180 | * |
||
181 | * @return void |
||
182 | */ |
||
183 | public function setExpiration(int $expiration) |
||
187 | |||
188 | /** |
||
189 | * Set the default image to use for avatars |
||
190 | * |
||
191 | * @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". |
||
192 | * |
||
193 | * @return void |
||
194 | * |
||
195 | * @throws Exceptions\InvalidArgumentException |
||
196 | */ |
||
197 | public function setDefaultImage($image) |
||
221 | |||
222 | /** |
||
223 | * Get the current default image setting |
||
224 | * |
||
225 | * @param string|NULL $defaultImage |
||
226 | * |
||
227 | * @return mixed - False if no default image set, string if one is set |
||
228 | */ |
||
229 | public function getDefaultImage(string $defaultImage = NULL) |
||
241 | |||
242 | /** |
||
243 | * Set the maximum allowed rating for avatars. |
||
244 | * |
||
245 | * @param string $rating - The maximum rating to use for avatars ('g', 'pg', 'r', 'x') |
||
246 | * |
||
247 | * @return void |
||
248 | * |
||
249 | * @throws Exceptions\InvalidArgumentException |
||
250 | */ |
||
251 | public function setMaxRating(string $rating) |
||
261 | |||
262 | /** |
||
263 | * Get the current maximum allowed rating for avatars |
||
264 | * |
||
265 | * @param string|NULL $maxRating |
||
266 | * |
||
267 | * @return string - The string representing the current maximum allowed rating ('g', 'pg', 'r', 'x'). |
||
268 | */ |
||
269 | public function getMaxRating(string $maxRating = NULL) : string |
||
277 | |||
278 | /** |
||
279 | * Returns the Nette\Image instance |
||
280 | * |
||
281 | * @return Utils\Image |
||
282 | */ |
||
283 | public function getImage() : Utils\Image |
||
287 | |||
288 | /** |
||
289 | * Returns the type of a image |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | public function getImageType() : string |
||
297 | |||
298 | /** |
||
299 | * Check if we are using the secure protocol for the image URLs |
||
300 | * |
||
301 | * @return bool - Are we supposed to use the secure protocol? |
||
302 | */ |
||
303 | public function usingSecureImages() : bool |
||
307 | |||
308 | /** |
||
309 | * Enable the use of the secure protocol for image URLs |
||
310 | * |
||
311 | * @return void |
||
312 | */ |
||
313 | public function enableSecureImages() |
||
317 | |||
318 | /** |
||
319 | * Disable the use of the secure protocol for image URLs |
||
320 | * |
||
321 | * @return void |
||
322 | */ |
||
323 | public function disableSecureImages() |
||
327 | |||
328 | /** |
||
329 | * Create gravatar image |
||
330 | * |
||
331 | * @param string|NULL $email |
||
332 | * @param int|NULL $size |
||
333 | * |
||
334 | * @return Utils\Image |
||
335 | * |
||
336 | * @throws Exceptions\InvalidArgumentException |
||
337 | */ |
||
338 | public function get(string $email = NULL, int $size = NULL) : Utils\Image |
||
365 | |||
366 | /** |
||
367 | * Build the avatar URL based on the provided email address |
||
368 | * |
||
369 | * @param string|NULL $email |
||
370 | * @param int|NULL $size |
||
371 | * @param string|NULL $maxRating |
||
372 | * @param string|NULL $defaultImage |
||
373 | * |
||
374 | * @return string |
||
375 | * |
||
376 | * @throws Exceptions\InvalidArgumentException |
||
377 | */ |
||
378 | public function buildUrl(string $email = NULL, int $size = NULL, string $maxRating = NULL, string $defaultImage = NULL) : string |
||
391 | |||
392 | /** |
||
393 | * Checks if a gravatar exists for the email. It does this by checking for the presence of 404 in the header |
||
394 | * returned. Will return null if fsockopen fails, for example when the hostname cannot be resolved. |
||
395 | * |
||
396 | * @param string $email |
||
397 | * |
||
398 | * @return bool|NULL Boolean if we could connect, null if no connection to gravatar.com |
||
399 | */ |
||
400 | public function exists(string $email) |
||
414 | |||
415 | /** |
||
416 | * @return Templating\Helpers |
||
417 | */ |
||
418 | public function createTemplateHelpers() : Templating\Helpers |
||
422 | |||
423 | /** |
||
424 | * @param string $email |
||
425 | * @param int|NULL $size |
||
426 | * @param string|NULL $maxRating |
||
427 | * @param string|NULL $defaultImage |
||
428 | * |
||
429 | * @return Http\Url |
||
430 | */ |
||
431 | private function createUrl(string $email, int $size = NULL, string $maxRating = NULL, string $defaultImage = NULL) : Http\Url |
||
456 | } |
||
457 |