Test Setup Failed
Push — master ( 22888c...730ea3 )
by alexfloppy
01:21
created

JsonApiResponseHelper::sendFailedResponse()   D

Complexity

Conditions 9
Paths 256

Size

Total Lines 75
Code Lines 50

Duplication

Lines 56
Ratio 74.67 %

Importance

Changes 0
Metric Value
dl 56
loc 75
rs 4
c 0
b 0
f 0
cc 9
eloc 50
nc 256
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Helpers;
4
5
use Illuminate\Http\JsonResponse;
6
7
/**
8
 * Class jsonApiResponseHelper
9
 * @package Helpers
10
 */
11
trait JsonApiResponseHelper
12
{
13
    /**
14
     * @param array $errors
15
     * @param int $statusCode
16
     * @return JsonResponse
17
     */
18
    public function sendFailedResponse(array $errors, int $statusCode)
19
    {
20
        $errorsJson = [];
21 View Code Duplication
        if (!empty($errors['email'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
            $emailError = [
23
                'status' => $statusCode,
24
                'title'  => 'Email error',
25
                'detail' => $errors['email'][0]
26
            ];
27
            array_push($errorsJson, $emailError);
28
        }
29
30 View Code Duplication
        if (!empty($errors['password'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
            $passwordError = [
32
                'status' => $statusCode,
33
                'title'  => 'Password error',
34
                'detail' => $errors['password'][0]
35
            ];
36
            array_push($errorsJson, $passwordError);
37
        }
38
39 View Code Duplication
        if (!empty($errors['password_confirmation'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
            $passwordConfirmationError = [
41
                'status' => $statusCode,
42
                'title'  => 'Password error',
43
                'detail' => $errors['password_confirmation'][0]
44
            ];
45
            array_push($errorsJson, $passwordConfirmationError);
46
        }
47
48 View Code Duplication
        if (!empty($errors['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
            $nameError = [
50
                'status' => $statusCode,
51
                'title'  => 'Name error',
52
                'detail' => $errors['name'][0]
53
            ];
54
            array_push($errorsJson, $nameError);
55
        }
56
57 View Code Duplication
        if (!empty($errors['token'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
            $tokenError = [
59
                'status' => $statusCode,
60
                'title'  => 'Token error',
61
                'detail' => $errors['token']
62
            ];
63
            array_push($errorsJson, $tokenError);
64
        }
65
66 View Code Duplication
        if (!empty($errors['token'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            $tokenError = [
68
                'status' => $statusCode,
69
                'title'  => 'Token error',
70
                'detail' => $errors['token']
71
            ];
72
            array_push($errorsJson, $tokenError);
73
        }
74
75 View Code Duplication
        if (!empty($errors['activation_hash'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
            $activationHashError = [
77
                'status' => $statusCode,
78
                'title'  => 'Activation hash error',
79
                'detail' => $errors['activation_hash'][0]
80
            ];
81
            array_push($errorsJson, $activationHashError);
82
        }
83
84
        $errorsJson = ($errorsJson) ? $errorsJson : $errors;
85
86
        $responseJson = response()->json([
87
            'errors' =>
88
                $errorsJson
89
        ], $statusCode);
90
91
        return $responseJson;
92
    }
93
94
    /**
95
     * @return JsonResponse
96
     */
97
    public function sendSuccessResponse()
98
    {
99
        return response()->json([], self::HTTP_CODE_OK);
100
    }
101
}
102