JSend   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 79
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 6 1
A error() 0 6 1
A fail() 0 6 1
A getResponseHandler() 0 12 3
A getErrorResponseHandler() 0 4 1
A getFailResponseHandler() 0 4 1
A getSuccessResponseHandler() 0 4 1
1
<?php
2
3
namespace RichToms\LaravelJsend;
4
5
class JSend
6
{
7
    /**
8
     * @param  mixed  $data
9
     * @param  int  $statusCode
10
     * @return $this
11
     */
12
    public function build($data, $statusCode = 200)
13
    {
14
        return $this->getResponseHandler($statusCode)
15
            ->withData($data)
16
            ->withStatusCode($statusCode);
17
    }
18
19
    /**
20
     * @param  string  $message
21
     * @param  int  $statusCode
22
     * @return $this
23
     */
24
    public function error($message, $statusCode = 500, $code = null, $data = null)
0 ignored issues
show
Unused Code introduced by
The parameter $code is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        return $this->getResponseHandler($statusCode)
27
            ->withMessage($message)
28
            ->withStatusCode($statusCode);
29
    }
30
31
    /**
32
     * @param  array  $data
33
     * @param  int  $statusCode
34
     * @return $this
35
     */
36
    public function fail($data, $statusCode = 400)
37
    {
38
        return $this->getResponseHandler($statusCode)
39
            ->withData($data)
40
            ->withStatusCode($statusCode);
41
    }
42
43
    /**
44
     * @param  int  $statusCode
45
     * @return $this
46
     */
47
    protected function getResponseHandler($statusCode)
48
    {
49
        if ($statusCode >= 500) {
50
            return $this->getErrorResponseHandler();
51
        }
52
53
        if ($statusCode >= 400) {
54
            return $this->getFailResponseHandler();
55
        }
56
57
        return $this->getSuccessResponseHandler();
58
    }
59
60
    /**
61
     * @return \RichToms\LaravelJSend\Responses\Response
62
     */
63
    protected function getErrorResponseHandler()
64
    {
65
        return new Responses\ErrorResponse;
66
    }
67
68
    /**
69
     * @return \RichToms\LaravelJSend\Responses\Response
70
     */
71
    protected function getFailResponseHandler()
72
    {
73
        return new Responses\FailResponse;
74
    }
75
76
    /**
77
     * @return \RichToms\LaravelJSend\Responses\Response
78
     */
79
    protected function getSuccessResponseHandler()
80
    {
81
        return new Responses\SuccessResponse;
82
    }
83
}
84