HttpServer   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 9
dl 0
loc 153
ccs 45
cts 50
cp 0.9
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A __destruct() 0 5 1
A getDriver() 0 4 1
A setComponent() 0 4 2
A getComponent() 0 4 1
A handleConnect() 0 6 1
A handleDisconnect() 0 7 2
B handleMessage() 0 31 5
A handleError() 0 11 2
A close() 0 7 1
1
<?php
2
3
namespace Dazzle\Http\Http;
4
5
use Dazzle\Http\NetworkComponentAwareInterface;
6
use Dazzle\Http\Http\Driver\HttpDriver;
7
use Dazzle\Http\Http\Driver\HttpDriverInterface;
8
use Dazzle\Http\Null\NullServer;
9
use Dazzle\Http\NetworkMessageInterface;
10
use Dazzle\Http\NetworkComponentInterface;
11
use Dazzle\Http\NetworkConnectionInterface;
12
use Dazzle\Util\Buffer\Buffer;
13
use Error;
14
use Exception;
15
16
class HttpServer implements HttpServerInterface, NetworkComponentAwareInterface
17
{
18
    /**
19
     * @var NetworkComponentInterface
20
     */
21
    protected $httpServer;
22
23
    /**
24
     * @var HttpDriverInterface
25
     */
26
    protected $httpDriver;
27
28
    /**
29
     * @param NetworkComponentAwareInterface|null $aware
30
     * @param NetworkComponentInterface|null $component
31
     */
32 65
    public function __construct(NetworkComponentAwareInterface $aware = null, NetworkComponentInterface $component = null)
33
    {
34 65
        $this->httpServer = $component;
35 65
        $this->httpDriver = new HttpDriver();
36
37 65
        if ($aware !== null)
38
        {
39 65
            $aware->setComponent($this);
40
        }
41 65
    }
42
43
    /**
44
     *
45
     */
46
    public function __destruct()
47
    {
48
        unset($this->httpServer);
49
        unset($this->httpDriver);
50
    }
51
52
    /**
53
     * @override
54
     * @inheritDoc
55
     */
56 1
    public function getDriver()
57
    {
58 1
        return $this->httpDriver;
59
    }
60
61
    /**
62
     * @override
63
     * @inheritDoc
64
     */
65 23
    public function setComponent(NetworkComponentInterface $component = null)
66
    {
67 23
        $this->httpServer = $component === null ? new NullServer() : $component;
68 23
    }
69
70
    /**
71
     * @override
72
     * @inheritDoc
73
     */
74 3
    public function getComponent()
75
    {
76 3
        return $this->httpServer;
77
    }
78
79
    /**
80
     * @override
81
     * @inheritDoc
82
     */
83 4
    public function handleConnect(NetworkConnectionInterface $conn)
84
    {
85 4
        $conn->httpBuffer = new Buffer();
0 ignored issues
show
Bug introduced by
Accessing httpBuffer on the interface Dazzle\Http\NetworkConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
86 4
        $conn->httpHeadersReceived = false;
0 ignored issues
show
Bug introduced by
Accessing httpHeadersReceived on the interface Dazzle\Http\NetworkConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
87 4
        $conn->httpRequest = null;
0 ignored issues
show
Bug introduced by
Accessing httpRequest on the interface Dazzle\Http\NetworkConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
88 4
    }
89
90
    /**
91
     * @override
92
     * @inheritDoc
93
     */
94 5
    public function handleDisconnect(NetworkConnectionInterface $conn)
95
    {
96 5
        if ($conn->httpHeadersReceived)
0 ignored issues
show
Bug introduced by
Accessing httpHeadersReceived on the interface Dazzle\Http\NetworkConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
97
        {
98 4
            $this->httpServer->handleDisconnect($conn);
99
        }
100 5
    }
101
102
    /**
103
     * @override
104
     * @inheritDoc
105
     */
106 6
    public function handleMessage(NetworkConnectionInterface $conn, NetworkMessageInterface $message)
107
    {
108 6
        if ($conn->httpHeadersReceived !== true)
0 ignored issues
show
Bug introduced by
Accessing httpHeadersReceived on the interface Dazzle\Http\NetworkConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
109
        {
110
            try
111
            {
112 5
                if (($request = $this->httpDriver->readRequest($conn->httpBuffer, $message->read())) === null)
0 ignored issues
show
Bug introduced by
Accessing httpBuffer on the interface Dazzle\Http\NetworkConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
113
                {
114 4
                    return;
115
                }
116
            }
117 1
            catch (Error $ex)
118
            {
119
                return $this->close($conn, 413);
120
            }
121 1
            catch (Exception $ex)
122
            {
123 1
                return $this->close($conn, 413);
124
            }
125
126 4
            $conn->httpHeadersReceived = true;
0 ignored issues
show
Bug introduced by
Accessing httpHeadersReceived on the interface Dazzle\Http\NetworkConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
127 4
            $conn->httpRequest = $request;
0 ignored issues
show
Bug introduced by
Accessing httpRequest on the interface Dazzle\Http\NetworkConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
128
129 4
            $this->httpServer->handleConnect($conn);
130 4
            $this->httpServer->handleMessage($conn, $request);
0 ignored issues
show
Documentation introduced by
$request is of type object<Dazzle\Http\Http\HttpRequestInterface>, but the function expects a object<Dazzle\Http\NetworkMessageInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131
        }
132
        else
133
        {
134 1
            $this->httpServer->handleMessage($conn, $message);
135
        }
136 5
    }
137
138
    /**
139
     * @override
140
     * @inheritDoc
141
     */
142 2
    public function handleError(NetworkConnectionInterface $conn, $ex)
143
    {
144 2
        if ($conn->httpHeadersReceived)
0 ignored issues
show
Bug introduced by
Accessing httpHeadersReceived on the interface Dazzle\Http\NetworkConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
145
        {
146 1
            $this->httpServer->handleError($conn, $ex);
147
        }
148
        else
149
        {
150 1
            $this->close($conn, 500);
151
        }
152 2
    }
153
154
    /**
155
     * Close a connection with an HTTP response.
156
     *
157
     * @param NetworkConnectionInterface $conn
158
     * @param int $code
159
     * @return null
160
     */
161 1
    protected function close(NetworkConnectionInterface $conn, $code = 400)
162
    {
163 1
        $response = new HttpResponse($code);
164
165 1
        $conn->send($response);
166 1
        $conn->close();
167 1
    }
168
}
169