Completed
Pull Request — master (#5)
by Samuel
07:07 queued 03:31
created

ModelNotFoundHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 20
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A translationModelKeys() 0 3 1
A __construct() 0 3 1
A extractEntityName() 0 11 2
A entityHasTranslation() 0 9 2
A handle() 0 11 1
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
__('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 ignore-type  annotation

86
        return array_keys(/** @scrutinizer ignore-type */ __('exception::exceptions.models'));
Loading history...
87
    }
88
}
89