ApiResponse   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setStatusCode() 0 7 2
A setErrors() 0 10 3
A render() 0 10 1
A send() 0 7 1
1
<?php
2
/**
3
 * Api Response
4
 *
5
 * @package     erdiko/core
6
 * @copyright   2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com
7
 * @author      John Arroyo <[email protected]>
8
 */
9
namespace erdiko\core;
10
11
12
class ApiResponse extends Response
13
{
14
15
    /**
16
     * _statusCode
17
     *
18
     * Unless explicitly set, default to a 200 status
19
     * assuming everything went ok.
20
     */
21
    protected $_statusCode = 200;
22
23
    /**
24
     * _errors
25
     *
26
     *
27
     */
28
    protected $_errors = false;
29
30
    /**
31
     * Theme
32
     */
33
    protected $_theme;
34
35
    /**
36
     * Content
37
     */
38
    protected $_content = null;
39
40
41
42
    /**
43
     * setStatusCode
44
     *
45
     * Set, and send, the HTTP status code.
46
     */
47
    public function setStatusCode($code = null)
48
    {
49
        if (!empty($code)) {
50
            $this->_statusCode = $code;
51
            http_response_code($this->_statusCode); // this sends the http status code
52
        }
53
    }
54
55
    public function setErrors($errors = null)
56
    {
57
        if (!empty($errors)) {
58
            if (!is_array($errors)) {
59
                $errors = array($errors);
60
            }
61
62
            $this->_errors = $errors;
0 ignored issues
show
Documentation Bug introduced by
It seems like $errors of type array is incompatible with the declared type boolean of property $_errors.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
        }
64
    }
65
66
    /**
67
     * Ajax render function
68
     * @todo add support for xml
69
     *
70
     * @return string
71
     */
72
    public function render()
73
    {
74
        $responseData = array(
75
            "status" => $this->_statusCode,
76
            "body"   => $this->_content,
77
            "errors" => $this->_errors
78
        );
79
80
        return json_encode($responseData);
81
    }
82
83
    /**
84
     * Render and send data to browser then end request
85
     *
86
     * @todo allow switching between json and xml
87
     * @note This should only be called at the very end of processing the response
88
     */
89
    public function send()
90
    {
91
        // set the mime type to JSON
92
        header('Content-Type: application/json');
93
94
        echo $this->render();
95
    }
96
}
97