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