ModelNotFoundHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A entityHasTranslation() 0 9 2
A modelNotFoundException() 0 16 1
A extractEntityName() 0 11 2
A translationModelKeys() 0 3 1
1
<?php
2
3
namespace SMartins\JsonHandler;
4
5
use Illuminate\Database\Eloquent\ModelNotFoundException;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloq...\ModelNotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
trait ModelNotFoundHandler
8
{
9
    /**
10
     * Set the response if Exception is ModelNotFound.
11
     *
12
     * @param  ModelNotFoundException $exception
13
     */
14
    public function modelNotFoundException(ModelNotFoundException $exception)
15
    {
16
        $entity = $this->extractEntityName($exception->getModel());
17
18
        $ids = implode($exception->getIds(), ',');
0 ignored issues
show
Unused Code introduced by
The assignment to $ids is dead and can be removed.
Loading history...
Bug introduced by
',' of type string is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
        $ids = implode($exception->getIds(), /** @scrutinizer ignore-type */ ',');
Loading history...
19
20
        $error = [[
21
            'status'    => 404,
22
            'code'      => $this->getCode('model_not_found'),
0 ignored issues
show
Bug introduced by
It seems like getCode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
            'code'      => $this->/** @scrutinizer ignore-call */ getCode('model_not_found'),
Loading history...
23
            'source'    => ['pointer' => 'data/id'],
24
            'title'     => $exception->getMessage(),
25
            'detail'    => __('exception::exceptions.model_not_found.title', ['model' => $entity]),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
            'detail'    => /** @scrutinizer ignore-call */ __('exception::exceptions.model_not_found.title', ['model' => $entity]),
Loading history...
26
        ]];
27
28
        $this->jsonApiResponse->setStatus(404);
0 ignored issues
show
Bug Best Practice introduced by
The property jsonApiResponse does not exist on SMartins\JsonHandler\ModelNotFoundHandler. Did you maybe forget to declare it?
Loading history...
29
        $this->jsonApiResponse->setErrors($error);
30
    }
31
32
    /**
33
     * Get entitie name based on model path to mount the message.
34
     *
35
     * @param  string $model
36
     * @return string
37
     */
38
    public function extractEntityName($model)
39
    {
40
        $classNames = (array) explode('\\', $model);
41
42
        $entityName = end($classNames);
43
44
        if ($this->entityHasTranslation($entityName)) {
45
            return __('exception::exceptions.models.'.$entityName);
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
            return /** @scrutinizer ignore-call */ __('exception::exceptions.models.'.$entityName);
Loading history...
46
        }
47
48
        return $entityName;
49
    }
50
51
    /**
52
     * Check if entity returned on ModelNotFoundException has translation on
53
     * exceptions file
54
     * @param  string $entityName The model name to check if has translation
55
     * @return bool               Has translation or not
56
     */
57
    public function entityHasTranslation(string $entityName): bool
58
    {
59
        $hasKey = in_array($entityName, $this->translationModelKeys());
60
61
        if ($hasKey) {
62
            return ! empty($hasKey);
63
        }
64
65
        return false;
66
    }
67
68
    /**
69
     * Get the models keys on exceptions lang file
70
     * @return array An array with keys to translate
71
     */
72
    private function translationModelKeys(): array
73
    {
74
        return array_keys(__('exception::exceptions.models'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        return array_keys(/** @scrutinizer ignore-call */ __('exception::exceptions.models'));
Loading history...
75
    }
76
}
77