2021-12-21 16:25:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2021-12-21 16:25:03 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Entities\Clip;
|
|
|
|
|
|
|
|
use App\Entities\Episode;
|
|
|
|
use App\Entities\Podcast;
|
|
|
|
use App\Models\EpisodeModel;
|
|
|
|
use App\Models\PodcastModel;
|
|
|
|
use CodeIgniter\Entity\Entity;
|
|
|
|
use CodeIgniter\Files\File;
|
2021-12-24 17:55:56 +00:00
|
|
|
use CodeIgniter\I18n\Time;
|
2022-10-15 11:22:08 +00:00
|
|
|
use CodeIgniter\Shield\Entities\User;
|
|
|
|
use Modules\Auth\Models\UserModel;
|
2023-03-16 13:00:05 +00:00
|
|
|
use Modules\Media\Entities\Audio;
|
|
|
|
use Modules\Media\Entities\Video;
|
|
|
|
use Modules\Media\Models\MediaModel;
|
2021-12-21 16:25:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property int $id
|
|
|
|
* @property int $podcast_id
|
|
|
|
* @property Podcast $podcast
|
|
|
|
* @property int $episode_id
|
|
|
|
* @property Episode $episode
|
2022-01-03 13:52:07 +00:00
|
|
|
* @property string $title
|
2021-12-21 16:25:03 +00:00
|
|
|
* @property double $start_time
|
|
|
|
* @property double $end_time
|
|
|
|
* @property double $duration
|
|
|
|
* @property string $type
|
2021-12-24 17:55:56 +00:00
|
|
|
* @property int|null $media_id
|
|
|
|
* @property Video|Audio|null $media
|
2024-12-17 15:06:08 +00:00
|
|
|
* @property array<mixed>|null $metadata
|
2021-12-21 16:25:03 +00:00
|
|
|
* @property string $status
|
|
|
|
* @property string $logs
|
2021-12-24 09:49:34 +00:00
|
|
|
* @property User $user
|
2021-12-21 16:25:03 +00:00
|
|
|
* @property int $created_by
|
|
|
|
* @property int $updated_by
|
2021-12-24 17:55:56 +00:00
|
|
|
* @property Time|null $job_started_at
|
|
|
|
* @property Time|null $job_ended_at
|
2021-12-21 16:25:03 +00:00
|
|
|
*/
|
|
|
|
class BaseClip extends Entity
|
|
|
|
{
|
2021-12-23 16:56:06 +00:00
|
|
|
/**
|
2021-12-24 17:55:56 +00:00
|
|
|
* @var Video|Audio|null
|
2021-12-23 16:56:06 +00:00
|
|
|
*/
|
2021-12-24 17:55:56 +00:00
|
|
|
protected $media;
|
|
|
|
|
|
|
|
protected ?int $job_duration = null;
|
|
|
|
|
|
|
|
protected ?float $end_time = null;
|
|
|
|
|
|
|
|
/**
|
2023-08-26 13:03:01 +00:00
|
|
|
* @var array<int, string>
|
|
|
|
* @phpstan-var list<string>
|
2021-12-24 17:55:56 +00:00
|
|
|
*/
|
|
|
|
protected $dates = ['created_at', 'updated_at', 'job_started_at', 'job_ended_at'];
|
2021-12-23 16:56:06 +00:00
|
|
|
|
2021-12-21 16:25:03 +00:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'id' => 'integer',
|
2021-12-21 16:25:03 +00:00
|
|
|
'podcast_id' => 'integer',
|
|
|
|
'episode_id' => 'integer',
|
2023-06-12 14:47:38 +00:00
|
|
|
'title' => 'string',
|
2021-12-21 16:25:03 +00:00
|
|
|
'start_time' => 'double',
|
2023-06-12 14:47:38 +00:00
|
|
|
'duration' => 'double',
|
|
|
|
'type' => 'string',
|
|
|
|
'media_id' => '?integer',
|
|
|
|
'metadata' => '?json-array',
|
|
|
|
'status' => 'string',
|
|
|
|
'logs' => 'string',
|
2021-12-21 16:25:03 +00:00
|
|
|
'created_by' => 'integer',
|
|
|
|
'updated_by' => 'integer',
|
|
|
|
];
|
|
|
|
|
2021-12-24 09:49:34 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, mixed>|null $data
|
|
|
|
*/
|
|
|
|
public function __construct(array $data = null)
|
2021-12-21 16:25:03 +00:00
|
|
|
{
|
|
|
|
parent::__construct($data);
|
2021-12-24 17:55:56 +00:00
|
|
|
}
|
2021-12-21 16:25:03 +00:00
|
|
|
|
2021-12-24 17:55:56 +00:00
|
|
|
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) {
|
2021-12-21 16:25:03 +00:00
|
|
|
$this->end_time = $this->start_time + $this->duration;
|
|
|
|
}
|
2021-12-24 17:55:56 +00:00
|
|
|
|
|
|
|
return $this->end_time;
|
2021-12-21 16:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getPodcast(): ?Podcast
|
|
|
|
{
|
|
|
|
return (new PodcastModel())->getPodcastById($this->podcast_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEpisode(): ?Episode
|
|
|
|
{
|
|
|
|
return (new EpisodeModel())->getEpisodeById($this->episode_id);
|
|
|
|
}
|
|
|
|
|
2021-12-24 09:49:34 +00:00
|
|
|
public function getUser(): ?User
|
|
|
|
{
|
2023-06-13 16:05:02 +00:00
|
|
|
/** @var ?User */
|
2021-12-24 09:49:34 +00:00
|
|
|
return (new UserModel())->find($this->created_by);
|
|
|
|
}
|
|
|
|
|
2023-03-16 13:00:05 +00:00
|
|
|
public function setMedia(File $file, string $fileKey): static
|
2021-12-21 16:25:03 +00:00
|
|
|
{
|
2021-12-24 09:49:34 +00:00
|
|
|
if ($this->media_id !== null) {
|
2021-12-21 16:25:03 +00:00
|
|
|
$this->getMedia()
|
|
|
|
->setFile($file);
|
|
|
|
$this->getMedia()
|
2023-06-21 16:17:11 +00:00
|
|
|
->updated_by = $this->attributes['updated_by'];
|
2021-12-21 16:25:03 +00:00
|
|
|
(new MediaModel('audio'))->updateMedia($this->getMedia());
|
|
|
|
} else {
|
|
|
|
$media = new Audio([
|
2023-06-12 14:47:38 +00:00
|
|
|
'file_key' => $fileKey,
|
2021-12-21 16:25:03 +00:00
|
|
|
'language_code' => $this->getPodcast()
|
2024-12-17 15:06:08 +00:00
|
|
|
->language_code,
|
2023-06-21 16:17:11 +00:00
|
|
|
'uploaded_by' => $this->attributes['updated_by'],
|
|
|
|
'updated_by' => $this->attributes['updated_by'],
|
2021-12-21 16:25:03 +00:00
|
|
|
]);
|
|
|
|
$media->setFile($file);
|
|
|
|
|
|
|
|
$this->attributes['media_id'] = (new MediaModel())->saveMedia($media);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-12-23 16:56:06 +00:00
|
|
|
public function getMedia(): Audio | Video | null
|
2021-12-21 16:25:03 +00:00
|
|
|
{
|
|
|
|
if ($this->media_id !== null && $this->media === null) {
|
|
|
|
$this->media = (new MediaModel($this->type))->getMediaById($this->media_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->media;
|
|
|
|
}
|
|
|
|
}
|