Newer
Older

Yassine Doghri
committed
<?php
declare(strict_types=1);
/**
* @copyright 2020 Ad Aures

Yassine Doghri
committed
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Entities\Clip;

Yassine Doghri
committed
use CodeIgniter\Files\File;
use Modules\Media\Entities\Video;
use Modules\Media\Models\MediaModel;

Yassine Doghri
committed

Yassine Doghri
committed
/**

Yassine Doghri
committed
* @property array{name:string,preview:string} $theme

Yassine Doghri
committed
* @property string $format

Yassine Doghri
committed
*/
class VideoClip extends BaseClip
{
protected string $type = 'video';

Yassine Doghri
committed
/**
* @param array<string, mixed>|null $data
*/
public function __construct(?array $data = null)

Yassine Doghri
committed
{
parent::__construct($data);

Yassine Doghri
committed
if ($this->metadata !== null && $this->metadata !== []) {

Yassine Doghri
committed
$this->theme = $this->metadata['theme'];
$this->format = $this->metadata['format'];
}
}

Yassine Doghri
committed
/**

Yassine Doghri
committed
* @param array{name:string,preview:string} $theme

Yassine Doghri
committed
*/
public function setTheme(array $theme): self
{
// TODO: change?
$this->attributes['metadata'] = json_decode($this->attributes['metadata'] ?? '[]', true);
$this->attributes['theme'] = $theme;
$this->attributes['metadata']['theme'] = $theme;
$this->attributes['metadata'] = json_encode($this->attributes['metadata']);
return $this;
}
public function setFormat(string $format): self
{
$this->attributes['metadata'] = json_decode((string) $this->attributes['metadata'], true);

Yassine Doghri
committed
$this->attributes['format'] = $format;
$this->attributes['metadata']['format'] = $format;
$this->attributes['metadata'] = json_encode($this->attributes['metadata']);
return $this;
}
public function setMedia(File $file, string $fileKey): static

Yassine Doghri
committed
{

Yassine Doghri
committed
if ($this->attributes['media_id'] !== null) {
// media is already set, do nothing
return $this;
}

Yassine Doghri
committed
$video = new Video([
'file_key' => $fileKey,

Yassine Doghri
committed
'language_code' => $this->getPodcast()

Yassine Doghri
committed
->language_code,

Yassine Doghri
committed
'uploaded_by' => $this->attributes['created_by'],
'updated_by' => $this->attributes['created_by'],

Yassine Doghri
committed
]);
$video->setFile($file);
$this->attributes['media_id'] = (new MediaModel('video'))->saveMedia($video);

Yassine Doghri
committed
return $this;
}