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 | * Create instance using the Exception to be handled. |
||||
| 13 | * |
||||
| 14 | * @param \Illuminate\Database\Eloquent\ModelNotFoundException $e |
||||
| 15 | */ |
||||
| 16 | public function __construct(ModelNotFoundException $e) |
||||
| 17 | { |
||||
| 18 | parent::__construct($e); |
||||
| 19 | } |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * {@inheritdoc} |
||||
| 23 | */ |
||||
| 24 | public function handle() |
||||
| 25 | { |
||||
| 26 | $entity = $this->extractEntityName($this->exception->getModel()); |
||||
|
0 ignored issues
–
show
|
|||||
| 27 | |||||
| 28 | $detail = __('exception::exceptions.model_not_found.title', ['model' => $entity]); |
||||
| 29 | |||||
| 30 | return (new Error)->setStatus(404) |
||||
| 31 | ->setCode($this->getCode('model_not_found')) |
||||
| 32 | ->setSource((new Source())->setPointer('data/id')) |
||||
| 33 | ->setTitle(snake_case(class_basename($this->exception))) |
||||
| 34 | ->setDetail($detail); |
||||
| 35 | } |
||||
| 36 | |||||
| 37 | /** |
||||
| 38 | * Get entity name based on model path to mount the message. |
||||
| 39 | * |
||||
| 40 | * @param string $model |
||||
| 41 | * @return string |
||||
| 42 | */ |
||||
| 43 | public function extractEntityName($model) |
||||
| 44 | { |
||||
| 45 | $classNames = (array) explode('\\', $model); |
||||
| 46 | |||||
| 47 | $entityName = end($classNames); |
||||
| 48 | |||||
| 49 | if ($this->entityHasTranslation($entityName)) { |
||||
| 50 | return __('exception::exceptions.models.'.$entityName); |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | return $entityName; |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * Check if entity returned on ModelNotFoundException has translation on |
||||
| 58 | * exceptions file. |
||||
| 59 | * |
||||
| 60 | * @param string $entityName The model name to check if has translation |
||||
| 61 | * @return bool Has translation or not |
||||
| 62 | */ |
||||
| 63 | public function entityHasTranslation(string $entityName): bool |
||||
| 64 | { |
||||
| 65 | $hasKey = in_array($entityName, $this->translationModelKeys()); |
||||
| 66 | |||||
| 67 | if ($hasKey) { |
||||
| 68 | return ! empty($hasKey); |
||||
| 69 | } |
||||
| 70 | |||||
| 71 | return false; |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | /** |
||||
| 75 | * Get the models keys on exceptions lang file. |
||||
| 76 | * |
||||
| 77 | * @return array An array with keys to translate |
||||
| 78 | */ |
||||
| 79 | private function translationModelKeys(): array |
||||
| 80 | { |
||||
| 81 | return array_keys(__('exception::exceptions.models')); |
||||
|
0 ignored issues
–
show
__('exception::exceptions.models') of type string is incompatible with the type array expected by parameter $input of array_keys().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 82 | } |
||||
| 83 | } |
||||
| 84 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.