Newer
Older

Yassine Doghri
committed

Yassine Doghri
committed
declare(strict_types=1);
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Entities;
use App\Entities\Media\Image;

Yassine Doghri
committed
use App\Libraries\SimpleRSSElement;
use App\Models\ActorModel;
use App\Models\CategoryModel;
use App\Models\EpisodeModel;
use App\Models\MediaModel;
use App\Models\PersonModel;
use App\Models\PlatformModel;

Yassine Doghri
committed
use App\Models\UserModel;
use CodeIgniter\Entity\Entity;

Yassine Doghri
committed
use CodeIgniter\Files\File;
use CodeIgniter\HTTP\Files\UploadedFile;
use CodeIgniter\I18n\Time;

Yassine Doghri
committed
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\Autolink\AutolinkExtension;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\DisallowedRawHtml\DisallowedRawHtmlExtension;
use League\CommonMark\Extension\SmartPunct\SmartPunctExtension;
use League\CommonMark\MarkdownConverter;

Yassine Doghri
committed
use Modules\Auth\Entities\User;

Yassine Doghri
committed
use RuntimeException;
/**
* @property int $id
* @property string $guid
* @property int $actor_id
* @property Actor|null $actor
* @property string $handle
* @property string $link
* @property string $feed_url
* @property string $title

Yassine Doghri
committed
* @property string|null $description Holds text only description, striped of any markdown or html special characters
* @property string $description_markdown
* @property string $description_html
* @property int $cover_id

Yassine Doghri
committed
* @property Image $cover
* @property int|null $banner_id

Yassine Doghri
committed
* @property Image|null $banner
* @property string $language_code
* @property int $category_id
* @property Category|null $category
* @property int[] $other_categories_ids
* @property Category[] $other_categories
* @property string|null $parental_advisory
* @property string|null $publisher
* @property string $owner_name
* @property string $owner_email
* @property string $type
* @property string|null $copyright
* @property string|null $episode_description_footer_markdown
* @property string|null $episode_description_footer_html
* @property bool $is_blocked
* @property bool $is_completed
* @property bool $is_locked
* @property string|null $imported_feed_url
* @property string|null $new_feed_url

Yassine Doghri
committed
* @property Location|null $location
* @property string|null $location_name
* @property string|null $location_geo

Yassine Doghri
committed
* @property string|null $location_osm
* @property string|null $payment_pointer
* @property array|null $custom_rss
* @property string $custom_rss_string
* @property string|null $partner_id
* @property string|null $partner_link_url
* @property string|null $partner_image_url
* @property int $created_by
* @property int $updated_by
* @property Time $created_at;
* @property Time $updated_at;
* @property Time|null $deleted_at;
*
* @property Episode[] $episodes
* @property Person[] $persons
* @property User[] $contributors
* @property Platform[] $podcasting_platforms
* @property Platform[] $social_platforms
* @property Platform[] $funding_platforms
*/
class Podcast extends Entity
{
protected string $link;

Yassine Doghri
committed
protected ?Actor $actor = null;
protected ?Image $cover = null;

Yassine Doghri
committed
protected ?Image $banner = null;

Yassine Doghri
committed
protected ?string $description = null;

Yassine Doghri
committed
protected ?Category $category = null;
* @var Category[]|null
protected ?array $other_categories = null;
/**
* @var string[]|null
protected ?array $other_categories_ids = null;
* @var Episode[]|null
protected ?array $episodes = null;
* @var Person[]|null
protected ?array $persons = null;
* @var User[]|null
protected ?array $contributors = null;
* @var Platform[]|null
protected ?array $podcasting_platforms = null;
/**
* @var Platform[]|null
*/
protected ?array $social_platforms = null;
/**
* @var Platform[]|null
*/
protected ?array $funding_platforms = null;

Yassine Doghri
committed
protected ?Location $location = null;
protected string $custom_rss_string;

Benjamin Bellamy
committed
/**
* @var array<string, string>
*/
'actor_id' => 'integer',
'handle' => 'string',
'title' => 'string',
'description_markdown' => 'string',
'description_html' => 'string',
'cover_id' => 'int',
'banner_id' => '?int',
'language_code' => 'string',
'category_id' => 'integer',
'parental_advisory' => '?string',
'publisher' => '?string',
'owner_name' => 'string',
'owner_email' => 'string',
'episode_description_footer_markdown' => '?string',
'episode_description_footer_html' => '?string',
'is_blocked' => 'boolean',
'is_completed' => 'boolean',
'is_locked' => 'boolean',
'imported_feed_url' => '?string',
'new_feed_url' => '?string',
'location_name' => '?string',
'location_geo' => '?string',

Yassine Doghri
committed
'location_osm' => '?string',
'payment_pointer' => '?string',

Benjamin Bellamy
committed
'custom_rss' => '?json-array',
'partner_id' => '?string',
'partner_link_url' => '?string',
'partner_image_url' => '?string',

Benjamin Bellamy
committed
'created_by' => 'integer',
'updated_by' => 'integer',
/**
* @noRector ReturnTypeDeclarationRector
*/
public function getActor(): ?Actor
{
if ($this->actor_id === 0) {

Yassine Doghri
committed
throw new RuntimeException('Podcast must have an actor_id before getting actor.');
}
if ($this->actor === null) {
// @phpstan-ignore-next-line
$this->actor = model(ActorModel::class)
->getActorById($this->actor_id);
}
// @phpstan-ignore-next-line
return $this->actor;
}

Yassine Doghri
committed
public function setCover(UploadedFile | File $file = null): self

Yassine Doghri
committed
if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) {
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
return $this;
}
if (array_key_exists('cover_id', $this->attributes) && $this->attributes['cover_id'] !== null) {
$this->getCover()
->setFile($file);
$this->getCover()
->updated_by = (int) user_id();
(new MediaModel('image'))->updateMedia($this->getCover());
} else {
$cover = new Image([
'file_name' => 'cover',
'file_directory' => 'podcasts/' . $this->attributes['handle'],
'sizes' => config('Images')
->podcastCoverSizes,
'uploaded_by' => user_id(),
'updated_by' => user_id(),
]);
$cover->setFile($file);
$this->attributes['cover_id'] = (new MediaModel('image'))->saveMedia($cover);
}
return $this;
}

