Skip to content
Snippets Groups Projects
Person.php 3.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * @copyright  2021 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;
    use App\Models\MediaModel;
    
    use CodeIgniter\HTTP\Files\UploadedFile;
    
    /**
     * @property int $id
     * @property string $full_name
     * @property string $unique_name
     * @property string|null $information_url
    
     * @property int $created_by
     * @property int $updated_by
    
    class Person extends Entity
    {
    
        protected $casts = [
            'id' => 'integer',
            'full_name' => 'string',
            'unique_name' => 'string',
            'information_url' => '?string',
    
            'podcast_id' => '?integer',
            'episode_id' => '?integer',
    
            'created_by' => 'integer',
            'updated_by' => 'integer',
        ];
    
        /**
    
         * Saves the person avatar in `public/media/persons/`
    
        public function setAvatar(UploadedFile | File $file = null): static
    
            if ($file === null || ($file instanceof UploadedFile && ! $file->isValid())) {
    
            if (array_key_exists('avatar_id', $this->attributes) && $this->attributes['avatar_id'] !== null) {
    
                $this->getAvatar()
                    ->setFile($file);
                $this->getAvatar()
                    ->updated_by = (int) user_id();
                (new MediaModel('image'))->updateMedia($this->getAvatar());
            } else {
    
                    'file_name' => $this->attributes['unique_name'],
                    'file_directory' => 'persons',
                    'sizes' => config('Images')
                        ->personAvatarSizes,
                    'uploaded_by' => user_id(),
                    'updated_by' => user_id(),
                ]);
    
                $this->attributes['avatar_id'] = (new MediaModel('image'))->saveMedia($avatar);
    
            if ($this->attributes['avatar_id'] === null) {
                helper('media');
                return new Image([
    
                    'file_path' => config('Images')
                        ->avatarDefaultPath,
                    'file_mimetype' => config('Images')
                        ->avatarDefaultMimeType,
                    'file_size' => 0,
                    'file_metadata' => [
                        'sizes' => config('Images')
                            ->personAvatarSizes,
                    ],
    
                ]);
            }
    
            if ($this->avatar === null) {
                $this->avatar = (new MediaModel('image'))->getMediaById($this->avatar_id);
    
            if ($this->attributes['podcast_id'] === null) {
    
                throw new RuntimeException('Person must have a podcast_id before getting roles.');
    
                $this->roles = (new PersonModel())->getPersonRoles(
                    $this->id,
    
                    (int) $this->attributes['podcast_id'],
                    array_key_exists('episode_id', $this->attributes) ? (int) $this->attributes['episode_id'] : null