Passed
Branch 2.0 (f5ddb0)
by Samuel
04:04
created

ValidationHandler::getTreatedFails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SMartins\Exceptions\Handlers;
4
5
use SMartins\Exceptions\JsonApi\Error;
6
use SMartins\Exceptions\JsonApi\Source;
7
use SMartins\Exceptions\JsonApi\ErrorCollection;
8
9
class ValidationHandler extends AbstractHandler
10
{
11
    /**
12
     * {@inheritDoc}
13
     */
14
    public function handle()
15
    {
16
        $errors = new ErrorCollection;
17
        $errors->setStatusCode(400);
18
19
        $validationMessages = $this->getTreatedMessages();
20
        $validationFails = $this->getTreatedFails();
21
22
        foreach ($validationMessages as $field => $messages) {
23
            foreach ($messages as $key => $message) {
24
                $attributes = $this->getValidationAttributes($validationFails, $key, $field);
25
26
                $error = (new Error)->setStatus(422)
27
                    ->setSource((new Source())->setPointer($field))
28
                    ->setTitle($attributes['title'])
0 ignored issues
show
Bug introduced by
It seems like $attributes['title'] can also be of type null and array; however, parameter $title of SMartins\Exceptions\JsonApi\Error::setTitle() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

28
                    ->setTitle(/** @scrutinizer ignore-type */ $attributes['title'])
Loading history...
29
                    ->setDetail($message);
30
31
                if (! is_null($attributes['code'])) {
32
                    $error->setCode($attributes['code']);
33
                }
34
35
                $errors->push($error);
36
            }
37
        }
38
39
        return $errors;
40
    }
41
42
    public function getValidationAttributes(array $validationFails, $key, $field)
43
    {
44
        return [
45
            'code' => $this->getValidationCode($validationFails, $key, $field),
46
            'title' => $this->getValidationTitle($validationFails, $key, $field),
47
        ];
48
    }
49
50
    public function getValidationTitle(array $validationFails, $key, $field)
51
    {
52
        return __('exception::exceptions.validation.title', [
53
            'fails' => array_keys($validationFails[$field])[$key],
54
            'field' => $field,
55
        ]);
56
    }
57
58
    public function getValidationCode(array $validationFails, $key, $field)
59
    {
60
        $rule = strtolower(array_keys($validationFails[$field])[$key]);
61
62
        return config('json-exception-handler.codes.validation_fields.'.$field.'.'.$rule);
63
    }
64
65
    /**
66
     * Get message based on exception type. If exception is generated by
67
     * $this->validate() from default Controller methods the exception has the
68
     * response object. If exception is generated by Validator::make() the
69
     * messages are getted different.
70
     *
71
     * @return array
72
     */
73
    public function getTreatedMessages()
74
    {
75
        return $this->exception->validator->messages()->messages();
76
    }
77
78
    public function getTreatedFails()
79
    {
80
        return $this->exception->validator->failed();
81
    }
82
}
83