Completed
Push — master ( cec12b...b38e85 )
by Timo
24s
created

MonitorTrait::getErrorCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
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
        $this->getMetaSubscriptionCollection()->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->getMetaSubscriptionCollection()->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
     * @param \Tidal\WampWatch\Subscription\Collection $collection
134
     */
135
    public function setMetaSubscriptionCollection(SubscriptionCollection $collection)
136
    {
137
        $this->metaSubscriptionCollection = $collection;
138
    }
139
140
    /**
141
     * @return \Tidal\WampWatch\Subscription\Collection
142
     */
143
    public function getMetaSubscriptionCollection()
144
    {
145
        return isset($this->metaSubscriptionCollection)
146
            ? $this->metaSubscriptionCollection
147
            : $this->metaSubscriptionCollection = new SubscriptionCollection($this->session);
148
    }
149
150
    protected function setInitialCall($procedure, callable $callback)
151
    {
152
        $this->initialCallProcedure = (string) $procedure;
153
        $this->initialCallCallback = $callback;
154
    }
155
156
    /**
157
     * @return \React\Promise\Promise
158
     */
159
    protected function callInitialProcedure()
160
    {
161
        if (!isset($this->initialCallProcedure) || !isset($this->initialCallCallback)) {
162
            $this->initialCallDone = true;
163
            $resolver = function (callable $resolve) {
164
                $resolve();
165
            };
166
167
            return new  Promise($resolver);
168
        }
169
170
        return $this->session->call($this->initialCallProcedure, [])->then(function ($res) {
171
            $this->initialCallDone = true;
172
            $cb = $this->initialCallCallback;
173
            $cb($res);
174
175
            return $res;
176
        },
177
            $this->getErrorCallback()
178
        );
179
    }
180
181
    /**
182
     * Checks if all necessary subscriptions and calls have been responded to.
183
     */
184
    protected function checkStarted()
185
    {
186
        if ($this->isMetaSubscribed() &&
187
            $this->initialCallDone &&
188
            !$this->isRunning()
189
        ) {
190
            $this->isRunning = true;
191
            $this->emit('start', [$this->getList()]);
192
        }
193
    }
194
195
    protected function isMetaSubscribed()
196
    {
197
        if (!$this->getMetaSubscriptionCollection()->hasSubscription()) {
198
            return true;
199
        }
200
201
        return $this->getMetaSubscriptionCollection()->isSubscribed();
202
    }
203
204
    private function getErrorCallback()
205
    {
206
        return function ($error) {
207
            $this->emit('error', [$error]);
208
209
            return $error;
210
        };
211
    }
212
}
213