♻️ Move validation to Input class

This commit is contained in:
Dan Jones 2022-03-25 17:02:09 -05:00
commit 3246004e58
5 changed files with 79 additions and 37 deletions

View file

@ -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();
}
}