Yassine Doghri
committed
public function getCover(): Image
{
if (! $this->cover instanceof Image) {
$this->cover = (new MediaModel('image'))->getMediaById($this->cover_id);

Yassine Doghri
committed
}
return $this->cover;

Yassine Doghri
committed
public function setBanner(UploadedFile | File $file = null): self

Yassine Doghri
committed
if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) {
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
return $this;
}
if (array_key_exists('banner_id', $this->attributes) && $this->attributes['banner_id'] !== null) {
$this->getBanner()
->setFile($file);
$this->getBanner()
->updated_by = (int) user_id();
(new MediaModel('image'))->updateMedia($this->getBanner());
} else {
$banner = new Image([
'file_name' => 'banner',
'file_directory' => 'podcasts/' . $this->attributes['handle'],
'sizes' => config('Images')
->podcastBannerSizes,
'uploaded_by' => user_id(),
'updated_by' => user_id(),
]);
$banner->setFile($file);
$this->attributes['banner_id'] = (new MediaModel('image'))->saveMedia($banner);
}
return $this;
}

Yassine Doghri
committed
public function getBanner(): Image
if ($this->banner_id === null) {
$defaultBanner = config('Images')
->podcastBannerDefaultPaths[service('settings')->get('App.theme')] ?? config(
'Images'
)->podcastBannerDefaultPaths['default'];
return new Image([
'file_path' => $defaultBanner['path'],
'file_mimetype' => $defaultBanner['mimetype'],
'file_size' => 0,
'file_metadata' => [
'sizes' => config('Images')
->podcastBannerSizes,
],
]);
}
if (! $this->banner instanceof Image) {
$this->banner = (new MediaModel('image'))->getMediaById($this->banner_id);

Yassine Doghri
committed
}
return $this->banner;

Yassine Doghri
committed
public function getLink(): string
return url_to('podcast-activity', $this->attributes['handle']);

Yassine Doghri
committed
public function getFeedUrl(): string
return url_to('podcast_feed', $this->attributes['handle']);

Yassine Doghri
committed
/**
* Returns the podcast's episodes
*

Yassine Doghri
committed
* @return Episode[]

Yassine Doghri
committed
*/

