Newer
Older

Yassine Doghri
committed
<?php
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/

Yassine Doghri
committed
namespace App\Entities;

Yassine Doghri
committed

Yassine Doghri
committed
use RuntimeException;

Yassine Doghri
committed
use App\Models\PodcastModel;
use Myth\Auth\Entities\User as MythAuthUser;

Yassine Doghri
committed
/**
* @property int $id
* @property string $username
* @property string $email
* @property string $password
* @property bool $active
* @property bool $force_pass_reset
* @property int|null $podcast_id
* @property string|null $podcast_role
*
* @property Podcast[] $podcasts All podcasts the user is contributing to
*/
class User extends MythAuthUser

Yassine Doghri
committed
{
/**

Yassine Doghri
committed
* @var Podcast[]

Yassine Doghri
committed
*/
protected $podcasts = [];
/**
* Array of field names and the type of value to cast them as
* when they are accessed.

Yassine Doghri
committed
*
* @var array<string, string>

Yassine Doghri
committed
*/
protected $casts = [
'id' => 'integer',

Yassine Doghri
committed
'active' => 'boolean',
'force_pass_reset' => 'boolean',
'podcast_role' => '?string',

Yassine Doghri
committed
];
/**
* Returns the podcasts the user is contributing to
*

Yassine Doghri
committed
* @return Podcast[]

Yassine Doghri
committed
*/

Yassine Doghri
committed
public function getPodcasts(): array

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

Yassine Doghri
committed
throw new RuntimeException(
'Users must be created before getting podcasts.',

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

Yassine Doghri
committed
$this->podcasts = (new PodcastModel())->getUserPodcasts($this->id);
}
return $this->podcasts;
}
}