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 App\Entities\Media\Video;
use App\Models\MediaModel;
use CodeIgniter\Files\File;

Yassine Doghri
committed
/**

Yassine Doghri
committed
* @property array $theme
* @property string $format

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

Yassine Doghri
committed
/**
* @param array<string, mixed>|null $data
*/

Yassine Doghri
committed
public function __construct(array $data = null)
{
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @param array<string, string> $theme
*/
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($this->attributes['metadata'], true);
$this->attributes['format'] = $format;
$this->attributes['metadata']['format'] = $format;
$this->attributes['metadata'] = json_encode($this->attributes['metadata']);
return $this;
}
public function setMedia(string $filePath = null): static
{
if ($filePath === null) {
return $this;
}

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

Yassine Doghri
committed
$video = new Video([
'file_path' => $filePath,
'language_code' => $this->getPodcast()
->language_code,
'uploaded_by' => $this->attributes['created_by'],
'updated_by' => $this->attributes['created_by'],
]);
$video->setFile($file);
$this->attributes['media_id'] = (new MediaModel())->saveMedia($video);
return $this;
}