Passed
Pull Request — master (#62)
by Timo
07:26
created

SubscriptionMonitor::getSubscriptionIdRetrievalCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
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 Tidal\WampWatch\Async\PromiseInterface;
15
use Tidal\WampWatch\ClientSessionInterface as ClientSession;
16
17
/**
18
 * Class SubscriptionMonitor.
19
 */
20
class SubscriptionMonitor implements MonitorInterface
21
{
22
    use MonitorTrait;
23
24
    const SUBSCRIPTION_CREATE_TOPIC = 'wamp.subscription.on_create';
25
    const SUBSCRIPTION_SUB_TOPIC = 'wamp.subscription.on_subscribe';
26
    const SUBSCRIPTION_UNSUB_TOPIC = 'wamp.subscription.on_unsubscribe';
27
    const SUBSCRIPTION_DELETE_TOPIC = 'wamp.subscription.on_delete';
28
    const SUBSCRIPTION_LIST_TOPIC = 'wamp.subscription.list';
29
    const SUBSCRIPTION_LOOKUP_TOPIC = 'wamp.subscription.lookup';
30
    const SUBSCRIPTION_MATCH_TOPIC = 'wamp.subscription.match';
31
    const SUBSCRIPTION_GET_TOPIC = 'wamp.subscription.get';
32
    const SUBSCRIPTION_SUBLIST_TOPIC = 'wamp.subscription.list_subscribers';
33
    const SUBSCRIPTION_SUBCOUNT_TOPIC = 'wamp.subscription.count_subscribers';
34
35
    protected $sessionIds = [];
36
37
    /**
38
     * @var \stdClass Objects withs lists of subscriptions (exact, prefix, wildcard)
39
     */
40
    protected $subscriptionIds = null;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param ClientSession $session
46
     */
47
    public function __construct(ClientSession $session)
48
    {
49
        $this->setClientSession($session);
50
        $this->initSetupCalls();
51
    }
52
53
    /**
54
     * @param string $topic
55
     *
56
     * @return PromiseInterface
57
     */
58
    public function getSubscriptionInfo($topic)
59
    {
60
        return $this->session->call(self::SUBSCRIPTION_GET_TOPIC, [$topic])->then(
61
            function ($res) {
62
                $this->emit('info', [$res]);
63
64
                return $res;
65
            },
66
            $this->getErrorCallback()
67
        );
68
    }
69
70
    public function getSubscriptionIds()
71
    {
72
        if (!isset($this->subscriptionIds)) {
73
            return $this->retrieveSubscriptionIds();
74
        }
75
76
        return $this->createPromiseAdapter(function (callable $resolve) {
77
            $resolve($this->subscriptionIds);
78
        });
79
    }
80
81
    /**
82
     * Initializes the subscription to the meta-events.
83
     */
84 View Code Duplication
    protected function initSetupCalls()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        // @var \Tidal\WampWatch\Subscription\Collection
87
        $collection = $this->getMetaSubscriptionCollection();
88
89
        $collection->addSubscription(self::SUBSCRIPTION_CREATE_TOPIC, $this->getSubscriptionHandler('create'));
90
        $collection->addSubscription(self::SUBSCRIPTION_DELETE_TOPIC, $this->getSubscriptionHandler('delete'));
91
        $collection->addSubscription(self::SUBSCRIPTION_SUB_TOPIC, $this->getSubscriptionHandler('subscribe'));
92
        $collection->addSubscription(self::SUBSCRIPTION_UNSUB_TOPIC, $this->getSubscriptionHandler('unsubscribe'));
93
94
        $this->setInitialCall(self::SUBSCRIPTION_LIST_TOPIC, $this->getSubscriptionIdRetrievalCallback());
95
    }
96
97
    protected function retrieveSubscriptionIds()
98
    {
99
        return $this->retrieveCallData(
100
            self::SUBSCRIPTION_LIST_TOPIC,
101
            $this->getSubscriptionIdRetrievalCallback(),
102
            []
103
        );
104
    }
105
106
    protected function setList($list)
107
    {
108
        $this->subscriptionIds = $list;
109
    }
110
111
    protected function getList()
112
    {
113
        return $this->subscriptionIds;
114
    }
115
}
116