Completed
Push — master ( 869006...91820d )
by Timo
22s
created

MonitorTrait::isMetaSubscribed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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 React\Promise\Promise;
16
use Tidal\WampWatch\ClientSessionInterface as ClientSession;
17
use Tidal\WampWatch\Subscription\Collection as SubscriptionCollection;
18
19
/**
20
 * Description of MonitorTrait.
21
 *
22
 * @author Timo
23
 */
24
trait MonitorTrait
25
{
26
    use EventEmitterTrait;
27
28
    /**
29
     * The monitor's WAMP client session.
30
     *
31
     * @var ClientSession
32
     */
33
    protected $session;
34
35
    /**
36
     * if the monitor is running.
37
     *
38
     * @var bool
39
     */
40
    protected $isRunning = false;
41
42
    /**
43
     * @var SubscriptionCollection collection for meta subscriptions
44
     */
45
    protected $metaSubscriptionCollection;
46
47
    /**
48
     * @var string
49
     */
50
    protected $initialCallProcedure;
51
52
    /**
53
     * @var callable
54
     */
55
    protected $initialCallCallback;
56
57
    /**
58
     * @var bool
59
     */
60
    protected $initialCallDone = false;
61
62
    /**
63
     * @param ClientSession $session
64
     */
65
    protected function setClientSession(ClientSession $session)
66
    {
67
        $this->session = $session;
68
    }
69
70
    /**
71
     * Start the monitor.
72
     *
73
     * @return bool
74
     */
75
    public function start()
76
    {
77
        $promises = [
78
            $this->getMetaSubscriptionCollection()->subscribe(),
79
            $this->callInitialProcedure(),
80
        ];
81
82
        \React\Promise\all($promises)->done(function () {
83
            $this->isRunning = true;
84
            $this->emit('start', [$this->getList()]);
85
        });
86
87
        return true;
88
    }
89
90
    /**
91
     * Stop the monitor.
92
     * Returns boolean if the monitor could be started.
93
     *
94
     * @return bool
95
     */
96
    public function stop()
97
    {
98
        if (!$this->isRunning()) {
99
            return false;
100
        }
101
102
        $this->getMetaSubscriptionCollection()->unsubscribe();
103
104
        $this->isRunning = false;
105
        $this->emit('stop', [$this]);
106
107
        return true;
108
    }
109
110
    protected function getList()
111
    {
112
        return [];
113
    }
114
115
    /**
116
     * Get the monitor's WAMP client session.
117
     *
118
     * @return ClientSession
119
     */
120
    public function getServerSession()
121
    {
122
        return $this->session;
123
    }
124
125
    /**
126
     * Get the monitor's running state.
127
     *
128
     * @return bool
129
     */
130
    public function isRunning()
131
    {
132
        return $this->isRunning;
133
    }
134
135
    /**
136
     * @param \Tidal\WampWatch\Subscription\Collection $collection
137
     */
138
    public function setMetaSubscriptionCollection(SubscriptionCollection $collection)
139
    {
140
        $this->metaSubscriptionCollection = $collection;
141
    }
142
143
    /**
144
     * @return \Tidal\WampWatch\Subscription\Collection
145
     */
146
    public function getMetaSubscriptionCollection()
147
    {
148
        return isset($this->metaSubscriptionCollection)
149
            ? $this->metaSubscriptionCollection
150
            : $this->metaSubscriptionCollection = new SubscriptionCollection($this->session);
151
    }
152
153
    protected function setInitialCall($procedure, callable $callback)
154
    {
155
        $this->initialCallProcedure = (string) $procedure;
156
        $this->initialCallCallback = $callback;
157
    }
158
159
    /**
160
     * @return \React\Promise\Promise
161
     */
162
    protected function callInitialProcedure()
163
    {
164
        if (!isset($this->initialCallProcedure) || !isset($this->initialCallCallback)) {
165
            $this->initialCallDone = true;
166
167
            return new  Promise(function (callable $resolve) {
168
                $resolve();
169
            });
170
        }
171
172
        return $this->session->call($this->initialCallProcedure, [])->then(function ($res) {
173
            $this->initialCallDone = true;
174
            $cb = $this->initialCallCallback;
175
            $cb($res);
176
177
            return $res;
178
        },
179
            $this->getErrorCallback()
180
        );
181
    }
182
183
    private function getErrorCallback()
184
    {
185
        return function ($error) {
186
            $this->emit('error', [$error]);
187
188
            return $error;
189
        };
190
    }
191
}
192