sfelix-martins /
json-exception-handler
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SMartins\Exceptions\Handlers; |
||
| 4 | |||
| 5 | use SMartins\Exceptions\JsonApi\Error; |
||
| 6 | use SMartins\Exceptions\JsonApi\Source; |
||
| 7 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
||
| 8 | |||
| 9 | class ModelNotFoundHandler extends AbstractHandler |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var ModelNotFoundException |
||
| 13 | */ |
||
| 14 | protected $exception; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Create instance using the Exception to be handled. |
||
| 18 | * |
||
| 19 | * @param \Illuminate\Database\Eloquent\ModelNotFoundException $e |
||
| 20 | */ |
||
| 21 | public function __construct(ModelNotFoundException $e) |
||
| 22 | { |
||
| 23 | parent::__construct($e); |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | public function handle() |
||
| 30 | { |
||
| 31 | $entity = $this->extractEntityName($this->exception->getModel()); |
||
| 32 | |||
| 33 | $detail = __('exception::exceptions.model_not_found.title', ['model' => $entity]); |
||
| 34 | |||
| 35 | return (new Error)->setStatus(404) |
||
| 36 | ->setCode($this->getCode('model_not_found')) |
||
| 37 | ->setSource((new Source())->setPointer('data/id')) |
||
| 38 | ->setTitle(snake_case(class_basename($this->exception))) |
||
| 39 | ->setDetail($detail); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Get entity name based on model path to mount the message. |
||
| 44 | * |
||
| 45 | * @param string $model |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | public function extractEntityName(string $model) |
||
| 49 | { |
||
| 50 | $classNames = (array) explode('\\', $model); |
||
| 51 | |||
| 52 | $entityName = end($classNames); |
||
| 53 | |||
| 54 | if ($this->entityHasTranslation($entityName)) { |
||
| 55 | return __('exception::exceptions.models.'.$entityName); |
||
| 56 | } |
||
| 57 | |||
| 58 | return $entityName; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Check if entity returned on ModelNotFoundException has translation on |
||
| 63 | * exceptions file. |
||
| 64 | * |
||
| 65 | * @param string $entityName The model name to check if has translation |
||
| 66 | * @return bool Has translation or not |
||
| 67 | */ |
||
| 68 | public function entityHasTranslation(string $entityName): bool |
||
| 69 | { |
||
| 70 | $hasKey = in_array($entityName, $this->translationModelKeys()); |
||
| 71 | |||
| 72 | if ($hasKey) { |
||
| 73 | return ! empty($hasKey); |
||
| 74 | } |
||
| 75 | |||
| 76 | return false; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get the models keys on exceptions lang file. |
||
| 81 | * |
||
| 82 | * @return array An array with keys to translate |
||
| 83 | */ |
||
| 84 | private function translationModelKeys(): array |
||
| 85 | { |
||
| 86 | return array_keys(__('exception::exceptions.models')); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 87 | } |
||
| 88 | } |
||
| 89 |