Complex classes like LastfmService 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 LastfmService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class LastfmService extends ApiClient implements ApiConsumerInterface |
||
| 12 | { |
||
| 13 | public const SESSION_KEY_PREFERENCE_KEY = 'lastfm_session_key'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Specify the response format, since Last.fm only returns XML. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $responseFormat = 'xml'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Override the key param, since, again, Lastfm wants to be different. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $keyParam = 'api_key'; |
||
| 28 | |||
| 29 | private $userPreferenceService; |
||
| 30 | |||
| 31 | public function __construct( |
||
| 40 | |||
| 41 | 5 | /** |
|
| 42 | * Determine if our application is using Last.fm. |
||
| 43 | */ |
||
| 44 | public function used(): bool |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Determine if Last.fm integration is enabled. |
||
| 51 | 2 | */ |
|
| 52 | public function enabled(): bool |
||
| 56 | |||
| 57 | 2 | /** |
|
| 58 | * Get information about an artist. |
||
| 59 | * |
||
| 60 | 2 | * @param string $name Name of the artist |
|
| 61 | * |
||
| 62 | 2 | * @return mixed[]|null |
|
| 63 | */ |
||
| 64 | public function getArtistInformation(string $name): ?array |
||
| 95 | 1 | ||
| 96 | /** |
||
| 97 | 1 | * Build a Koel-usable array of artist information using the data from Last.fm. |
|
| 98 | 1 | * |
|
| 99 | * @param mixed[] $artistData |
||
| 100 | * |
||
| 101 | * @return mixed[] |
||
| 102 | */ |
||
| 103 | private function buildArtistInformation(array $artistData): array |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get information about an album. |
||
| 117 | 2 | * |
|
| 118 | 2 | * @return mixed[]|null |
|
| 119 | */ |
||
| 120 | public function getAlbumInformation(string $albumName, string $artistName): ?array |
||
| 154 | |||
| 155 | 1 | /** |
|
| 156 | 1 | * Build a Koel-usable array of album information using the data from Last.fm. |
|
| 157 | * |
||
| 158 | 1 | * @param mixed[] $albumData |
|
| 159 | 1 | * |
|
| 160 | * @return mixed[] |
||
| 161 | 1 | */ |
|
| 162 | private function buildAlbumInformation(array $albumData): array |
||
| 180 | 1 | ||
| 181 | /** |
||
| 182 | 1 | * Get Last.fm's session key for the authenticated user using a token. |
|
| 183 | 1 | * |
|
| 184 | 1 | * @param string $token The token after successfully connecting to Last.fm |
|
| 185 | 1 | * |
|
| 186 | * @link http://www.last.fm/api/webauth#4 |
||
| 187 | */ |
||
| 188 | 1 | public function fetchSessionKeyUsingToken(string $token): ?string |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Scrobble a song. |
||
| 206 | * |
||
| 207 | * @param string $artist The artist name |
||
| 208 | * @param string $track The track name |
||
| 209 | * @param string|int $timestamp The UNIX timestamp |
||
| 210 | * @param string $album The album name |
||
| 211 | * @param string $sk The session key |
||
| 212 | */ |
||
| 213 | public function scrobble(string $artist, string $track, $timestamp, string $album, string $sk): void |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Love or unlove a track on Last.fm. |
||
| 232 | * |
||
| 233 | * @param string $track The track name |
||
| 234 | * @param string $artist The artist's name |
||
| 235 | * @param string $sk The session key |
||
| 236 | * @param bool $love Whether to love or unlove. Such cheesy terms... urrgggh |
||
| 237 | */ |
||
| 238 | public function toggleLoveTrack(string $track, string $artist, string $sk, ?bool $love = true): void |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Update a track's "now playing" on Last.fm. |
||
| 252 | * |
||
| 253 | * @param string $artist Name of the artist |
||
| 254 | * @param string $track Name of the track |
||
| 255 | * @param string $album Name of the album |
||
| 256 | * @param int|float $duration Duration of the track, in seconds |
||
| 257 | * @param string $sk The session key |
||
| 258 | */ |
||
| 259 | public function updateNowPlaying(string $artist, string $track, string $album, $duration, string $sk): void |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Build the parameters to use for _authenticated_ Last.fm API calls. |
||
| 277 | * Such calls require: |
||
| 278 | * - The API key (api_key) |
||
| 279 | * - The API signature (api_sig). |
||
| 280 | * |
||
| 281 | * @link http://www.last.fm/api/webauth#5 |
||
| 282 | * |
||
| 283 | * @param array $params The array of parameters. |
||
| 284 | * @param bool $toString Whether to turn the array into a query string |
||
| 285 | * |
||
| 286 | * @return array|string |
||
| 287 | */ |
||
| 288 | public function buildAuthCallParams(array $params, bool $toString = false) |
||
| 315 | 2 | ||
| 316 | /** |
||
| 317 | * Correctly format a value returned by Last.fm. |
||
| 318 | * |
||
| 319 | * @param string|array $value |
||
| 320 | */ |
||
| 321 | protected function formatText($value): string |
||
| 329 | |||
| 330 | public function getUserSessionKey(User $user): ?string |
||
| 334 | 6 | ||
| 335 | public function setUserSessionKey(User $user, string $sessionKey): void |
||
| 339 | 5 | ||
| 340 | public function deleteUserSessionKey(User $user): void |
||
| 344 | 6 | ||
| 345 | public function isUserConnected(User $user): bool |
||
| 349 | |||
| 350 | public function getKey(): ?string |
||
| 354 | |||
| 355 | public function getEndpoint(): ?string |
||
| 359 | |||
| 360 | public function getSecret(): ?string |
||
| 364 | } |
||
| 365 |
This check looks for function calls that miss required arguments.