Server::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Toolshedr;
4
5
use Toolshedr\Core\{
6
    Headers,
7
    Response,
8
    Request
9
};
10
11
final class Server
12
{
13
    /**
14
     * @var Headers 
15
     */
16
    private $headers;
17
18
    /**
19
     * @var Request
20
     */
21
    private $request;
22
23
    /**
24
     * @var Response
25
     */
26
    private $response;
27
28
    /**
29
     * Server constructor.
30
     */
31 4
    public function __construct()
32
    {
33 4
        $this->headers = new Headers();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Toolshedr\Core\Headers() of type object<Toolshedr\Core\Headers> is incompatible with the declared type object<Headers> of property $headers.

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...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
34 4
        $this->request = new Request();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Toolshedr\Core\Request() of type object<Toolshedr\Core\Request> is incompatible with the declared type object<Request> of property $request.

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...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
35 4
        $this->response = new Response();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Toolshedr\Core\Response() of type object<Toolshedr\Core\Response> is incompatible with the declared type object<Response> of property $response.

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...
36 4
    }
37
38
    /**
39
     * Whitelist your UI
40
     *
41
     * @param $origin
42
     * @return void
43
     */
44 1
    public function whitelist(string $origin)
45
    {
46 1
        $this->headers->addToWhitelist($origin);
47 1
    }
48
49
    /**
50
     * Is Domain whitelisted
51
     * 
52
     * @param string $origin
53
     * @return bool
54
     */
55 1
    public function isWhitelisted(string $origin)
56
    {
57 1
        return $this->headers->isWhitelisted($origin);
58
    }
59
60
    /**
61
     * Chack is API KEY valid
62
     * 
63
     * @param string $apy_key
0 ignored issues
show
Bug introduced by
There is no parameter named $apy_key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
64
     * @return mixed
65
     */
66 1
    public function isValidApiKey(string $api_key)
67
    {
68 1
        return $this->request->isValidApiKey($api_key);
69
    }
70
    
71
    /**
72
     * Set API KEY
73
     * 
74
     * @param string $api_key
75
     * @return void
76
     */
77 1
    public function apikey(string $api_key)
78
    {
79 1
        $this->request->setApiKey($api_key);
80 1
    }
81
    
82
    /**
83
     * Run the server
84
     *
85
     * @return void
86
     */
87
    public function run()
88
    {
89
        /**
90
         * Check are request headers ok
91
         */
92
        if ($this->headers->areOk() && !$this->request->isOptionRequest()) {
93
            $this->response->handle($this->request, $this->headers);
94
        } else {
95
            $this->response->options($this->headers);
96
        }
97
98
        $this->headers->send();
99
        
100
        print json_encode($this->response->output(), JSON_PRETTY_PRINT);
101
        
102
        // We are done
103
        exit();
104
    }
105
}
106