From 3246004e58d185d05279d49a7916345af97364b9 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 25 Mar 2022 17:02:09 -0500 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Move=20validation=20to=20I?= =?UTF-8?q?nput=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Commands/GetShow.php | 41 +++++----------------------- app/Models/Input.php | 34 +++++++++++++++++++++-- app/Models/Model.php | 25 +++++++++++++++++ app/Providers/AppServiceProvider.php | 3 +- app/Services/ProcessInput.php | 13 +++++++++ 5 files changed, 79 insertions(+), 37 deletions(-) create mode 100644 app/Models/Model.php create mode 100644 app/Services/ProcessInput.php diff --git a/app/Commands/GetShow.php b/app/Commands/GetShow.php index 3d5f1ac..399715e 100644 --- a/app/Commands/GetShow.php +++ b/app/Commands/GetShow.php @@ -4,6 +4,8 @@ namespace App\Commands; use App\Exceptions\Quit; use App\Models\Input; +use App\Services\ProcessInput; +use Symfony\Component\Console\Output\OutputInterface; class GetShow extends Command { @@ -20,46 +22,17 @@ class GetShow extends Command public function handle(): int { - $this->line(json_encode($this->options())); - $dest = $this->getDestination(); + $this->line('Options: ' . json_encode($this->options()), verbosity: OutputInterface::VERBOSITY_DEBUG); $input = $this->getInput(); - - if ($this->confirm('Do you wish to continue?', true)) { - $this->line(json_encode([$input, $dest])); - } + $this->line('Input: ' . json_encode($input), verbosity: OutputInterface::VERBOSITY_DEBUG); + $job = app()->make(ProcessInput::class)->getJob($input); + $this->line('Job: ' . json_encode($job), verbosity: OutputInterface::VERBOSITY_DEBUG); return static::SUCCESS; } protected function getInput(): Input { - $ret = new Input; - $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); + return (new Input($this->options()))->assertValid(); } } diff --git a/app/Models/Input.php b/app/Models/Input.php index 1f9353f..71ec18b 100644 --- a/app/Models/Input.php +++ b/app/Models/Input.php @@ -4,22 +4,52 @@ namespace App\Models; use App\Exceptions\Quit; -class Input +class Input extends Model { public ?array $files; 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( empty($this->files) && empty($this->url), Quit::class, 'Must have either a valid file or URL' ); + + return $this; } public function hasURL(): bool { 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 + } } diff --git a/app/Models/Model.php b/app/Models/Model.php new file mode 100644 index 0000000..75f19c3 --- /dev/null +++ b/app/Models/Model.php @@ -0,0 +1,25 @@ + $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; + } + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 35471f6..5710047 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Services\ProcessInput; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider @@ -23,6 +24,6 @@ class AppServiceProvider extends ServiceProvider */ public function register() { - // + $this->app->instance(ProcessInput::class, new ProcessInput()); } } diff --git a/app/Services/ProcessInput.php b/app/Services/ProcessInput.php new file mode 100644 index 0000000..470b3ac --- /dev/null +++ b/app/Services/ProcessInput.php @@ -0,0 +1,13 @@ +