Response   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 111
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __destruct() 0 7 1
A __invoke() 0 4 1
A call() 0 4 1
B send() 0 31 3
1
<?php
2
3
namespace Dazzle\Channel\Extra;
4
5
use Dazzle\Promise\Promise;
6
use Dazzle\Promise\PromiseInterface;
7
use Dazzle\Channel\Protocol\ProtocolInterface;
8
use Dazzle\Channel\Channel;
9
use Dazzle\Channel\ChannelInterface;
10
use Error;
11
use Exception;
12
13
class Response
14
{
15
    /**
16
     * @var ChannelInterface
17
     */
18
    protected $channel;
19
20
    /**
21
     * @var ProtocolInterface
22
     */
23
    protected $protocol;
24
25
    /**
26
     * @var string|string[]|Error|Exception
27
     */
28
    protected $message;
29
30
    /**
31
     * @var mixed[]
32
     */
33
    protected $params;
34
35
    /**
36
     * @param ChannelInterface $channel
37
     * @param ProtocolInterface $protocol
38
     * @param string|string[]|Error|Exception $message
39
     * @param mixed[] $params
40
     */
41 6
    public function __construct($channel, $protocol, $message, $params = [])
42
    {
43 6
        $this->channel = $channel;
44 6
        $this->protocol = $protocol;
45 6
        $this->message = $message;
46 6
        $this->params = $params;
47 6
    }
48
49
    /**
50
     *
51
     */
52 2
    public function __destruct()
53
    {
54 2
        unset($this->channel);
55 2
        unset($this->protocol);
56 2
        unset($this->message);
57 2
        unset($this->params);
58 2
    }
59
60
    /**
61
     * Send the prepared response.
62
     *
63
     * @return PromiseInterface
64
     */
65 1
    public function __invoke()
66
    {
67 1
        return $this->send(new Promise());
68
    }
69
70
    /**
71
     * Send the prepared response.
72
     *
73
     * @return PromiseInterface
74
     * @resolves mixed
75
     * @rejects Error|Exception|string|null
76
     * @cancels Error|Exception|string|null
77
     */
78 1
    public function call()
79
    {
80 1
        return $this->send(new Promise());
81
    }
82
83
    /**
84
     * Send the request using passed Promise.
85
     *
86
     * @param PromiseInterface $promise
87
     * @return PromiseInterface
88
     * @resolves mixed
89
     * @rejects Error|Exception|string|null
90
     * @cancels Error|Exception|string|null
91
     */
92 2
    protected function send(PromiseInterface $promise)
93
    {
94 2
        $pid     = $this->protocol->getPid();
95 2
        $origin  = $this->protocol->getOrigin();
96 2
        $message = $this->message;
97 2
        $channel = $this->channel;
98
99 2
        if ($message instanceof Error || $message instanceof Exception)
100
        {
101
            $answer = $channel
102 1
                ->createProtocol($message->getMessage())
103 1
                ->setPid($pid, true)
104 1
                ->setException(get_class($message), true)
105
            ;
106
        }
107
        else
108
        {
109
            $answer = $channel
110 1
                ->createProtocol($message)
111 1
                ->setPid($pid, true)
112
            ;
113
        }
114
115 2
        $this->channel->send(
116 2
            $origin,
117 2
            $answer,
118 2
            Channel::MODE_BUFFER_ONLINE
119
        );
120
121 2
        return $promise->resolve();
122
    }
123
}
124