vid-queue/app/Models/Input.php

56 lines
1.3 KiB
PHP
Raw Normal View History

2022-03-05 19:51:04 -06:00
<?php
namespace App\Models;
use App\Exceptions\Quit;
2022-03-25 17:02:09 -05:00
class Input extends Model
2022-03-05 19:51:04 -06:00
{
protected ?array $files = null;
protected ?string $url = null;
protected ?string $destination = null;
protected ?string $title = null;
protected bool $low = false;
protected bool $sync = false;
2022-03-05 19:51:04 -06:00
2022-03-25 17:02:09 -05:00
public function assertValid(): self
2022-03-05 19:51:04 -06:00
{
2022-03-25 17:02:09 -05:00
// @todo probably should use a validator for this
2022-03-05 19:51:04 -06:00
throw_if(
empty($this->files) && empty($this->url),
Quit::class,
'Must have either a valid file or URL'
);
2022-03-25 17:02:09 -05:00
return $this;
2022-03-05 19:51:04 -06:00
}
public function hasURL(): bool
{
return !empty($this->url);
}
2022-03-25 17:02:09 -05:00
public function setInput(?array $files): void
{
$this->setFiles($files);
}
public function setFiles(?array $files): void
{
$this->files = array_map(function (string $file) {
if (!file_exists($file)) {
throw new Quit("$file is not a valid file");
}
return realpath($file);
}, $files ?? []);
}
public function setDestination(?string $dir): void
{
throw_if(!is_dir($dir), Quit::class, "$dir is not a valid directory");
$this->destination = $dir;
// @todo get title from .nfo file, if available
}
2022-03-05 19:51:04 -06:00
}