Skip to content

Commit

Permalink
License.
Browse files Browse the repository at this point in the history
  • Loading branch information
wector975 committed Dec 22, 2024
1 parent aec508b commit c129ec0
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 61 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Aleksandr Zakhozhiy <azakhozhiy@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# azakhozhiy/laravel-exceptions
# Laravel Exceptions

## License

This library is licensed under the [MIT License](./LICENSE).
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "azakhozhiy/laravel-exceptions",
"version": "1.0.0",
"description": "Laravel Exceptions",
"license": "MIT",
"authors": [
{
"name": "Aleksandr Zakhozhiy",
Expand Down
31 changes: 31 additions & 0 deletions src/Contract/ExceptionRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace AZakhozhiy\Laravel\Exceptions\Contract;

use AZakhozhiy\Laravel\Exceptions\BaseExceptionObject;
use AZakhozhiy\Laravel\Exceptions\BaseServiceException;
use AZakhozhiy\Laravel\Exceptions\Mapping\ExceptionCategoryItem;
use Throwable;

interface ExceptionRepositoryInterface
{
public function getCategoriesSlugs(): array;

public function registerExceptionCategory(string $catClass): static;

public function registerException(string $catClass, string $exceptionClass): static;

public function getExceptionObjectsByCategory(string $catSlug): array;

public function getExceptionObject(string $catSlug, int $errorCode): BaseExceptionObject;

public function getExceptionCategory(string $catSlug, bool $withCodes = false): ExceptionCategoryItem;

public function buildException(
string $catSlug,
int $errorCode,
string|array|null $customMessage = null,
?int $customHttpCode = null,
?Throwable $previous = null
): BaseServiceException;
}
121 changes: 61 additions & 60 deletions src/Service/ExceptionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AZakhozhiy\Laravel\Exceptions\Service;

use AZakhozhiy\Laravel\Exceptions\Contract\ExceptionRepositoryInterface;
use Closure;
use Throwable;
use AZakhozhiy\Laravel\Exceptions\BaseExceptionCategory;
Expand All @@ -17,7 +18,7 @@
use AZakhozhiy\Laravel\Exceptions\System\UnknownExceptionCategory;
use AZakhozhiy\Laravel\Exceptions\System\UnknownExceptionCode;

class ExceptionRepository
class ExceptionRepository implements ExceptionRepositoryInterface
{
/** @var Closure[] */
protected array $categories;
Expand All @@ -30,24 +31,6 @@ public function getCategoriesSlugs(): array
return array_keys($this->categories);
}

public function assertCategoryNotExists(string $catSlug): void
{
if (isset($this->categories[$catSlug])) {
throw new ExceptionCategoryAlreadyRegistered(
"Category $catSlug already registered."
);
}
}

public function assertCategoryExists(string $catSlug): void
{
if (!isset($this->categories[$catSlug])) {
throw new UnknownExceptionCategory(
"Unknown exception category $catSlug."
);
}
}

/**
* @param class-string<BaseExceptionCategory> $catClass
* @return $this
Expand Down Expand Up @@ -82,25 +65,6 @@ public function registerException(string $catClass, string $exceptionClass): sta
return $this;
}

protected function assertCategoryAndExceptionNotExist(string $catSlug, int $errorCode): void
{
if (isset($this->exceptions[$catSlug][$errorCode])) {
throw new ExceptionCodeAlreadyRegistered(
"Exception code [$errorCode] already " .
"registered for the [$catSlug] category."
);
}
}

protected function assertCategoryAndExceptionExist(string $catSlug, int $errorCode): void
{
if (!isset($this->exceptions[$catSlug][$errorCode])) {
throw new UnknownExceptionCode(
"Unknown exception code $errorCode for category $catSlug."
);
}
}

/**
* @param string $catSlug
* @return BaseExceptionObject[]
Expand All @@ -117,28 +81,6 @@ public function getExceptionObjectsByCategory(string $catSlug): array
return $items;
}

public function getExceptionObject(string $catSlug, int $errorCode): BaseExceptionObject
{
$this->assertCategoryExists($catSlug);
$this->assertCategoryAndExceptionExist($catSlug, $errorCode);

return $this->exceptions[$catSlug][$errorCode]();
}

public function getExceptionCategory(string $catSlug, bool $withCodes = false): ExceptionCategoryItem
{
$this->assertCategoryExists($catSlug);

/** @var ExceptionCategoryItem $cat */
$cat = $this->categories[$catSlug]();

if ($withCodes) {
$cat->addCodes($this->getExceptionObjectsByCategory($catSlug));
}

return $cat;
}

public function buildException(
string $catSlug,
int $errorCode,
Expand Down Expand Up @@ -170,4 +112,63 @@ public function buildException(
$previous
);
}

public function getExceptionObject(string $catSlug, int $errorCode): BaseExceptionObject
{
$this->assertCategoryExists($catSlug);
$this->assertCategoryAndExceptionExist($catSlug, $errorCode);

return $this->exceptions[$catSlug][$errorCode]();
}

public function getExceptionCategory(string $catSlug, bool $withCodes = false): ExceptionCategoryItem
{
$this->assertCategoryExists($catSlug);

/** @var ExceptionCategoryItem $cat */
$cat = $this->categories[$catSlug]();

if ($withCodes) {
$cat->addCodes($this->getExceptionObjectsByCategory($catSlug));
}

return $cat;
}

protected function assertCategoryNotExists(string $catSlug): void
{
if (isset($this->categories[$catSlug])) {
throw new ExceptionCategoryAlreadyRegistered(
"Category $catSlug already registered."
);
}
}

protected function assertCategoryExists(string $catSlug): void
{
if (!isset($this->categories[$catSlug])) {
throw new UnknownExceptionCategory(
"Unknown exception category $catSlug."
);
}
}

protected function assertCategoryAndExceptionNotExist(string $catSlug, int $errorCode): void
{
if (isset($this->exceptions[$catSlug][$errorCode])) {
throw new ExceptionCodeAlreadyRegistered(
"Exception code [$errorCode] already " .
"registered for the [$catSlug] category."
);
}
}

protected function assertCategoryAndExceptionExist(string $catSlug, int $errorCode): void
{
if (!isset($this->exceptions[$catSlug][$errorCode])) {
throw new UnknownExceptionCode(
"Unknown exception code $errorCode for category $catSlug."
);
}
}
}

0 comments on commit c129ec0

Please sign in to comment.