Rest::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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 	./Rest/Platforms/Controllers/Rest.php
9
 */
10
 
11
namespace JaegerApp\Platforms\Controllers;
12
13
/**
14
 * Jaeger - REST Base Controller
15
 *
16
 * Contains the global REST methods
17
 *
18
 * @package Rest\Authentication
19
 * @author Eric Lamb <[email protected]>
20
 */
21
class Rest
22
{
23
    /**
24
     * The JSON body payload sent with requests
25
     * @var array
26
     */
27
    protected $body_data = null;
28
    
29
    /**
30
     * The Rest object
31
     * @var \JaegerApp\Rest
32
     */
33
    protected $rest = null;
34
35
    /**
36
     * The abstracted platform object
37
     * 
38
     * @var \JaegerApp\Platforms\Eecms
39
     */
40
    protected $platform = null;
41
    
42
    /**
43
     * Sets the Rest object
44
     * @param \JaegerApp\Rest $rest
45
     * @return \JaegerApp\Platforms\Controllers\Rest
46
     */
47
    public function setRest(\JaegerApp\Rest $rest)
48
    {
49
        $this->rest = $rest;
50
        return $this;
51
    }
52
    
53
    /**
54
     * Returns the Rest object
55
     * @return \JaegerApp\Rest
56
     */
57
    public function getRest()
58
    {
59
        return $this->rest;
60
    }
61
    
62
    /**
63
     * Sets the Platform object
64
     * @param \JaegerApp\Platforms\AbstractPlatform $platform
65
     * @return \JaegerApp\Platforms\Controllers\Rest
66
     */
67
    public function setPlatform(\JaegerApp\Platforms\AbstractPlatform $platform)
68
    {
69
        $this->platform = $platform;
0 ignored issues
show
Documentation Bug introduced by
It seems like $platform of type object<JaegerApp\Platforms\AbstractPlatform> is incompatible with the declared type object<JaegerApp\Platforms\Eecms> of property $platform.

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...
70
        return $this;
71
    }
72
    
73
    /**
74
     * Returns the Platform object
75
     * @return \JaegerApp\Platforms\Eecms
76
     */
77
    public function getPlatform()
78
    {
79
        return $this->platform;
80
    }
81
    
82
    /**
83
     * Authenticates the request
84
     * @return boolean
85
     */
86
    public function authenticate()
0 ignored issues
show
Coding Style introduced by
authenticate 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...
87
    {
88
        $hmac = $this->getRest()->getServer()->getHmac();
89
        $data = array_merge($this->getRequestHeaders(true), $this->getBodyData());
90
        $auth = $hmac->setData($data)
91
                     ->setRoute($this->getPlatform()->getPost('api_method'))
92
                     ->setMethod($_SERVER['REQUEST_METHOD'])
93
                     ->auth($this->settings['api_key'], $this->settings['api_secret']);
0 ignored issues
show
Bug introduced by
The property settings 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...
94
        
95
        if(!$auth) {
96
            return false;
97
        }
98
        
99
        return true;
100
    }
101
    
102
    /**
103
     * Returns the input data as an array
104
     * @return array
105
     */
106
    public function getBodyData()
107
    {
108
        if(is_null($this->body_data)) 
109
        {
110
            $data = json_decode(file_get_contents("php://input"), true);
111
            if(!$data)
112
            {
113
                $data = array();
114
            }
115
            
116
            $this->body_data = $data;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data of type * is incompatible with the declared type array of property $body_data.

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...
117
        }
118
        
119
        return $this->body_data;
120
    }
121
    
122
    /**
123
     * Returns an associative array of the request headers
124
     * @return multitype:unknown
0 ignored issues
show
Documentation introduced by
The doc-type multitype:unknown could not be parsed: Unknown type name "multitype:unknown" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
125
     */
126
    public function getRequestHeaders($auth = true)
127
    {
128
        $headers = \getallheaders();
129
        if($auth) {
130
            $hmac = $this->getRest()->getServer()->getHmac();
131
            $return = array(
132
                $hmac->getPrefix().'timestamp' => (isset($headers[$hmac->getPrefix().'timestamp']) ? $headers[$hmac->getPrefix().'timestamp'] : ''),
133
                $hmac->getPrefix().'signature' => (isset($headers[$hmac->getPrefix().'signature']) ? $headers[$hmac->getPrefix().'signature'] : ''),
134
                $hmac->getPrefix().'key' => (isset($headers[$hmac->getPrefix().'key']) ? $headers[$hmac->getPrefix().'key'] : ''),
135
                $hmac->getPrefix().'version' => (isset($headers[$hmac->getPrefix().'version']) ? $headers[$hmac->getPrefix().'version'] : ''),
136
            );
137
            $headers = $return;
138
        }
139
        
140
        return $headers;
141
    }
142
    
143
    /**
144
     * Handy little method to disable unused HTTP verb methods
145
     *
146
     * @return ApiProblem
147
     */
148
    protected function methodNotAllowed()
149
    {
150
        return $this->view_helper->renderError(405, 'method_not_allowed');
0 ignored issues
show
Bug introduced by
The property view_helper 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...
151
    }
152
    
153
    public function options($id = false)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
154
    {
155
        return;
156
    }
157
    
158
    public function post()
159
    {
160
        return $this->methodNotAllowed();
161
    }
162
    
163
    public function create()
164
    {
165
        return $this->methodNotAllowed();
166
    }
167
    
168
    public function delete($id = false)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
169
    {
170
        return $this->methodNotAllowed();
171
    }
172
    
173
    public function get($id = false)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
174
    {
175
        return $this->methodNotAllowed();
176
    }
177
    
178
    public function head($id = null)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
179
    {
180
        return $this->methodNotAllowed();
181
    }
182
    
183
    public function patch($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
184
    {
185
        return $this->methodNotAllowed();
186
    }
187
    
188
    public function put($id = false)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
189
    {
190
        return $this->methodNotAllowed();
191
    }
192
}