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;
use App\Entities\Clip;
use CodeIgniter\Database\BaseResult;
use CodeIgniter\Model;
class ClipsModel 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 = [
'podcast_id',
'episode_id',
'label',
'type',
'start_time',
'duration',
'created_by',
'updated_by',
];

Yassine Doghri
committed
/**
* @var string
*/
protected $returnType = Clip::class;

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

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

Yassine Doghri
committed
/**
* @var string[]
*/
protected $afterInsert = ['clearCache'];

Yassine Doghri
committed
/**
* @var string[]
*/
protected $afterUpdate = ['clearCache'];

Yassine Doghri
committed
/**
* @var string[]
*/
protected $beforeDelete = ['clearCache'];
public function deleteClip(int $podcastId, int $episodeId, int $clipId): BaseResult | bool
{
return $this->delete([
'podcast_id' => $podcastId,
'episode_id' => $episodeId,
'id' => $clipId,
]);
}
/**
* Gets all clips for an episode
* @return Clip[]
public function getEpisodeClips(int $podcastId, int $episodeId): array
$cacheName = "podcast#{$podcastId}_episode#{$episodeId}_clips";
if (! ($found = cache($cacheName))) {
$found = $this->where([
'episode_id' => $episodeId,
'podcast_id' => $podcastId,
])
->orderBy('start_time')
->findAll();
cache()
->save($cacheName, $found, DECADE);
}
return $found;
}

Yassine Doghri
committed
/**
* @param array<string, array<string|int, mixed>> $data

Yassine Doghri
committed
* @return array<string, array<string|int, mixed>>
*/
public function clearCache(array $data): array
{
$episode = (new EpisodeModel())->find(
isset($data['data'])
? $data['data']['episode_id']
: $data['id']['episode_id'],
cache()
->delete("podcast#{$episode->podcast_id}_episode#{$episode->id}_clips");
// delete cache for rss feed
cache()
->deleteMatching("podcast#{$episode->podcast_id}_feed*");
cache()

Yassine Doghri
committed
->deleteMatching("page_podcast#{$episode->podcast_id}_episode#{$episode->id}_*");
return $data;
}
}