AjaxResponse   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 7
c 6
b 0
f 0
lcom 1
cbo 1
dl 0
loc 83
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
 * AjaxResponse
4
 *
5
 * @package     erdiko/core
6
 * @copyright   2012-2017 Arroyo Labs, Inc. http://www.arroyolabs.com
7
 * @author      John Arroyo <[email protected]>
8
 * @author      Andy Armstrong <[email protected]>
9
 */
10
namespace erdiko\core;
11
12
13
class AjaxResponse extends Response
14
{
15
16
    /**
17
     * _statusCode
18
     *
19
     * Unless explicitly set, default to a 200 status
20
     * assuming everything went ok.
21
     */
22
    protected $_statusCode = 200;
23
24
    /**
25
     * _errors
26
     *
27
     *
28
     */
29
    protected $_errors = false;
30
31
    /**
32
     * Theme
33
     */
34
    protected $_theme;
35
36
    /**
37
     * Content
38
     */
39
    protected $_content = null;
40
41
42
43
    /**
44
     * setStatusCode
45
     *
46
     * Set, and send, the HTTP status code.
47
     */
48
    public function setStatusCode($code = null)
49
    {
50
        if (!empty($code)) {
51
            $this->_statusCode = $code;
52
            http_response_code($this->_statusCode); // this sends the http status code
53
        }
54
    }
55
56
    public function setErrors($errors = null)
57
    {
58
        if (!empty($errors)) {
59
            if (!is_array($errors)) {
60
                $errors = array($errors);
61
            }
62
63
            $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...
64
        }
65
    }
66
67
    /**
68
     * Ajax render function
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
     * @note This should only be called at the very end of processing the response
87
     */
88
    public function send()
89
    {
90
        // set the mime type to JSON
91
        header('Content-Type: application/json');
92
93
        echo $this->render();
94
    }
95
}
96