PartiesTopic   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 164
Duplicated Lines 23.78 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 17
c 0
b 0
f 0
lcom 1
cbo 5
dl 39
loc 164
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A onSubscribe() 0 4 1
A onPublish() 0 4 1
A onPartyAvailable() 0 13 2
A onPartyGone() 13 13 2
A onSlotJoin() 0 13 2
A onPartyStarted() 13 13 2
A onPartyEnded() 13 13 2
A onUnSubscribe() 0 4 1
A souldBroadcastForGame() 0 7 2
A getSubscribedEvents() 0 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Eole\Websocket\Topic;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Ratchet\Wamp\WampConnection;
7
use Eole\Sandstone\Websocket\Topic;
8
use Eole\Core\Model\Party;
9
use Eole\Core\Event\PartyEvent;
10
use Eole\Core\Event\SlotEvent;
11
12
class PartiesTopic extends Topic implements EventSubscriberInterface
13
{
14
    /**
15
     * @var null|string
16
     */
17
    private $gameName;
18
19
    /**
20
     * @param string $topicPath
21
     * @param array $arguments
22
     */
23
    public function __construct($topicPath, array $arguments = array())
24
    {
25
        parent::__construct($topicPath, $arguments);
26
27
        $this->gameName = $arguments['game_name'];
28
    }
29
30
    /**
31
     * {@InheritDoc}
32
     */
33
    public function onSubscribe(WampConnection $conn, $topic)
34
    {
35
        parent::onSubscribe($conn, $topic);
36
    }
37
38
    /**
39
     * {@InheritDoc}
40
     */
41
    public function onPublish(WampConnection $conn, $topic, $event)
42
    {
43
        // noop
44
    }
45
46
    /**
47
     * @param PartyEvent $event
48
     */
49
    public function onPartyAvailable(PartyEvent $event)
50
    {
51
        $party = $event->getParty();
52
53
        if (!$this->souldBroadcastForGame($party)) {
54
            return;
55
        }
56
57
        $this->broadcast([
58
            'type' => 'created',
59
            'party' => $party,
60
        ]);
61
    }
62
63
    /**
64
     * @param PartyEvent $event
65
     */
66 View Code Duplication
    public function onPartyGone(PartyEvent $event)
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...
67
    {
68
        $party = $event->getParty();
69
70
        if (!$this->souldBroadcastForGame($party)) {
71
            return;
72
        }
73
74
        $this->broadcast([
75
            'type' => 'gone',
76
            'party' => $party,
77
        ]);
78
    }
79
80
    /**
81
     * @param SlotEvent $event
82
     */
83
    public function onSlotJoin(SlotEvent $event)
84
    {
85
        $party = $event->getParty();
86
87
        if (!$this->souldBroadcastForGame($party)) {
88
            return;
89
        }
90
91
        $this->broadcast([
92
            'type' => 'slot_join',
93
            'event' => $event,
94
        ]);
95
    }
96
97
    /**
98
     * @param PartyEvent $event
99
     */
100 View Code Duplication
    public function onPartyStarted(PartyEvent $event)
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...
101
    {
102
        $party = $event->getParty();
103
104
        if (!$this->souldBroadcastForGame($party)) {
105
            return;
106
        }
107
108
        $this->broadcast([
109
            'type' => 'party_started',
110
            'event' => $event,
111
        ]);
112
    }
113
114
    /**
115
     * @param PartyEvent $event
116
     */
117 View Code Duplication
    public function onPartyEnded(PartyEvent $event)
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...
118
    {
119
        $party = $event->getParty();
120
121
        if (!$this->souldBroadcastForGame($party)) {
122
            return;
123
        }
124
125
        $this->broadcast([
126
            'type' => 'party_ended',
127
            'event' => $event,
128
        ]);
129
    }
130
131
    /**
132
     * {@InheritDoc}
133
     */
134
    public function onUnSubscribe(WampConnection $conn, $topic)
135
    {
136
        parent::onUnSubscribe($conn, $topic);
137
    }
138
139
    /**
140
     * Returns whether it should broadcast event
141
     * depending on the current game name.
142
     *
143
     * @param Party $party
144
     *
145
     * @return bool
146
     */
147
    private function souldBroadcastForGame(Party $party)
148
    {
149
        return
150
            (null === $this->gameName) ||
151
            ($party->getGame()->getName() === $this->gameName)
152
        ;
153
    }
154
155
    /**
156
     * {@InheritDoc}
157
     */
158
    public static function getSubscribedEvents()
159
    {
160
        return array(
161
            PartyEvent::CREATE_AFTER => array(
162
                array('onPartyAvailable'),
163
            ),
164
            SlotEvent::JOIN_AFTER => array(
165
                array('onSlotJoin'),
166
            ),
167
            PartyEvent::STARTED => array(
168
                array('onPartyStarted'),
169
            ),
170
            PartyEvent::ENDED => array(
171
                array('onPartyEnded'),
172
            ),
173
        );
174
    }
175
}
176