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