Yassine Doghri
committed
public function getEpisodes(): array
if ($this->id === null) {

Yassine Doghri
committed
throw new RuntimeException('Podcast must be created before getting episodes.');

Yassine Doghri
committed
}
if ($this->episodes === null) {

Yassine Doghri
committed
$this->episodes = (new EpisodeModel())->getPodcastEpisodes($this->id, $this->type);

Yassine Doghri
committed
}

Yassine Doghri
committed
return $this->episodes;
/**
* Returns the podcast's persons
*
* @return Person[]

Yassine Doghri
committed
public function getPersons(): array
if ($this->id === null) {

Yassine Doghri
committed
throw new RuntimeException('Podcast must be created before getting persons.');
if ($this->persons === null) {
$this->persons = (new PersonModel())->getPodcastPersons($this->id);
return $this->persons;
/**
* Returns the podcast category entity
*/
public function getCategory(): ?Category
{
if ($this->id === null) {

Yassine Doghri
committed
throw new RuntimeException('Podcast must be created before getting category.');
}
if (! $this->category instanceof Category) {

Yassine Doghri
committed
$this->category = (new CategoryModel())->getCategoryById($this->category_id);
}
return $this->category;
}
/**
* Returns all podcast contributors
*

Yassine Doghri
committed
* @return User[]
*/

Yassine Doghri
committed
public function getContributors(): array
if ($this->id === null) {

Yassine Doghri
committed
throw new RuntimeException('Podcasts must be created before getting contributors.');

Yassine Doghri
committed
}
if ($this->contributors === null) {

Yassine Doghri
committed
$this->contributors = (new UserModel())->getPodcastContributors($this->id);

Yassine Doghri
committed
}
return $this->contributors;
public function setDescriptionMarkdown(string $descriptionMarkdown): static
{

Yassine Doghri
committed
$config = [
'html_input' => 'escape',

Yassine Doghri
committed
'allow_unsafe_links' => false,

Yassine Doghri
committed
];
$environment = new Environment($config);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new AutolinkExtension());
$environment->addExtension(new SmartPunctExtension());
$environment->addExtension(new DisallowedRawHtmlExtension());
$converter = new MarkdownConverter($environment);
$this->attributes['description_markdown'] = $descriptionMarkdown;

Yassine Doghri
committed
$this->attributes['description_html'] = $converter->convert($descriptionMarkdown);
return $this;
}
public function setEpisodeDescriptionFooterMarkdown(?string $episodeDescriptionFooterMarkdown = null): static
{
if ($episodeDescriptionFooterMarkdown === null || $episodeDescriptionFooterMarkdown === '') {
$this->attributes[
'episode_description_footer_markdown'
$this->attributes[
'episode_description_footer_html'
] = null;
return $this;

Yassine Doghri
committed
$config = [
'html_input' => 'escape',
'allow_unsafe_links' => false,

Yassine Doghri
committed
];
$environment = new Environment($config);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new AutolinkExtension());
$environment->addExtension(new SmartPunctExtension());
$environment->addExtension(new DisallowedRawHtmlExtension());
$converter = new MarkdownConverter($environment);
$this->attributes[
'episode_description_footer_markdown'
] = $episodeDescriptionFooterMarkdown;
$this->attributes[
'episode_description_footer_html'

Yassine Doghri
committed
] = $converter->convert($episodeDescriptionFooterMarkdown);
return $this;
}

Yassine Doghri
committed
public function getDescription(): string

Yassine Doghri
committed
if ($this->description === null) {
$this->description = trim(
(string) preg_replace('~\s+~', ' ', strip_tags($this->attributes['description_html'])),

Yassine Doghri
committed
);

Yassine Doghri
committed
return $this->description;
}
/**
* Returns the podcast's podcasting platform links
*

Yassine Doghri
committed
* @return Platform[]
*/

Yassine Doghri
committed
public function getPodcastingPlatforms(): array
{
if ($this->id === null) {

Yassine Doghri
committed
throw new RuntimeException('Podcast must be created before getting podcasting platform links.');
}
if ($this->podcasting_platforms === null) {

Yassine Doghri
committed
$this->podcasting_platforms = (new PlatformModel())->getPodcastPlatforms($this->id, 'podcasting');
}
return $this->podcasting_platforms;
}
/**
* Returns the podcast's social platform links
*

Yassine Doghri
committed
* @return Platform[]
*/

Yassine Doghri
committed
public function getSocialPlatforms(): array
{
if ($this->id === null) {

Yassine Doghri
committed
throw new RuntimeException('Podcast must be created before getting social platform links.');
}
if ($this->social_platforms === null) {

Yassine Doghri
committed
$this->social_platforms = (new PlatformModel())->getPodcastPlatforms($this->id, 'social');
}
return $this->social_platforms;
}
/**
* Returns the podcast's funding platform links
*

Yassine Doghri
committed
* @return Platform[]
*/

Yassine Doghri
committed
public function getFundingPlatforms(): array
{
if ($this->id === null) {

Yassine Doghri
committed
throw new RuntimeException('Podcast must be created before getting funding platform links.');
}
if ($this->funding_platforms === null) {

Yassine Doghri
committed
$this->funding_platforms = (new PlatformModel())->getPodcastPlatforms($this->id, 'funding');
}
return $this->funding_platforms;
}

Yassine Doghri
committed
* @return Category[]

Yassine Doghri
committed
public function getOtherCategories(): array
if ($this->id === null) {

Yassine Doghri
committed
throw new RuntimeException('Podcast must be created before getting other categories.');
}
if ($this->other_categories === null) {

Yassine Doghri
committed
$this->other_categories = (new CategoryModel())->getPodcastCategories($this->id);
}
return $this->other_categories;
}

