HttpOutput   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 53
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A defaultHeaders() 0 6 1
A writeHead() 0 5 1
A write() 0 11 3
A writeln() 0 7 2
A disconnect() 0 9 2
1
<?php
2
3
namespace Padawan\Framework\Application\Socket;
4
5
use Symfony\Component\Console\Output\OutputInterface;
6
use Symfony\Component\Console\Output\NullOutput;
7
use React\Http\Response;
8
use React\Promise;
9
10
/**
11
 * Class HttpOutput
12
 */
13
class HttpOutput extends NullOutput
14
{
15
    public function __construct(Response $client)
16
    {
17
        $this->client = $client;
18
    }
19
20
    public function defaultHeaders()
21
    {
22
        $this->writeHead(200, [
23
            'Content-Type' => 'application/json'
24
        ]);
25
    }
26
27
    public function writeHead($status, $headers = [])
28
    {
29
        $this->isHeadWritten = true;
30
        return $this->client->writeHead($status, $headers);
31
    }
32
33
    public function write($message, $newline = false, $options = 0)
34
    {
35
        if (!$this->isHeadWritten) {
36
            $this->defaultHeaders();
37
        }
38
        if (is_array($message)) {
39
            $message = implode("\n", $message);
40
        }
41
        $this->client->end($message);
42
        return Promise\resolve();
43
    }
44
45
    public function writeln($message, $options = 0)
46
    {
47
        if (!$this->isHeadWritten) {
48
            $this->defaultHeaders();
49
        }
50
        return $this->client->write($message . "\n");
51
    }
52
53
    public function disconnect()
54
    {
55
        if (!$this->isHeadWritten) {
56
            $this->defaultHeaders();
57
        }
58
        return Promise\resolve()->then(function() {
59
            $this->client->end();
60
        });
61
    }
62
63
    private $client;
64
    private $isHeadWritten = false;
65
}
66