*/ protected $casts = [ 'id' => 'integer', 'podcast_id' => 'integer', 'episode_id' => 'integer', 'title' => 'string', 'start_time' => 'double', 'duration' => 'double', 'type' => 'string', 'media_id' => '?integer', 'metadata' => '?json-array', 'status' => 'string', 'logs' => 'string', 'created_by' => 'integer', 'updated_by' => 'integer', ]; /** * @param array|null $data */ public function __construct(array $data = null) { parent::__construct($data); } public function getJobDuration(): ?int { if ($this->job_duration === null && $this->job_started_at && $this->job_ended_at) { $this->job_duration = ($this->job_started_at->difference($this->job_ended_at)) ->getSeconds(); } return $this->job_duration; } public function getEndTime(): float { if ($this->end_time === null) { $this->end_time = $this->start_time + $this->duration; } return $this->end_time; } public function getPodcast(): ?Podcast { return (new PodcastModel())->getPodcastById($this->podcast_id); } public function getEpisode(): ?Episode { return (new EpisodeModel())->getEpisodeById($this->episode_id); } public function getUser(): ?User { return (new UserModel())->find($this->created_by); } public function setMedia(string $filePath = null): static { if ($filePath === null) { return $this; } $file = new File($filePath); if ($this->media_id !== null) { $this->getMedia() ->setFile($file); $this->getMedia() ->updated_by = (int) user_id(); (new MediaModel('audio'))->updateMedia($this->getMedia()); } else { $media = new Audio([ 'file_path' => $filePath, 'language_code' => $this->getPodcast() ->language_code, 'uploaded_by' => $this->attributes['created_by'], 'updated_by' => $this->attributes['created_by'], ]); $media->setFile($file); $this->attributes['media_id'] = (new MediaModel())->saveMedia($media); } return $this; } public function getMedia(): Audio | Video | null { if ($this->media_id !== null && $this->media === null) { $this->media = (new MediaModel($this->type))->getMediaById($this->media_id); } return $this->media; } }