Newer
Older

Yassine Doghri
committed
declare(strict_types=1);
* Class SoundbiteModel Model for podcasts_soundbites table in database
*
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Models;

Yassine Doghri
committed
use App\Entities\Clip\BaseClip;
use App\Entities\Clip\Soundbite;
use App\Entities\Clip\VideoClip;
use CodeIgniter\Database\BaseResult;

Yassine Doghri
committed
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Model;

Yassine Doghri
committed
use CodeIgniter\Validation\ValidationInterface;

Yassine Doghri
committed
class ClipModel extends Model

Yassine Doghri
committed
/**
* @var string
*/
protected $table = 'clips';

Yassine Doghri
committed
/**
* @var string
*/
protected $primaryKey = 'id';

Yassine Doghri
committed
/**
* @var string[]
*/
protected $allowedFields = [

Yassine Doghri
committed
'id',
'podcast_id',
'episode_id',

Yassine Doghri
committed
'title',
'start_time',
'duration',

Yassine Doghri
committed
'type',
'media_id',

Yassine Doghri
committed
'metadata',

Yassine Doghri
committed
'status',
'logs',
'created_by',
'updated_by',

Yassine Doghri
committed
'job_started_at',
'job_ended_at',

Yassine Doghri
committed
/**
* @var string
*/

Yassine Doghri
committed
protected $returnType = BaseClip::class;

Yassine Doghri
committed
/**
* @var bool
*/
protected $useSoftDeletes = false;

Yassine Doghri
committed
/**
* @var bool
*/
protected $useTimestamps = true;

Yassine Doghri
committed
public function __construct(
protected string $type = 'audio',
ConnectionInterface &$db = null,
ValidationInterface $validation = null
) {
// @phpstan-ignore-next-line
switch ($type) {
case 'audio':
$this->returnType = Soundbite::class;
break;
case 'video':
$this->returnType = VideoClip::class;
break;
default:
// do nothing, keep default class
break;
}
parent::__construct($db, $validation);
public function getVideoClipById(int $videoClipId): ?VideoClip
{
$cacheName = "video-clip#{$videoClipId}";
if (! ($found = cache($cacheName))) {
$clip = $this->find($videoClipId);
if ($clip === null) {
return null;
}
// @phpstan-ignore-next-line
$found = new VideoClip($clip->toArray());
cache()
->save($cacheName, $found, DECADE);
}
return $found;
}

Yassine Doghri
committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* 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;
}

Yassine Doghri
committed
/**
* Gets scheduled video clips for an episode
*
* @return VideoClip[]
*/
public function getScheduledVideoClips(): array
{
$found = $this->where([
'type' => 'video',
'status' => 'queued',
])
->orderBy('created_at')
->findAll();
foreach ($found as $key => $videoClip) {
$found[$key] = new VideoClip($videoClip->toArray());
}
return $found;
}

Yassine Doghri
committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
public function getSoundbiteById(int $soundbiteId): ?Soundbite
{
$cacheName = "soundbite#{$soundbiteId}";
if (! ($found = cache($cacheName))) {
$clip = $this->find($soundbiteId);
if ($clip === null) {
return null;
}
// @phpstan-ignore-next-line
$found = new Soundbite($clip->toArray());
cache()
->save($cacheName, $found, DECADE);
}
return $found;
}
/**
* Gets all clips for an episode
*
* @return Soundbite[]
*/
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;
}

Yassine Doghri
committed
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,
]);
}

Yassine Doghri
committed
// cache()
// ->deleteMatching("page_podcast#{$clip->podcast_id}_episode#{$clip->episode_id}_*");