returnType = Soundbite::class; break; case 'video': $this->returnType = VideoClip::class; break; default: // do nothing, keep default class break; } parent::__construct($db, $validation); } /** * Gets all clips for an episode * * @return BaseClip[] */ public function getEpisodeSoundbites(int $podcastId, int $episodeId): array { $cacheName = "podcast#{$podcastId}_episode#{$episodeId}_soundbites"; if (! ($found = cache($cacheName))) { $found = $this->where([ 'episode_id' => $episodeId, 'podcast_id' => $podcastId, 'type' => 'audio', ]) ->orderBy('start_time') ->findAll(); foreach ($found as $key => $soundbite) { $found[$key] = new Soundbite($soundbite->toArray()); } cache() ->save($cacheName, $found, DECADE); } return $found; } /** * Gets all video clips for an episode * * @return BaseClip[] */ public function getVideoClips(int $podcastId, int $episodeId): array { $cacheName = "podcast#{$podcastId}_episode#{$episodeId}_video-clips"; if (! ($found = cache($cacheName))) { $found = $this->where([ 'episode_id' => $episodeId, 'podcast_id' => $podcastId, 'type' => 'video', ]) ->orderBy('start_time') ->findAll(); foreach ($found as $key => $videoClip) { $found[$key] = new VideoClip($videoClip->toArray()); } cache() ->save($cacheName, $found, DECADE); } return $found; } public function deleteSoundbite(int $podcastId, int $episodeId, int $clipId): BaseResult | bool { cache() ->delete("podcast#{$podcastId}_episode#{$episodeId}_soundbites"); return $this->delete([ 'podcast_id' => $podcastId, 'episode_id' => $episodeId, 'id' => $clipId, ]); } /** * @param array> $data * @return array> */ public function clearCache(array $data): array { $episode = (new EpisodeModel())->find( isset($data['data']) ? $data['data']['episode_id'] : $data['id']['episode_id'], ); // delete cache for rss feed cache() ->deleteMatching("podcast#{$episode->podcast_id}_feed*"); cache() ->deleteMatching("page_podcast#{$episode->podcast_id}_episode#{$episode->id}_*"); return $data; } }