Newer
Older

Yassine Doghri
committed

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

Yassine Doghri
committed

Yassine Doghri
committed
use App\Entities\Platform;
use CodeIgniter\Model;
class PlatformModel extends Model
{

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

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

Yassine Doghri
committed
/**
* @var string[]
*/
protected $allowedFields = ['slug', 'type', 'label', 'home_url', 'submit_url'];

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

Yassine Doghri
committed
/**
* @var bool
*/

Yassine Doghri
committed
/**
* @var bool
*/
protected $useTimestamps = false;
/**
* @return Platform[]
*/
public function getPlatforms(): array
if (! ($found = cache('platforms'))) {
$baseUrl = rtrim(config('app')->baseURL, '/');
$found = $this->select(

Yassine Doghri
committed
"*, CONCAT('{$baseUrl}/assets/images/platforms/',`type`,'/',`slug`,'.svg') as icon",
)->findAll();
cache()
->save('platforms', $found, DECADE);
}
return $found;
}
public function getPlatform(string $slug): ?Platform
{

Yassine Doghri
committed
$cacheName = "platform-{$slug}";
if (! ($found = cache($cacheName))) {
$found = $this->where('slug', $slug)
->first();
cache()
->save($cacheName, $found, DECADE);
}
return $found;
}
public function createPlatform(
string $slug,
string $type,
string $label,
string $homeUrl,
string $submitUrl = null
): int | false {
$data = [
'slug' => $slug,
'type' => $type,
'label' => $label,
'home_url' => $homeUrl,
'submit_url' => $submitUrl,
];
return $this->insert($data, false);
}
* @return Platform[]
*/
public function getPlatformsWithLinks(int $podcastId, string $platformType): array
{
if (
! ($found = cache("podcast#{$podcastId}_platforms_{$platformType}_withLinks"))
) {
$found = $this->select(
'platforms.*, podcasts_platforms.link_url, podcasts_platforms.link_content, podcasts_platforms.is_visible, podcasts_platforms.is_on_embed',
->join(
'podcasts_platforms',

Yassine Doghri
committed
"podcasts_platforms.platform_slug = platforms.slug AND podcasts_platforms.podcast_id = {$podcastId}",

Yassine Doghri
committed
'left',
)
->where('platforms.type', $platformType)
->findAll();
cache()

Yassine Doghri
committed
->save("podcast#{$podcastId}_platforms_{$platformType}_withLinks", $found, DECADE);
}
return $found;
}
/**
* @return Platform[]
*/
public function getPodcastPlatforms(int $podcastId, string $platformType): array
{
$cacheName = "podcast#{$podcastId}_platforms_{$platformType}";
if (! ($found = cache($cacheName))) {
$found = $this->select(
'platforms.*, podcasts_platforms.link_url, podcasts_platforms.link_content, podcasts_platforms.is_visible, podcasts_platforms.is_on_embed',
)
->join('podcasts_platforms', 'podcasts_platforms.platform_slug = platforms.slug')
->where('podcasts_platforms.podcast_id', $podcastId)
->where('platforms.type', $platformType)
->findAll();
cache()
->save($cacheName, $found, DECADE);
}
return $found;

Yassine Doghri
committed
/**
* @param mixed[] $podcastsPlatformsData

Yassine Doghri
committed
*/
public function savePodcastPlatforms(
int $podcastId,
string $platformType,
array $podcastsPlatformsData
): int | false {

Yassine Doghri
committed
$this->clearCache($podcastId);
$podcastsPlatformsTable = $this->db->prefixTable('podcasts_platforms');
$platformsTable = $this->db->prefixTable('platforms');
$deleteJoinQuery = <<<CODE_SAMPLE

Yassine Doghri
committed
DELETE {$podcastsPlatformsTable}
FROM {$podcastsPlatformsTable}
INNER JOIN {$platformsTable} ON {$platformsTable}.slug = {$podcastsPlatformsTable}.platform_slug

Yassine Doghri
committed
WHERE `podcast_id` = ? AND `type` = ?
CODE_SAMPLE;
$this->db->query($deleteJoinQuery, [$podcastId, $platformType]);
->table('podcasts_platforms')
->insertBatch($podcastsPlatformsData);

Yassine Doghri
committed
/**
* @param mixed[] $podcastsPlatformsData

Yassine Doghri
committed
*/
public function createPodcastPlatforms(int $podcastId, array $podcastsPlatformsData): int | false
$this->clearCache($podcastId);
return $this->db
->table('podcasts_platforms')
->insertBatch($podcastsPlatformsData);
public function removePodcastPlatform(int $podcastId, string $platformSlug): bool | string

Yassine Doghri
committed
$this->clearCache($podcastId);
return $this->db->table('podcasts_platforms')
->delete([
'podcast_id' => $podcastId,
'platform_slug' => $platformSlug,
]);

Yassine Doghri
committed
public function clearCache(int $podcastId): void

Yassine Doghri
committed
{
cache()->deleteMatching("podcast#{$podcastId}_platforms_*");

Yassine Doghri
committed
// delete localized podcast page cache
cache()
->deleteMatching("page_podcast#{$podcastId}*");

Yassine Doghri
committed
}