Yassine Doghri
committed
/**
* @return int[]

Yassine Doghri
committed
*/
public function getOtherCategoriesIds(): array
if ($this->other_categories_ids === null) {

Yassine Doghri
committed
$this->other_categories_ids = array_column($this->getOtherCategories(), 'id');
}
return $this->other_categories_ids;
}
/**
* Saves the location name and fetches OpenStreetMap info
*/

Yassine Doghri
committed
public function setLocation(?Location $location = null): static

Yassine Doghri
committed
if ($location === null) {
$this->attributes['location_name'] = null;
$this->attributes['location_geo'] = null;

Yassine Doghri
committed
$this->attributes['location_osm'] = null;

Yassine Doghri
committed
return $this;
}
! isset($this->attributes['location_name']) ||

Yassine Doghri
committed
$this->attributes['location_name'] !== $location->name

Yassine Doghri
committed
$location->fetchOsmLocation();

Yassine Doghri
committed
$this->attributes['location_name'] = $location->name;
$this->attributes['location_geo'] = $location->geo;
$this->attributes['location_osm'] = $location->osm;

Yassine Doghri
committed

Benjamin Bellamy
committed
public function getLocation(): ?Location
{
if ($this->location_name === null) {
return null;
}
if ($this->location === null) {

Yassine Doghri
committed
$this->location = new Location($this->location_name, $this->location_geo, $this->location_osm);
}
return $this->location;
}

Benjamin Bellamy
committed
/**
* Get custom rss tag as XML String
*/
public function getCustomRssString(): string

Benjamin Bellamy
committed
{
if ($this->attributes['custom_rss'] === null) {

Benjamin Bellamy
committed
return '';
}

Yassine Doghri
committed
helper('rss');
$xmlNode = (new SimpleRSSElement(
'<?xml version="1.0" encoding="utf-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:podcast="https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"/>',
))->addChild('channel');
array_to_rss([
'elements' => $this->custom_rss,

Yassine Doghri
committed
], $xmlNode);

Yassine Doghri
committed
return str_replace(['<channel>', '</channel>'], '', $xmlNode->asXML());

Benjamin Bellamy
committed
}
/**
* Saves custom rss tag into json
*/
public function setCustomRssString(string $customRssString): static

Benjamin Bellamy
committed
{
if ($customRssString === '') {
$this->attributes['custom_rss'] = null;

Yassine Doghri
committed
return $this;
}

Benjamin Bellamy
committed
helper('rss');
$customRssArray = rss_to_array(
simplexml_load_string(
'<?xml version="1.0" encoding="utf-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:podcast="https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0"><channel>' .
$customRssString .
'</channel></rss>',
),

Benjamin Bellamy
committed
)['elements'][0];

Yassine Doghri
committed

Benjamin Bellamy
committed
if (array_key_exists('elements', $customRssArray)) {

Yassine Doghri
committed
$this->attributes['custom_rss'] = json_encode($customRssArray['elements']);

Benjamin Bellamy
committed
} else {
$this->attributes['custom_rss'] = null;
}

Yassine Doghri
committed
return $this;

Benjamin Bellamy
committed
}