Completed
Push — master ( 6d66c7...e2b06f )
by Timo
02:45
created

SubscriptionMonitor::getSessionIds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
namespace Phaim\Server\Wamp\Monitor;
3
4
/*
5
 * Copyright 2015 Timo.
6
 *
7
 * For the full copyright and license information, please view the LICENSE file
8
 * that was distributed with this source code.
9
 */
10
use Phaim\Server\Wamp\Util;
11
12
/**
13
 * Description of SessionMonitor
14
 *
15
 * @author Timo
16
 */
17
class SubscriptionMonitor  
18
    implements 
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
19
        MonitorInterface {
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces before interface name; 8 found
Loading history...
20
   
21
    use MonitorTrait {
22
        start as doStart;
23
        stop as doStop;
24
    }
25
26
    const SUBSCRIPTION_CREATE_TOPIC   = 'wamp.subscription.on_create';
27
    const SUBSCRIPTION_SUB_TOPIC      = 'wamp.subscription.on_subscribe';
28
    const SUBSCRIPTION_UNSUB_TOPIC    = 'wamp.subscription.on_unsubscribe';
29
    const SUBSCRIPTION_DELETE_TOPIC   = 'wamp.subscription.on_delete';
30
    const SUBSCRIPTION_LIST_TOPIC     = 'wamp.subscription.list';
31
    const SUBSCRIPTION_LOOKUP_TOPIC   = 'wamp.subscription.lookup';
32
    const SUBSCRIPTION_MATCH_TOPIC    = 'wamp.subscription.match';
33
    const SUBSCRIPTION_GET_TOPIC      = 'wamp.subscription.get';
34
    const SUBSCRIPTION_SUBLIST_TOPIC  = 'wamp.subscription.list_subscribers';
35
    const SUBSCRIPTION_SUBCOUNT_TOPIC = 'wamp.subscription.count_subscribers';
36
    
37
    const LOOKUP_MATCH_WILDCARD       = 'wildcard';
38
    const LOOKUP_MATCH_PREFIX         = 'prefix';
39
40
    protected $sessionIds = [];
41
    protected $subscriptionIds = [];
42
    
43
    public function start() {
44
        $this->once('list', function(){
45
            $this->startSubscriptions();
46
            $this->doStart();
47
        });
48
        $this->retrieveSubscriptionIds();
49
    }
50
51
    public function stop() {
52
        $this->stopSubscriptions();
53
        $this->doStart();
54
    }
55
    
56
    public function getSessionInfo($sessionId, callable $callback) {
57
        return $this->session->call(self::SUBSCRIPTION_INFO_TOPIC, $sessionId)->then(
58
            function ($res)use($callback){
59
                $this->emit('info', $res);
60
                $callback($res[0]);
61
            },
62
            function ($error) {
63
                $this->emit('error', [$error]);
64
            }
65
        );    
66
    }
67
    
68
    public function getSessionIds(callable $callback) {
69
        if(!count($this->subscriptionIds)){
70
            $this->retrieveSubscriptionIds($callback);
71
        }else{
72
            $callback($this->subscriptionIds);
73
        }
74
    }
75
    
76
    protected function startSubscriptions() {
77
        $this->session->subscribe(self::SUBSCRIPTION_CREATE_TOPIC, function($res){
78
            return $this->createHandler($res);
79
        });
80
        $this->session->subscribe(self::SUBSCRIPTION_DELETE_TOPIC, function($res){
81
            return $this->deleteHandler($res);
82
        });
83
        $this->session->subscribe(self::SUBSCRIPTION_SUB_TOPIC, function($res){
84
            return $this->subHandler($res);
85
        });
86
    }
87
    
88
    protected function createHandler($res) {
89
        $sessionInfo = $res[0];
90
        $sessionId = $sessionInfo['session'];
91 View Code Duplication
        if(($key = array_search($sessionId, $this->subscriptionIds)) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
92
            $this->subscriptionIds[] = $sessionId;
93
            $this->emit('create', [$sessionInfo]);
94
        }
95
    }
96
    
97
    protected function deleteHandler($res) {
98
        $sessionId = $res[0];
99 View Code Duplication
        if(($key = array_search($sessionId, $this->subscriptionIds)) !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
100
            unset($this->subscriptionIds[$key]);
101
            $this->emit('delete', [$sessionId]);
102
        }
103
    }
104
    
105
    protected function subHandler($res) {
106
        $sessionId = $res[0];
107
        $subId = $res[1];
108
        $this->getSubscriptionDetail($subId)->then(
109
            function ($res)use($sessionId, $subId){    
110
                $this->emit('sub', [$sessionId, $subId, $res[0]]);            
111
            },
112
            function ($error) {                
0 ignored issues
show
Unused Code introduced by
The parameter $error is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
                $this->emit('sub', [$sessionId, $subId, [
0 ignored issues
show
Bug introduced by
The variable $sessionId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $subId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
114
                    'id'        => $subId,
115
                    'created'   => null,
116
                    'uri'       => null,
117
                    'match'     => null
118
                ]]);
119
            }
120
        );    
121
    }
122
123
124
    protected function stopSubscriptions() {       
125
        Util::unsubscribe($this->session, self::SUBSCRIPTION_JOIN_TOPIC);
126
        Util::unsubscribe($this->session, self::SUBSCRIPTION_LEAVE_TOPIC);
127
    }
128
    
129
    public function getSubscriptionDetail($subId) {
130
        return $this->session->call(self::SUBSCRIPTION_GET_TOPIC, [$subId])->then(
131
            function ($res){
0 ignored issues
show
Unused Code introduced by
The parameter $res is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
                    
133
            },
134
            function ($error) {
0 ignored issues
show
Unused Code introduced by
The parameter $error is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
              
136
            }
137
        );
138
    }
139
    
140 View Code Duplication
    protected function retrieveSubscriptionIds($callback = null) {
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...
141
        return $this->session->call(self::SUBSCRIPTION_LIST_TOPIC, [])->then(
142
            function ($res)use($callback){
143
                $this->subscriptionIds = $res[0];
144
                $this->emit('list', [$this->subscriptionIds]);
145
                if($callback){
146
                    $callback($this->subscriptionIds);
147
                }
148
            },
149
            function ($error) {
150
                $this->emit('error', [$error]);
151
            }
152
        );  
153
        
154
    }
155
    
156
    protected function getList() {
157
        return $this->subscriptionIds;
158
    }
159
    
160
}
161