| Total Complexity | 40 |
| Total Lines | 227 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PodcastService 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.
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 PodcastService, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 24 | class PodcastService { |
||
| 25 | private $channelBusinessLayer; |
||
| 26 | private $episodeBusinessLayer; |
||
| 27 | private $logger; |
||
| 28 | |||
| 29 | public const STATUS_OK = 0; |
||
| 30 | public const STATUS_INVALID_URL = 1; |
||
| 31 | public const STATUS_INVALID_RSS = 2; |
||
| 32 | public const STATUS_ALREADY_EXISTS = 3; |
||
| 33 | public const STATUS_NOT_FOUND = 4; |
||
| 34 | |||
| 35 | public function __construct( |
||
| 36 | PodcastChannelBusinessLayer $channelBusinessLayer, |
||
| 37 | PodcastEpisodeBusinessLayer $episodeBusinessLayer, |
||
| 38 | Logger $logger) { |
||
| 39 | $this->channelBusinessLayer = $channelBusinessLayer; |
||
| 40 | $this->episodeBusinessLayer = $episodeBusinessLayer; |
||
| 41 | $this->logger = $logger; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get a specified podcast channel of a user |
||
| 46 | */ |
||
| 47 | public function getChannel(int $id, string $userId, bool $includeEpisodes) : ?PodcastChannel { |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get all podcast channels of a user |
||
| 62 | * @return PodcastChannel[] |
||
| 63 | */ |
||
| 64 | public function getAllChannels(string $userId, bool $includeEpisodes) : array { |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get a specified podcast episode of a user |
||
| 76 | */ |
||
| 77 | public function getEpisode(int $id, string $userId) : ?PodcastEpisode { |
||
| 78 | try { |
||
| 79 | return $this->episodeBusinessLayer->find($id, $userId); |
||
| 80 | } catch (BusinessLayerException $ex) { |
||
| 81 | $this->logger->log("Requested episode $id not found: " . $ex->getMessage(), 'warn'); |
||
| 82 | return null; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Inject episodes to the given podcast channels |
||
| 88 | * @param PodcastChannel[] $channels input/output |
||
| 89 | * @param bool $allChannelsIncluded Set this to true if $channels contains all the podcasts of the user. |
||
| 90 | * This helps in optimizing the DB query. |
||
| 91 | */ |
||
| 92 | public function injectEpisodes(array &$channels, string $userId, bool $allChannelsIncluded) : void { |
||
| 93 | if ($allChannelsIncluded || \count($channels) > 999) { |
||
| 94 | $episodes = $this->episodeBusinessLayer->findAll($userId); |
||
| 95 | } else { |
||
| 96 | $episodes = $this->episodeBusinessLayer->findAllByChannel(Util::extractIds($channels), $userId); |
||
| 97 | } |
||
| 98 | |||
| 99 | $episodesPerChannel = []; |
||
| 100 | foreach ($episodes as $episode) { |
||
| 101 | $episodesPerChannel[$episode->getChannelId()][] = $episode; |
||
| 102 | } |
||
| 103 | |||
| 104 | foreach ($channels as &$channel) { |
||
| 105 | $channel->setEpisodes($episodesPerChannel[$channel->getId()] ?? []); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Add a followed podcast for a user from an RSS feed |
||
| 111 | * @return array like ['status' => int, 'channel' => ?PodcastChannel] |
||
| 112 | */ |
||
| 113 | public function subscribe(string $url, string $userId) : array { |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Deletes a podcast channel from a user |
||
| 144 | * @return int status code |
||
| 145 | */ |
||
| 146 | public function unsubscribe(int $channelId, string $userId) : int { |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Check a single podcast channel for updates |
||
| 159 | * @param ?string $prevHash Previous content hash known by the client. If given, the result will tell |
||
| 160 | * if the channel content has updated from this state. If omitted, the result |
||
| 161 | * will tell if the channel changed from its previous server-known state. |
||
| 162 | * @return array like ['status' => int, 'updated' => bool, 'channel' => ?PodcastChannel] |
||
| 163 | */ |
||
| 164 | public function updateChannel(int $id, string $userId, ?string $prevHash = null) : array { |
||
| 210 | ]; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Check updates for all chanenls of the user, one-by-one |
||
| 215 | * @return array like ['changed' => int, 'unchanged' => int, 'failed' => int] |
||
| 216 | * where each int represent number of channels in that category |
||
| 217 | */ |
||
| 218 | public function updateAllChannels(string $userId, ?float $olderThan, ?callable $progressCallback = null) : array { |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Reset all the subscribed podcasts of the user |
||
| 247 | */ |
||
| 248 | public function resetAll(string $userId) : void { |
||
| 254 |