Passed
Push — master ( d5b9c5...238a05 )
by Timo
26s
created

ClientSession::call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Tidal/WampWatch package.
5
 *   (c) 2016 Timo Michna <timomichna/yahoo.de>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 *
10
 */
11
12
namespace Tidal\WampWatch\Adapter\Thruway;
13
14
use Thruway\ClientSession as ThruwaySession;
15
use Tidal\WampWatch\ClientSessionInterface;
16
use Tidal\WampWatch\Adapter\React\PromiseAdapter;
17
use Tidal\WampWatch\Behavior\Async\MakesPromisesTrait;
18
use Tidal\WampWatch\Adapter\React\PromiseFactory;
19
20
class ClientSession implements ClientSessionInterface
21
{
22
    use MakesPromisesTrait;
23
24
    /**
25
     * @var ThruwaySession
26
     */
27
    protected $thruwaySession;
28
29
    /**
30
     * ClientSession constructor.
31
     *
32
     * @param ThruwaySession $session
33
     * @param PromiseFactory $factory
34
     */
35
    public function __construct(ThruwaySession $session, PromiseFactory $factory)
36
    {
37
        $this->setThruwaySession($session);
38
        $this->setPromiseFactory($factory);
39
    }
40
41
    /**
42
     * @param ThruwaySession $session
43
     */
44
    public function setThruwaySession(ThruwaySession $session)
45
    {
46
        $this->thruwaySession = $session;
47
    }
48
49
    /**
50
     * Subscribe.
51
     *
52
     * @param string   $topicName
53
     * @param callable $callback
54
     * @param          $options   array
55
     *
56
     * @return PromiseAdapter
57
     */
58
    public function subscribe($topicName, callable $callback, $options = null)
59
    {
60
        return $this->callAdaptee('subscribe', [$topicName, $callback, $options]);
61
    }
62
63
    /**
64
     * Publish.
65
     *
66
     * @param string      $topicName
67
     * @param array|mixed $arguments
68
     * @param array|mixed $argumentsKw
69
     * @param array|mixed $options
70
     *
71
     * @return PromiseAdapter
72
     */
73
    public function publish($topicName, $arguments = null, $argumentsKw = null, $options = null)
74
    {
75
        return $this->callAdaptee('publish', [$topicName, $arguments, $argumentsKw, $options]);
76
    }
77
78
    /**
79
     * Register.
80
     *
81
     * @param string      $procedureName
82
     * @param callable    $callback
83
     * @param array|mixed $options
84
     *
85
     * @return PromiseAdapter
86
     */
87
    public function register($procedureName, callable $callback, $options = null)
88
    {
89
        return $this->callAdaptee('register', [$procedureName, $callback, $options]);
90
    }
91
92
    /**
93
     * Unregister.
94
     *
95
     * @param string $procedureName
96
     *
97
     * @return PromiseAdapter
98
     */
99
    public function unregister($procedureName)
100
    {
101
        return $this->callAdaptee('unregister', [$procedureName]);
102
    }
103
104
    /**
105
     * Call.
106
     *
107
     * @param string      $procedureName
108
     * @param array|mixed $arguments
109
     * @param array|mixed $argumentsKw
110
     * @param array|mixed $options
111
     *
112
     * @return PromiseAdapter
113
     */
114
    public function call($procedureName, $arguments = null, $argumentsKw = null, $options = null)
115
    {
116
        return $this->callAdaptee('call', [$procedureName, $arguments, $argumentsKw, $options]);
117
    }
118
119
    /**
120
     * @param int $sessionId
121
     */
122
    public function setSessionId($sessionId)
123
    {
124
        $this->thruwaySession->setSessionId($sessionId);
125
    }
126
127
    /**
128
     * @return int the Session Id
129
     */
130
    public function getSessionId()
131
    {
132
        return $this->thruwaySession->getSessionId();
133
    }
134
135
    /**
136
     * @param $msg
137
     */
138
    public function sendMessage($msg)
139
    {
140
        $this->thruwaySession->sendMessage($msg);
141
    }
142
143
    private function callAdaptee($command, array $arguments = [])
144
    {
145
        return $this->promiseFactory->createFromAdaptee(
146
            call_user_func_array([
147
                $this->thruwaySession,
148
                $command,
149
            ], $arguments)
150
        );
151
    }
152
}
153