Rest::getSystemErrors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Jaeger
4
 *
5
 * @copyright	Copyright (c) 2015-2016, mithra62
6
 * @link		http://jaeger-app.com
7
 * @version		1.0
8
 * @filesource 	./Platforms/View/Rest.php
9
 */
10
namespace JaegerApp\Platforms\View;
11
12
use Nocarrier\Hal;
13
use Crell\ApiProblem\ApiProblem;
14
15
/**
16
 * Jaeger - Rest View Object
17
 *
18
 * Contains the view helpers for Rest Requests
19
 *
20
 * @package Rest\View
21
 * @author Eric Lamb <[email protected]>
22
 */
23
class Rest extends AbstractView
24
{
25
    /**
26
     * The Platform object
27
     * @var \mithra62\Platforms\AbstractPlatform
28
     */
29
    protected $platform = null;
30
    
31
    /**
32
     * The System errors 
33
     * @var array
34
     */
35
    protected $system_errors = array();
36
    
37
    public function setSystemErrors(array $errors = array())
38
    {
39
        $this->system_errors = $errors;
40
        return $this;
41
    }
42
    
43
    /**
44
     * Returns any set system errors
45
     * @return array
46
     */
47
    public function getSystemErrors()
48
    {
49
        return $this->system_errors;
50
    }
51
    
52
    /**
53
     * Returns an instance of the Hal object for use
54
     * @param string $route
55
     * @return Hal
56
     */
57
    public function getHal($route, array $item = array())
58
    {
59
        return new Hal($route, $item);
60
    }
61
    
62
    /**
63
     * Returns an instance of ApiProblem object
64
     * @param string $title
65
     * @param string $type
66
     * @return \Crell\ApiProblem\ApiProblem
67
     */
68
    public function getApiProblem($title, $type)
69
    {
70
        return new ApiProblem($this->m62Lang($title), $type);
71
    }
72
73
74
    /**
75
     * Returns the data for output and sets the appropriate headers
76
     * @param \Nocarrier\Hal $hal
77
     * @return string
78
     */
79
    public function renderOutput(\Nocarrier\Hal $hal)
0 ignored issues
show
Coding Style introduced by
renderOutput uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
80
    {
81
        $this->sendHeaders();
0 ignored issues
show
Bug introduced by
The method sendHeaders() does not seem to exist on object<JaegerApp\Platforms\View\Rest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
        if($this->getSystemErrors())
83
        {
84
            $system_errors = array();
85
            foreach($this->getSystemErrors() As $key => $value) {
86
                $system_errors[$key] = $this->m62Lang($key);
87
            }
88
            $hal->setData($hal->getData() + array('_system_errors' => $system_errors));
89
    
90
        }
91 View Code Duplication
        if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos(strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'xml') !== false)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
92
        {
93
            header('Content-Type: application/hal+xml');
94
            return $hal->asXml(true);
95
        }
96
        else
97
        {
98
            header('Content-Type: application/hal+json');
99
            return $hal->asJson(true);
100
        }
101
    }
102
    
103
    /**
104
     * Wrapper to handle error output
105
     *
106
     * Note that $detail should be a key for language translation
107
     *
108
     * @param int $code The HTTP response code to send
109
     * @param string $title The title to display
110
     * @param array $errors Any errors to explain what went wrong
111
     * @param string $detail A human readable explanation of what happened
112
     * @param string $type A URI resource to deaper explanations on what happened
113
     */
114
    public function renderError($code, $title, array $errors = array(), $detail = null, $type = null)
0 ignored issues
show
Coding Style introduced by
renderError uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
115
    {
116
        http_response_code($code);
117
    
118
        $problem = $this->getApiProblem($title, $type);
119
        $problem->setStatus($code);
120
        $problem->setDetail($detail);
121
        if($errors)
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
122
        {
123
            $problem['errors'] = $errors;
124
        }
125
    
126
        $this->sendHeaders();
0 ignored issues
show
Bug introduced by
The method sendHeaders() does not seem to exist on object<JaegerApp\Platforms\View\Rest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127 View Code Duplication
        if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos(strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'xml') !== false)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
128
        {
129
            header('Content-Type: application/problem+xml');
130
            return $problem->asXml(true);
131
        }
132
        else
133
        {
134
            header('Content-Type: application/problem+json');
135
            return $problem->asJson(true);
136
        }
137
    }
138
}