From 984d537089f8feb1e7b826358e97312a3c0498b9 Mon Sep 17 00:00:00 2001 From: Oscar Cabrera Date: Thu, 20 Jun 2024 17:40:52 -0600 Subject: [PATCH] fix src/Trait/FailedValidationTrait.php --- README.md | 2 +- src/Handlers/Makers/MakeController.php | 2 +- .../Makers/MakeInterfaceRepository.php | 2 +- src/Handlers/Makers/MakeInterfaceServices.php | 2 +- src/Handlers/Makers/MakeRepository.php | 2 +- src/Handlers/Makers/MakeRequest.php | 2 +- src/Handlers/Makers/MakeService.php | 2 +- src/Handlers/Makers/MakeStructure.php | 26 ++++++++++++++----- src/Trait/FailedValidationTrait.php | 8 +++--- 9 files changed, 31 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 08b180a..171fc07 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,7 @@ This project provides a complete implementation of a model repository using the ### keywords: -> VitePress, Documentation, Markdown, Vite, Tutorials, API, GitHub Pages +> Laravel, RESTful API, Repository Patter, CRUD, API, Repositories, Services, File generation ## **Additional resources:** diff --git a/src/Handlers/Makers/MakeController.php b/src/Handlers/Makers/MakeController.php index e628db5..7d3d40f 100644 --- a/src/Handlers/Makers/MakeController.php +++ b/src/Handlers/Makers/MakeController.php @@ -52,6 +52,6 @@ public function make(string $name, Options $options): array $directory = app_path("Http/Controllers/$name"); $path = $this->getFilePath($directory, $name, $this->type); - return $this->createFromClassStub($path, $replace, $this->type, $options->force); + return $this->createFromClassStub($path, $replace, $this->type, $options); } } diff --git a/src/Handlers/Makers/MakeInterfaceRepository.php b/src/Handlers/Makers/MakeInterfaceRepository.php index 9d37603..243ca57 100644 --- a/src/Handlers/Makers/MakeInterfaceRepository.php +++ b/src/Handlers/Makers/MakeInterfaceRepository.php @@ -50,6 +50,6 @@ public function make(string $name, Options $options): array $directory = app_path("Contracts/Repositories/$name"); $path = $this->getFilePath($directory, 'I' . $name, 'Repository'); - return $this->createFromClassStub($path, $replace, $this->type, $options->force); + return $this->createFromClassStub($path, $replace, $this->type, $options); } } diff --git a/src/Handlers/Makers/MakeInterfaceServices.php b/src/Handlers/Makers/MakeInterfaceServices.php index 096f8ab..f8ed661 100644 --- a/src/Handlers/Makers/MakeInterfaceServices.php +++ b/src/Handlers/Makers/MakeInterfaceServices.php @@ -50,6 +50,6 @@ public function make(string $name, Options $options): array $directory = app_path("Contracts/Services/$name"); $path = $this->getFilePath($directory, 'I' . $name, 'Service'); - return $this->createFromClassStub($path, $replace, $this->type, $options->force); + return $this->createFromClassStub($path, $replace, $this->type, $options); } } diff --git a/src/Handlers/Makers/MakeRepository.php b/src/Handlers/Makers/MakeRepository.php index 0cf97f5..0e068b6 100644 --- a/src/Handlers/Makers/MakeRepository.php +++ b/src/Handlers/Makers/MakeRepository.php @@ -50,7 +50,7 @@ public function make(string $name, Options $options): array $directory = app_path("Repositories/$name"); $path = $this->getFilePath($directory, $name, $this->type); - return $this->createFromClassStub($path, $replace, $this->type, $options->force); + return $this->createFromClassStub($path, $replace, $this->type, $options); } /** diff --git a/src/Handlers/Makers/MakeRequest.php b/src/Handlers/Makers/MakeRequest.php index 3f5c14a..b497fb6 100644 --- a/src/Handlers/Makers/MakeRequest.php +++ b/src/Handlers/Makers/MakeRequest.php @@ -50,6 +50,6 @@ public function make(string $name, Options $options): array $directory = app_path("Http/Requests/$name"); $path = $this->getFilePath($directory, $name, $this->type); - return $this->createFromClassStub($path, $replace, $this->type, $options->force); + return $this->createFromClassStub($path, $replace, $this->type, $options); } } diff --git a/src/Handlers/Makers/MakeService.php b/src/Handlers/Makers/MakeService.php index 655baa8..8511b6c 100644 --- a/src/Handlers/Makers/MakeService.php +++ b/src/Handlers/Makers/MakeService.php @@ -54,7 +54,7 @@ public function make(string $name, Options $options): array $directory = app_path("Services/$name"); $path = $this->getFilePath($directory, $name, $this->type); - return $this->createFromClassStub($path, $replace, $this->type, $options->force); + return $this->createFromClassStub($path, $replace, $this->type, $options); } /** diff --git a/src/Handlers/Makers/MakeStructure.php b/src/Handlers/Makers/MakeStructure.php index 1a89b0d..949472c 100644 --- a/src/Handlers/Makers/MakeStructure.php +++ b/src/Handlers/Makers/MakeStructure.php @@ -3,6 +3,7 @@ namespace Oscabrera\ModelRepository\Handlers\Makers; use Illuminate\Support\Facades\File; +use Oscabrera\ModelRepository\Classes\Options; use Oscabrera\ModelRepository\Exception\Command\CreateStructureException; use Oscabrera\ModelRepository\Exception\Command\StubException; use Oscabrera\ModelRepository\Handlers\BindingServices; @@ -30,7 +31,7 @@ public function __construct( * @param string $classPath * @param array $replacements * @param string $type - * @param bool $force Determines if the class should be overwritten if it already exists. + * @param Options $options * @return array{type: string, path: string} * @throws StubException|CreateStructureException */ @@ -38,12 +39,9 @@ protected function createFromClassStub( string $classPath, array $replacements, string $type, - bool $force = false + Options $options ): array { - if ($this->file::exists($classPath) && !$force) { - throw new CreateStructureException('already exists', $type, $classPath); - } - + $this->validateClassExists($classPath, $type, $options); $contentStub = $this->getStubContent($type); $classContent = str_replace( array_keys($replacements), @@ -54,6 +52,22 @@ protected function createFromClassStub( return ['type' => $type, 'path' => $classPath]; } + /** + * Validate if the class exists. + * + * @param string $classPath The file path of the class. + * @param string $type The type of the class (e.g., interface, service). + * @param Options $options The options for creating the class. + * @return void + * @throws CreateStructureException If the class already exists and force option is false. + */ + protected function validateClassExists(string $classPath, string $type, Options $options): void + { + if ($this->file::exists($classPath) && !$options->force) { + throw new CreateStructureException('already exists', $type, $classPath); + } + } + /** * Retrieves the path of the stub file based on the given type. * diff --git a/src/Trait/FailedValidationTrait.php b/src/Trait/FailedValidationTrait.php index 4bce577..d269a0f 100644 --- a/src/Trait/FailedValidationTrait.php +++ b/src/Trait/FailedValidationTrait.php @@ -27,12 +27,12 @@ protected function failedValidation(Validator $validator): void 'field' => $field, 'title' => 'Failed request validation', "message" => 'Invalid data', - "detail" => $validator->errors()->all() + "detail" => $message ]; } - response()->json([ - 'errors' => $formattedErrors, - ], Response::HTTP_UNPROCESSABLE_ENTITY)->send(); + throw new HttpResponseException( + response()->json(['errors' => $formattedErrors], Response::HTTP_UNPROCESSABLE_ENTITY) + ); } }