Responder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 9
Bugs 1 Features 3
Metric Value
wmc 5
c 9
b 1
f 3
lcom 1
cbo 0
dl 0
loc 41
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A respond() 0 12 3
A getRequestFormat() 0 4 1
1
<?php
2
3
namespace Vinelab\Api;
4
5
/*
6
 * @author Mahmoud Zalt <[email protected]>
7
 */
8
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Response;
11
12
class Responder
13
{
14
    /**
15
     * @param \Illuminate\Http\Request $request
16
     */
17
    public function __construct(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
18
    {
19
        $this->request = $request;
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
    }
21
22
    /**
23
     * @param $response
24
     * @param $status
25
     * @param $headers
26
     * @param $options
27
     *
28
     * @return $this|\Illuminate\Http\JsonResponse
29
     */
30
    public function respond($response, $status, $headers, $options)
31
    {
32
        switch ($this->getRequestFormat()) {
33
            case 'html' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
34
            case 'text/html' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
35
                return Response::make($response, $status, $headers, $options)
36
                    ->header('Content-Type', 'text/html');
37
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
38
                // whether 'Content-Type' is NULL or equal to anything such as 'application/json' response will be JSON
39
                return Response::json($response, $status, $headers, $options);
40
        }
41
    }
42
43
    /**
44
     * Get request format from the content type header property.
45
     *
46
     * @return string
47
     */
48
    public function getRequestFormat()
49
    {
50
        return $this->request->header('Content-Type');
51
    }
52
}
53