returnType = Audio::class; break; case 'video': $this->returnType = Video::class; break; case 'image': $this->returnType = Image::class; break; case 'transcript': $this->returnType = Transcript::class; break; case 'chapters': $this->returnType = Chapters::class; break; default: // do nothing, keep Document class as default break; } parent::__construct($db, $validation); } public function getMediaById(int $mediaId): mixed { $cacheName = "media#{$mediaId}"; if (! ($found = cache($cacheName))) { $builder = $this->where([ 'id' => $mediaId, ]); $result = $builder->first(); $mediaClass = $this->returnType; $found = new $mediaClass($result->toArray(false, true)); cache() ->save($cacheName, $found, DECADE); } return $found; } /** * @param Document|Audio|Video|Image|Transcript|Chapters $media * * @noRector ReturnTypeDeclarationRector */ public function saveMedia(object $media): int | false { // insert record in database if (! $mediaId = $this->insert($media, true)) { return false; } return $mediaId; } /** * @param Document|Audio|Video|Image|Transcript|Chapters $media * * @noRector ReturnTypeDeclarationRector */ public function updateMedia(object $media): bool { return $this->update($media->id, $media); } /** * @return array */ public function getAllOfType(): array { $result = $this->where('type', $this->fileType) ->findAll(); $mediaClass = $this->returnType; foreach ($result as $key => $media) { $result[$key] = new $mediaClass($media->toArray(false, true)); } return $result; } public function deleteMedia(object $media): bool|BaseResult { $media->deleteFile(); return $this->delete($media->id, true); } /** * @param mixed[] $data * * @return mixed[] */ protected function clearCache(array $data): array { $mediaId = (is_array($data['id']) ? $data['id'][0] : $data['id']); cache() ->delete("media#{$mediaId}"); return $data; } }