returnType = Audio::class; break; case 'image': $this->returnType = Image::class; break; default: // do nothing, keep Media class as default break; } parent::__construct($db, $validation); } /** * @return Media|Image|Audio */ public function getMediaById(int $mediaId): object { $cacheName = "media#{$mediaId}"; if (! ($found = cache($cacheName))) { $builder = $this->where([ 'id' => $mediaId, ]); $found = $builder->first(); cache() ->save($cacheName, $found, DECADE); } return $found; } /** * @param Media|Image $media */ public function saveMedia(object $media): int | false { // insert record in database if (! $mediaId = $this->insert($media, true)) { return false; } // @phpstan-ignore-next-line return $mediaId; } public function deleteFile(int $mediaId): void { // TODO: get file, delete it from disk & from database } }