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

JsonApiResponseHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 61.54 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 56
loc 91
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D sendFailedResponse() 56 75 9
A sendSuccessResponse() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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