CustomException::isLower()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
1
<?php namespace H4ad\Scheduler\Exceptions;
2
3
/**
4
 * Esse arquivo faz parte do Scheduler,
5
 * uma biblioteca para auxiliar com agendamentos.
6
 *
7
 * @license MIT
8
 * @package H4ad\Scheduler
9
 */
10
11
class CustomException extends \Exception
12
{
13
    /**
14
     * Model que não pertence ao usuário
15
     *
16
     * @var mixed
17
     */
18
    protected $model;
19
20
    /**
21
     * Key para o arquivo de tradução de exceções.
22
     *
23
     * @var string
24
     */
25
    protected $trans;
26
27
    /**
28
     * HTTP Status Code
29
     *
30
     * @var int
31
     */
32
    protected $statusCode;
33
34
    /**
35
     * Atributo usado como key para substituir por um texto.
36
     *
37
     * @var array|string
38
     */
39
    protected $attributes = 'model';
40
41
    /**
42
     * Diz se o alias será no singular ou no plural.
43
     *
44
     * @var string
45
     */
46
    protected $aliastype = 'singular';
47
48
    /**
49
     * Valor passado para o atributo
50
     *
51
     * @var array|string|null
52
     */
53
    protected $values = null;
54
55
    /**
56
     * Indica se a model será printada com lower case.
57
     *
58
     * @var boolean
59
     */
60
    protected $lowercase = false;
61
62
    /**
63
     * Construtor da exceção
64
     *
65
     * @param mixed $model
66
     */
67
    public function __construct($model = 'foo')
68
    {
69
        $this->model = $model;
70
    }
71
72
    /**
73
     * Render the exception as an HTTP response.
74
     *
75
     * @return \Illuminate\Http\JsonResponse
76
     */
77
    public function render()
78
    {
79
        return response()->
80
            /** @scrutinizer ignore-call */
81
            json([
82
            'messages' => trans('scheduler::exceptions.'. $this->trans, $this->parseValues())
83
        ], $this->statusCode);
84
    }
85
86
    /**
87
     * Dá parse nos valores para a string de tradução.
88
     *
89
     * @return array
90
     */
91
    protected function parseValues()
92
    {
93
        if(is_array($this->attributes) && is_array($this->values))
94
            return collect($this->attributes)->combine($this->values)->all();
95
96
        return [ $this->attributes => $this->values ?? $this->isLower() ];
97
    }
98
99
    /**
100
     * Verifica se é lowercase e retorna de acordo.
101
     *
102
     * @return string
103
     */
104
    protected function isLower()
105
    {
106
        return $this->lowercase ? strtolower($this->getAlias()) : $this->getAlias();
107
    }
108
109
    /**
110
     * Retorna o alias da model.
111
     *
112
     * @return string
113
     */
114
    protected function getAlias()
115
    {
116
        if(is_object($this->model))
117
            $this->model = get_class($this->model);
118
119
        return collect(trans('scheduler::exceptions.aliases.'. $this->aliastype))->search($this->model) ?: 'Recurso';
120
    }
121
122
    /**
123
     * Seta os valores.
124
     *
125
     * @param mixed $values
126
     */
127
    public function setValues($values)
128
    {
129
        $this->values = $values;
130
131
        return $this;
132
    }
133
}
134