Completed
Push — master ( 2651dd...a129fb )
by Timo
23s
created

MonitorTrait::callInitialProcedure()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 8
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->isRunning = true;
78
        $this->emit('start', [$this->getList()]);
79
80
        return true;
81
    }
82
83
    /**
84
     * Stop the monitor.
85
     * Returns boolean if the monitor could be started.
86
     *
87
     * @return bool
88
     */
89
    public function stop()
90
    {
91
        if (!$this->isRunning()) {
92
            return false;
93
        }
94
95
        $this->getSubscriptionCollection()->unsubscribe();
96
97
        $this->isRunning = false;
98
        $this->emit('stop', [$this]);
99
100
        return true;
101
    }
102
103
    protected function getList()
104
    {
105
        return [];
106
    }
107
108
    /**
109
     * Get the monitor's WAMP client session.
110
     *
111
     * @return ClientSession
112
     */
113
    public function getServerSession()
114
    {
115
        return $this->session;
116
    }
117
118
    /**
119
     * Get the monitor's running state.
120
     *
121
     * @return bool
122
     */
123
    public function isRunning()
124
    {
125
        return $this->isRunning;
126
    }
127
128
    /**
129
     * @return \Tidal\WampWatch\Subscription\Collection
130
     */
131
    protected function getSubscriptionCollection()
132
    {
133
        return isset($this->subscriptionCollection)
134
            ? $this->subscriptionCollection
135
            : $this->subscriptionCollection = new SubscriptionCollection($this->session);
136
    }
137
138
    protected function setInitialCall($pocedure, callable $callback)
139
    {
140
        $this->initialCallProcedure = (string) $pocedure;
141
        $this->initialCallCallback = $callback;
142
    }
143
144
    /**
145
     * @return \React\Promise\PromiseInterface
146
     */
147
    protected function callInitialProcedure()
148
    {
149
        if (!isset($this->initialCallProcedure) || !isset($this->initialCallCallback)) {
150
            $resolver = function (callable $resolve) {
151
                $resolve();
152
            };
153
154
            return new  Promise($resolver);
155
        }
156
157
        return $this->session->call($this->initialCallProcedure)->then(function ($res) {
158
            $this->initialCallDone = true;
159
160
            return $res;
161
        });
162
    }
163
164
    /**
165
     * Checks if all necessary subscriptions and calls have been responded to.
166
     */
167
    protected function checkStarted()
168
    {
169
        if ($this->getSubscriptionCollection()->isSubscribed() &&
170
            $this->initialCallDone &&
171
            !$this->isRunning()
172
        ) {
173
            $this->doStart();
1 ignored issue
show
Bug introduced by
It seems like doStart() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
174
        }
175
    }
176
}
177