Request::setApiKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * ******************************************************************
4
 * Created by   Marko Kungla on 09 Oct 2016
5
 * @package     toolshedr
6
 * Encoding     UTF-8
7
 * File         Request.php
8
 * Code format  PSR-2 and 12
9
 * *******************************************************************/
10
11
namespace Toolshedr\Core;
12
13
14
class Request
15
{
16
    private $api_key;
0 ignored issues
show
Unused Code introduced by
The property $api_key is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
    /**
19
     * Request constructor.
20
     */
21 4
    public function __construct()
22
    {
23 4
        $this->api_keys = array();
0 ignored issues
show
Bug introduced by
The property api_keys does not seem to exist. Did you mean api_key?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
24
25 4
    }
26
27
    /**
28
     * @inheritdoc
29
     * 
30
     * @param string $api_key
31
     */
32 1
    public function setApiKey(string $api_key)
33
    {
34 1
        array_push($this->api_keys, $api_key);
0 ignored issues
show
Bug introduced by
The property api_keys does not seem to exist. Did you mean api_key?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
35 1
    }
36
37
    /**
38
     * Authenticate the request
39
     * 
40
     * @return bool
41
     */
42
    public function authenticate()
43
    {
44
        return $this->checkApiKey();
45
        
46
    }
47
48
    /**
49
     * Is OPTIONS request
50
     * 
51
     * @return bool
52
     */
53
    public function isOptionRequest()
0 ignored issues
show
Coding Style introduced by
isOptionRequest 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...
54
    {
55
        return $_SERVER['REQUEST_METHOD'] === 'OPTIONS';
56
    }
57
58
    /**
59
     * 
60
     * @param string $api_key
61
     * @return bool
62
     */
63 1
    public function isValidApiKey(string $api_key)
64
    {
65 1
        return in_array($api_key, $this->api_keys);
0 ignored issues
show
Bug introduced by
The property api_keys does not seem to exist. Did you mean api_key?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
66
    }
67
    
68
    /**
69
     * Is recieved API KEY valid
70
     * 
71
     * @return bool
72
     */
73
    private function checkApiKey()
0 ignored issues
show
Coding Style introduced by
checkApiKey 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...
74
    {
75
        return isset($_SERVER['HTTP_X_TOOLSHEDR_API_KEY']) && $this->isValidApiKey($api_key);
0 ignored issues
show
Bug introduced by
The variable $api_key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
76
    }
77
}
78