ApiController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 15.84 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 16
loc 101
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatusCode() 0 4 1
A setStatusCode() 0 6 1
A respondNoContent() 0 4 1
A respondNotFound() 0 4 1
A respond() 0 4 1
A respondCreated() 8 8 1
A respondPoked() 8 8 1
A respondWithError() 0 9 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 PiFinder\Http\Controllers\Api;
4
5
use Illuminate\Support\Facades\Response;
6
use PiFinder\Http\Controllers\Controller;
7
8
class ApiController extends Controller
9
{
10
    /**
11
     * Default Status Code.
12
     *
13
     * @var int
14
     */
15
    protected $statusCode = 200;
16
17
    /**
18
     * @return int
19
     */
20 10
    public function getStatusCode()
21
    {
22 10
        return $this->statusCode;
23
    }
24
25
    /**
26
     * @param int $statusCode
27
     *
28
     * @return $this
29
     */
30 9
    public function setStatusCode($statusCode)
31
    {
32 9
        $this->statusCode = $statusCode;
33
34 9
        return $this;
35
    }
36
37
    /**
38
     * @param array $data
39
     *
40
     * @return mixed
41
     */
42 1 View Code Duplication
    public function respondCreated($data, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
43
    {
44
        $headers = [
45 1
            'Location' => route('devices.show', $id),
46
        ];
47
48 1
        return $this->setStatusCode(201)->respond(compact('data'), $headers);
49
    }
50
51
    /**
52
     * @param array $data
53
     *
54
     * @return mixed
55
     */
56 5 View Code Duplication
    public function respondPoked($data, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
58
        $headers = [
59 5
            'Location' => route('devices.show', $id),
60
        ];
61
62 5
        return $this->setStatusCode(200)->respond(compact('data'), $headers);
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68 1
    public function respondNoContent()
69
    {
70 1
        return $this->setStatusCode(204)->respond([]);
71
    }
72
73
    /**
74
     * @param string $message
75
     *
76
     * @return mixed
77
     */
78 3
    public function respondNotFound($message = 'Did not find the resource you are looking for!')
79
    {
80 3
        return $this->setStatusCode(404)->respondWithError($message);
81
    }
82
83
    /**
84
     * @param $data
85
     * @param array $headers
86
     *
87
     * @return mixed
88
     */
89 10
    public function respond($data, $headers = [])
90
    {
91 10
        return Response::json($data, $this->getStatusCode(), $headers);
92
    }
93
94
    /**
95
     * @param $message
96
     *
97
     * @return mixed
98
     */
99 3
    public function respondWithError($message)
100
    {
101 3
        return $this->respond([
102
            'errors' => [
103 3
                'title'  => $message,
104 3
                'status' => $this->getStatusCode(),
105
            ],
106
        ]);
107
    }
108
}
109