Skip to content

Commit

Permalink
Merge pull request #6 from Oscabrera/feature/Fix-Trait-Validation
Browse files Browse the repository at this point in the history
fix src/Trait/FailedValidationTrait.php
  • Loading branch information
Oscabrera authored Jun 20, 2024
2 parents 8880183 + 984d537 commit 8697c51
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/Makers/MakeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Handlers/Makers/MakeInterfaceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Handlers/Makers/MakeInterfaceServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Handlers/Makers/MakeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Handlers/Makers/MakeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Handlers/Makers/MakeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
26 changes: 20 additions & 6 deletions src/Handlers/Makers/MakeStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -30,20 +31,17 @@ public function __construct(
* @param string $classPath
* @param array<string, string> $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
*/
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),
Expand All @@ -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.
*
Expand Down
8 changes: 4 additions & 4 deletions src/Trait/FailedValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}

0 comments on commit 8697c51

Please sign in to comment.