Passed
Push — master ( d7d8c0...c26592 )
by Mads
03:35
created

Renderer::render()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 67
Code Lines 43

Duplication

Lines 53
Ratio 79.1 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 43
nc 7
nop 0
dl 53
loc 67
ccs 0
cts 36
cp 0
crap 56
rs 7.0237
c 1
b 0
f 0

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 Napp\Core\Api\Exceptions\Renderer;
4
5
use Illuminate\Http\JsonResponse;
6
use Napp\Core\Api\Exceptions\Exceptions\Exception as NappException;
7
8
class Renderer implements RendererInterface
9
{
10
    /**
11
     * @var \Exception
12
     */
13
    protected $exception;
14
15
    /**
16
     * @var int
17
     */
18
    protected $statusCode;
19
20
    /**
21
     * @var string
22
     */
23
    protected $statusMessage;
24
25
    /**
26
     * @var int
27
     */
28
    protected $responseCode;
29
30
    /**
31
     * @return JsonResponse
32
     */
33
    public function render(): JsonResponse
34
    {
35
        if (true === $this->exception instanceof NappException) {
36
            return response()->json(
37
                [
38
                'error' => [
39
                    'code' => $this->statusCode,
40
                    'message' => $this->statusMessage,
41
                ]],
42
                $this->responseCode
43
            );
44
        }
45
46
        switch ($this->responseCode) {
47 View Code Duplication
            case 400:
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...
48
                return response()->json(
49
                    [
50
                    'error' => [
51
                        'code' => $this->statusCode,
52
                        'message' => 'Unprocessable Entity'
53
                    ]],
54
                    $this->responseCode
55
                );
56 View Code Duplication
            case 401:
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...
57
                return response()->json(
58
                    [
59
                        'error' => [
60
                            'code' => $this->statusCode,
61
                            'message' => 'Authentication credentials were missing or incorrect'
62
                        ]],
63
                    $this->responseCode
64
                );
65 View Code Duplication
            case 403:
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...
66
                return response()->json(
67
                    [
68
                        'error' => [
69
                            'code' => $this->statusCode,
70
                            'message' => 'Forbidden'
71
                        ]],
72
                    $this->responseCode
73
                );
74 View Code Duplication
            case 404:
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...
75
                return response()->json(
76
                    [
77
                    'error' => [
78
                        'code' => $this->statusCode,
79
                        'message' => 'Not Found'
80
                    ]],
81
                    $this->responseCode
82
                );
83 View Code Duplication
            case 405:
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...
84
                return response()->json(
85
                    [
86
                    'error' => [
87
                        'code' => $this->statusCode,
88
                        'message' => 'Method Not Allowed'
89
                    ]],
90
                    $this->responseCode
91
                );
92 View Code Duplication
            default:
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...
93
                return response()->json(
94
                    [
95
                    'error' => [
96
                        'code' => $this->statusCode,
97
                        'message' => 'Internal Server Error'
98
                    ]],
99
                    500
100
                );
101
        }
102
    }
103
104
    /**
105
     * @param \Exception $e
106
     * @return void
107
     */
108
    public function setException(\Exception $e)
109
    {
110
        $this->exception = $e;
111
    }
112
113
    /**
114
     * @param int $responseCode
115
     * @return void
116
     */
117
    public function setResponseCode($responseCode)
118
    {
119
        $this->responseCode = $responseCode;
120
    }
121
122
    /**
123
     * @param int $statusCode
124
     * @return void
125
     */
126
    public function setStatusCode($statusCode)
127
    {
128
        $this->statusCode = $statusCode;
129
    }
130
131
    /**
132
     * @param string $statusMessage
133
     * @return void
134
     */
135
    public function setStatusMessage($statusMessage)
136
    {
137
        $this->statusMessage = $statusMessage;
138
    }
139
}
140