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