TransformsFlexibleErrors   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 97
ccs 30
cts 34
cp 0.8824
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A updateResponseErrors() 0 9 2
A transformFlexibleErrors() 0 7 1
A shouldTransformFlexibleErrors() 0 4 2
A transformMessages() 0 17 2
A getTransformedErrors() 0 17 3
1
<?php
2
3
namespace NovaFlexibleContent\Http;
4
5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Support\Facades\Lang;
7
use Illuminate\Support\Str;
8
use NovaFlexibleContent\Flexible;
9
use Symfony\Component\HttpFoundation\Response;
10
11
trait TransformsFlexibleErrors
12
{
13
    /**
14
     * Checks whether the given response's flexible errors can and should be transformed
15
     *
16
     * @param \Symfony\Component\HttpFoundation\Response $response
17
     * @return bool
18
     */
19 5
    protected function shouldTransformFlexibleErrors(Response $response)
20
    {
21 5
        return  $response->getStatusCode() === Response::HTTP_UNPROCESSABLE_ENTITY
22 5
                && is_a($response, JsonResponse::class);
23
    }
24
25
    /**
26
     * Updates given response's errors for the concerned flexible fields
27
     *
28
     * @param Response $response
29
     * @return \Symfony\Component\HttpFoundation\Response
30
     */
31 1
    protected function transformFlexibleErrors(Response $response)
32
    {
33 1
        $response->setData(
0 ignored issues
show
Bug introduced by
The method setData() does not exist on Symfony\Component\HttpFoundation\Response. Did you maybe mean setDate()? ( Ignorable by Annotation )

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

33
        $response->/** @scrutinizer ignore-call */ 
34
                   setData(

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.

Loading history...
34 1
            $this->updateResponseErrors($response->original)
35 1
        );
36
37 1
        return $response;
38
    }
39
40
    /**
41
     * Run response errors parsing if necessary
42
     *
43
     * @param  array  $data
44
     * @return array
45
     */
46 1
    protected function updateResponseErrors($data)
47
    {
48 1
        if (!($data['errors'] ?? null)) {
49
            return $data;
50
        }
51
52 1
        $data['errors'] = $this->getTransformedErrors($data['errors']);
53
54 1
        return $data;
55
    }
56
57
    /**
58
     * Transforms the original errors array in a nested
59
     * array structure.
60
     *
61
     * @param  array  $errors
62
     * @return array
63
     */
64 1
    protected function getTransformedErrors($errors): array
65
    {
66 1
        $parsed = [];
67
68 1
        foreach ($errors as $key => $messages) {
69 1
            $attribute = Flexible::getValidationKey($key);
70
71 1
            if (!$attribute) {
72
                $parsed[$key] = $messages;
73
74
                continue;
75
            }
76
77 1
            $parsed[$attribute->original] = $this->transformMessages($messages, $key, $attribute);
78
        }
79
80 1
        return $parsed;
81
    }
82
83
    /**
84
     * Update human error messages with correct field names
85
     *
86
     * @param  array  $messages
87
     * @param  string $key
88
     * @param  \NovaFlexibleContent\Http\FlexibleAttribute  $attribute
89
     * @return array
90
     */
91 1
    protected function transformMessages($messages, $key, $attribute)
92
    {
93 1
        $search    = str_replace('_', ' ', Str::snake($key));
94 1
        $attribute = str_replace('_', ' ', Str::snake($attribute->name));
95
96
        // We translate the attribute if it exists
97 1
        if (Lang::has('validation.attributes.'.$attribute)) {
98
            $attribute = trans('validation.attributes.'.$attribute);
99
        }
100
101 1
        return array_map(function ($message) use ($search, $attribute) {
102 1
            return str_replace(
103 1
                [$search, Str::upper($search), Str::ucfirst($search)],
104 1
                [$attribute, Str::upper($attribute), Str::ucfirst($attribute)],
105 1
                $message
106 1
            );
107 1
        }, $messages);
108
    }
109
}
110