Completed
Pull Request — master (#13)
by Marko
01:45
created

Server   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 73
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A whitelist() 0 4 1
A setApiKey() 0 4 1
A run() 0 18 3
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
    public function __construct()
32
    {
33
        $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
        $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
        $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
    }
37
38
    /**
39
     * Whitelist your UI
40
     *
41
     * @param $origin
42
     * @return void
43
     */
44
    public function whitelist(string $origin)
45
    {
46
        $this->headers->addToWhitelist($origin);
47
    }
48
49
    /**
50
     * Set API KEY
51
     * 
52
     * @param string $api_key
53
     * @return void
54
     */
55
    public function setApiKey(string $api_key)
56
    {
57
        $this->request->setApiKey($api_key);
58
    }
59
    
60
    /**
61
     * Run the server
62
     *
63
     * @return void
64
     */
65
    public function run()
66
    {
67
        /**
68
         * Check are request headers ok
69
         */
70
        if ($this->headers->areOk() && !$this->request->isOptionRequest()) {
71
            $this->response->handle($this->request, $this->headers);
72
        } else {
73
            $this->response->options($this->headers);
74
        }
75
76
        $this->headers->send();
77
        
78
        print json_encode($this->response->output(), JSON_PRETTY_PRINT);
79
        
80
        // We are done
81
        exit();
82
    }
83
}
84