Completed
Branch master (8e085e)
by Timo
02:38
created

MonitorTrait::isSubscribed()   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
44
     */
45
    protected $subscriptionCollection;
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
        $this->getSubscriptionCollection()->subscribe()->done(function () {
78
            $this->checkStarted();
79
        });
80
        $this->callInitialProcedure()->done(function () {
81
            $this->checkStarted();
82
        });
83
84
        return true;
85
    }
86
87
    /**
88
     * Stop the monitor.
89
     * Returns boolean if the monitor could be started.
90
     *
91
     * @return bool
92
     */
93
    public function stop()
94
    {
95
        if (!$this->isRunning()) {
96
            return false;
97
        }
98
99
        $this->getSubscriptionCollection()->unsubscribe();
100
101
        $this->isRunning = false;
102
        $this->emit('stop', [$this]);
103
104
        return true;
105
    }
106
107
    protected function getList()
108
    {
109
        return [];
110
    }
111
112
    /**
113
     * Get the monitor's WAMP client session.
114
     *
115
     * @return ClientSession
116
     */
117
    public function getServerSession()
118
    {
119
        return $this->session;
120
    }
121
122
    /**
123
     * Get the monitor's running state.
124
     *
125
     * @return bool
126
     */
127
    public function isRunning()
128
    {
129
        return $this->isRunning;
130
    }
131
132
    /**
133
     * @return \Tidal\WampWatch\Subscription\Collection
134
     */
135
    protected function getSubscriptionCollection()
136
    {
137
        return isset($this->subscriptionCollection)
138
            ? $this->subscriptionCollection
139
            : $this->subscriptionCollection = new SubscriptionCollection($this->session);
140
    }
141
142
    protected function setInitialCall($pocedure, callable $callback)
143
    {
144
        $this->initialCallProcedure = (string) $pocedure;
145
        $this->initialCallCallback = $callback;
146
    }
147
148
    /**
149
     * @return \React\Promise\Promise
150
     */
151
    protected function callInitialProcedure()
152
    {
153
        if (!isset($this->initialCallProcedure) || !isset($this->initialCallCallback)) {
154
            $this->initialCallDone = true;
155
            $resolver = function (callable $resolve) {
156
                $resolve();
157
            };
158
159
            return new  Promise($resolver);
160
        }
161
162
        return $this->session->call($this->initialCallProcedure, [])->then(function ($res) {
163
            $this->initialCallDone = true;
164
            $cb = $this->initialCallCallback;
165
            $cb($res);
166
167
            return $res;
168
        });
169
    }
170
171
    /**
172
     * Checks if all necessary subscriptions and calls have been responded to.
173
     */
174
    protected function checkStarted()
175
    {
176
        if ($this->isSubscribed() &&
177
            $this->initialCallDone &&
178
            !$this->isRunning()
179
        ) {
180
            $this->isRunning = true;
181
            $this->emit('start', [$this->getList()]);
182
        }
183
    }
184
185
    protected function isSubscribed()
186
    {
187
        if (!$this->getSubscriptionCollection()->hasSubscription()) {
188
            return true;
189
        }
190
191
        return $this->getSubscriptionCollection()->isSubscribed();
192
    }
193
}
194