♻️ Move validation to Input class
This commit is contained in:
parent
b1f1f77b17
commit
3246004e58
5 changed files with 79 additions and 37 deletions
|
|
@ -4,6 +4,8 @@ namespace App\Commands;
|
||||||
|
|
||||||
use App\Exceptions\Quit;
|
use App\Exceptions\Quit;
|
||||||
use App\Models\Input;
|
use App\Models\Input;
|
||||||
|
use App\Services\ProcessInput;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
class GetShow extends Command
|
class GetShow extends Command
|
||||||
{
|
{
|
||||||
|
|
@ -20,46 +22,17 @@ class GetShow extends Command
|
||||||
|
|
||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
$this->line(json_encode($this->options()));
|
$this->line('Options: ' . json_encode($this->options()), verbosity: OutputInterface::VERBOSITY_DEBUG);
|
||||||
$dest = $this->getDestination();
|
|
||||||
$input = $this->getInput();
|
$input = $this->getInput();
|
||||||
|
$this->line('Input: ' . json_encode($input), verbosity: OutputInterface::VERBOSITY_DEBUG);
|
||||||
if ($this->confirm('Do you wish to continue?', true)) {
|
$job = app()->make(ProcessInput::class)->getJob($input);
|
||||||
$this->line(json_encode([$input, $dest]));
|
$this->line('Job: ' . json_encode($job), verbosity: OutputInterface::VERBOSITY_DEBUG);
|
||||||
}
|
|
||||||
|
|
||||||
return static::SUCCESS;
|
return static::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getInput(): Input
|
protected function getInput(): Input
|
||||||
{
|
{
|
||||||
$ret = new Input;
|
return (new Input($this->options()))->assertValid();
|
||||||
$input = $this->option('input');
|
|
||||||
|
|
||||||
$ret->files = array_map(function (string $file) {
|
|
||||||
if (!file_exists($file)) {
|
|
||||||
throw new Quit("$file is not a valid file");
|
|
||||||
}
|
|
||||||
|
|
||||||
return realpath($file);
|
|
||||||
}, $input);
|
|
||||||
|
|
||||||
// @todo Get additional files
|
|
||||||
|
|
||||||
$ret->url = $this->option('url');
|
|
||||||
$this->line(json_encode($ret));
|
|
||||||
|
|
||||||
$ret->assertValid();
|
|
||||||
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function getDestination(): string
|
|
||||||
{
|
|
||||||
$dir = $this->option('destination');
|
|
||||||
throw_if(!is_dir($dir), Quit::class, "$dir is not a valid directory");
|
|
||||||
// @todo find show title
|
|
||||||
|
|
||||||
return realpath($dir);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,22 +4,52 @@ namespace App\Models;
|
||||||
|
|
||||||
use App\Exceptions\Quit;
|
use App\Exceptions\Quit;
|
||||||
|
|
||||||
class Input
|
class Input extends Model
|
||||||
{
|
{
|
||||||
public ?array $files;
|
public ?array $files;
|
||||||
public ?string $url;
|
public ?string $url;
|
||||||
|
public ?string $destination;
|
||||||
|
public ?string $title;
|
||||||
|
public bool $low;
|
||||||
|
public bool $sync;
|
||||||
|
|
||||||
public function assertValid(): void
|
public function assertValid(): self
|
||||||
{
|
{
|
||||||
|
// @todo probably should use a validator for this
|
||||||
throw_if(
|
throw_if(
|
||||||
empty($this->files) && empty($this->url),
|
empty($this->files) && empty($this->url),
|
||||||
Quit::class,
|
Quit::class,
|
||||||
'Must have either a valid file or URL'
|
'Must have either a valid file or URL'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasURL(): bool
|
public function hasURL(): bool
|
||||||
{
|
{
|
||||||
return !empty($this->url);
|
return !empty($this->url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
25
app/Models/Model.php
Normal file
25
app/Models/Model.php
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
abstract class Model
|
||||||
|
{
|
||||||
|
public function __construct(array $data = [])
|
||||||
|
{
|
||||||
|
foreach ($data as $k => $v) {
|
||||||
|
$this->__set($k, $v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __set(string $key, $value)
|
||||||
|
{
|
||||||
|
$setter = 'set' . Str::studly($key);
|
||||||
|
if (method_exists($this, $setter)) {
|
||||||
|
$this->$setter($value);
|
||||||
|
} elseif (property_exists($this, $key)) {
|
||||||
|
$this->$key = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Services\ProcessInput;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
|
|
@ -23,6 +24,6 @@ class AppServiceProvider extends ServiceProvider
|
||||||
*/
|
*/
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
//
|
$this->app->instance(ProcessInput::class, new ProcessInput());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
app/Services/ProcessInput.php
Normal file
13
app/Services/ProcessInput.php
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Models\Input;
|
||||||
|
|
||||||
|
class ProcessInput
|
||||||
|
{
|
||||||
|
public function getJob(Input $input)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue