MonitorTrait::setInitialCall()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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;
13
14
use Evenement\EventEmitterTrait;
15
use Tidal\WampWatch\ClientSessionInterface as ClientSession;
16
use Tidal\WampWatch\Subscription\Collection as SubscriptionCollection;
17
use Tidal\WampWatch\Async\PromiseInterface;
18
use Tidal\WampWatch\Behavior\Async\MakesPromisesTrait;
19
use Tidal\WampWatch\Behavior\Async\MakesDeferredPromisesTrait;
20
21
/**
22
 * Trait MonitorTrait.
23
 */
24
trait MonitorTrait
25
{
26
    use EventEmitterTrait,
27
        MakesPromisesTrait,
28
        MakesDeferredPromisesTrait;
29
30
    /**
31
     * The monitor's WAMP client session.
32
     *
33
     * @var ClientSession
34
     */
35
    protected $session;
36
37
    /**
38
     * if the monitor is running.
39
     *
40
     * @var bool
41
     */
42
    protected $isRunning = false;
43
44
    /**
45
     * @var SubscriptionCollection collection for meta subscriptions
46
     */
47
    protected $metaSubscriptionCollection;
48
49
    /**
50
     * @var string
51
     */
52
    protected $initialCallProcedure;
53
54
    /**
55
     * @var callable
56
     */
57
    protected $initialCallCallback;
58
59
    /**
60
     * @var bool
61
     */
62
    protected $initialCallDone = false;
63
64
    /**
65
     * @param ClientSession $session
66
     */
67
    protected function setClientSession(ClientSession $session)
68
    {
69
        $this->session = $session;
70
    }
71
72
    /**
73
     * Start the monitor.
74
     *
75
     * @return bool
76
     */
77
    public function start()
78
    {
79
        $promises = [
80
            $this->getMetaSubscriptionCollection()->subscribe(),
81
            $this->callInitialProcedure(),
82
        ];
83
84
        \React\Promise\all($promises)->done(function () {
85
            $this->isRunning = true;
86
            $this->emit('start', [$this->getList()]);
87
        });
88
89
        return true;
90
    }
91
92
    /**
93
     * Stop the monitor.
94
     * Returns boolean if the monitor could be started.
95
     *
96
     * @return bool
97
     */
98
    public function stop()
99
    {
100
        if (!$this->isRunning()) {
101
            return false;
102
        }
103
104
        $this->getMetaSubscriptionCollection()->unsubscribe();
105
106
        $this->isRunning = false;
107
        $this->emit('stop', [$this]);
108
109
        return true;
110
    }
111
112
    protected function getList()
113
    {
114
        return [];
115
    }
116
117
    /**
118
     * Get the monitor's WAMP client session.
119
     *
120
     * @return ClientSession
121
     */
122
    public function getServerSession()
123
    {
124
        return $this->session;
125
    }
126
127
    /**
128
     * Get the monitor's running state.
129
     *
130
     * @return bool
131
     */
132
    public function isRunning()
133
    {
134
        return $this->isRunning;
135
    }
136
137
    /**
138
     * @param \Tidal\WampWatch\Subscription\Collection $collection
139
     */
140
    public function setMetaSubscriptionCollection(SubscriptionCollection $collection)
141
    {
142
        $this->metaSubscriptionCollection = $collection;
143
    }
144
145
    /**
146
     * @return \Tidal\WampWatch\Subscription\Collection
147
     */
148
    public function getMetaSubscriptionCollection()
149
    {
150
        return isset($this->metaSubscriptionCollection)
151
            ? $this->metaSubscriptionCollection
152
            : $this->metaSubscriptionCollection = new SubscriptionCollection($this->session);
153
    }
154
155
    protected function setInitialCall($procedure, callable $callback)
156
    {
157
        $this->initialCallProcedure = (string) $procedure;
158
        $this->initialCallCallback = $callback;
159
    }
160
161
    /**
162
     * @return PromiseInterface
163
     */
164
    protected function callInitialProcedure()
165
    {
166
        if (!isset($this->initialCallProcedure) || !isset($this->initialCallCallback)) {
167
            $this->initialCallDone = true;
168
169
            return $this->createPromise(function (callable $resolve) {
170
                $resolve();
171
            });
172
        }
173
174
        return $this->session->call($this->initialCallProcedure, [])->then(function ($res) {
175
            $this->initialCallDone = true;
176
            $cb = $this->initialCallCallback;
177
            $cb($res);
178
179
            return $res;
180
        },
181
            $this->getErrorCallback()
182
        );
183
    }
184
185
    private function getErrorCallback()
186
    {
187
        return function ($error) {
188
            $this->emit('error', [$error]);
189
190
            return $error;
191
        };
192
    }
193
194
    private function retrieveCallData($procedure, callable $filter = null, $arguments = [])
195
    {
196
        $deferred = $this->createDeferred();
197
198
        $filter = $filter ?: function ($res) {
199
            return $res;
200
        };
201
202
        $this->session->call($procedure, $arguments)
203
            ->then(
204
                function ($res) use ($deferred, $filter) {
205
                    $deferred->resolve($filter($res));
206
                },
207
                $this->getErrorCallback()
208
            );
209
210
        return $deferred->promise();
211
    }
212
213
    /**
214
     * @param $event
215
     *
216
     * @return \Closure
217
     */
218
    private function getSubscriptionHandler($event)
219
    {
220
        return function ($res) use ($event) {
221
            $this->emit($event, $res);
222
        };
223
    }
224
225
    protected function getSubscriptionIdRetrievalCallback()
226
    {
227
        return function (\Thruway\CallResult $res) {
228
            /** @var \Thruway\Message\ResultMessage $message */
229
            $message = $res->getResultMessage();
230
            $list = $message->getArguments()[0];
231
            $this->setList($list);
232
            $this->emit('list', [
233
                $this->subscriptionIds->exact,
0 ignored issues
show
Bug introduced by
The property subscriptionIds does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
234
                $this->subscriptionIds->prefix,
235
                $this->subscriptionIds->wildcard,
236
            ]);
237
238
            return $list;
239
        };
240
    }
241
242
    abstract protected function setList($list);
243
}
244