VoteService::update()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 28

Duplication

Lines 18
Ratio 64.29 %

Code Coverage

Tests 24
CRAP Score 5

Importance

Changes 0
Metric Value
dl 18
loc 28
ccs 24
cts 24
cp 1
rs 9.1608
c 0
b 0
f 0
cc 5
nc 5
nop 0
crap 5
1
<?php
2
3
namespace eXpansion\Bundle\VoteManager\Services;
4
5
use eXpansion\Bundle\VoteManager\Plugins\Votes\AbstractVotePlugin;
6
use eXpansion\Bundle\VoteManager\Structures\Vote;
7
use eXpansion\Framework\Core\Helpers\ChatNotification;
8
use eXpansion\Framework\Core\Services\Application\Dispatcher;
9
use eXpansion\Framework\Core\Services\Console;
10
use eXpansion\Framework\Core\Services\DedicatedConnection\Factory;
11
use eXpansion\Framework\Core\Storage\Data\Player;
12
use Maniaplanet\DedicatedServer\Connection;
13
14
class VoteService
15
{
16
    /** @var Console */
17
    protected $console;
18
19
    /** @var Factory */
20
    protected $factory;
21
22
    /** @var ChatNotification */
23
    protected $chatNotification;
24
25
    /** @var Dispatcher */
26
    protected $dispatcher;
27
28
    /** @var AbstractVotePlugin[] */
29
    protected $votePlugins = [];
30
31
    /** @var array mapping between native MP votes and equivalent expansion votes. */
32
    protected $voteMapping = [];
33
34
    /** @var AbstractVotePlugin */
35
    protected $currentVote = null;
36
37
    /**
38
     * VoteService constructor.
39
     *
40
     * @param Console $console
41
     * @param Factory $factory
42
     * @param ChatNotification $chatNotification
43
     * @param Dispatcher $dispatcher
44
     * @param $voteFactories
45
     */
46 10
    public function __construct(
47
        Console $console,
48
        Factory $factory,
49
        ChatNotification $chatNotification,
50
        Dispatcher $dispatcher,
51
        $voteFactories
52
    ) {
53 10
        $this->console = $console;
54 10
        $this->factory = $factory;
55 10
        $this->dispatcher = $dispatcher;
56 10
        $this->chatNotification = $chatNotification;
57
58 10
        foreach ($voteFactories as $voteFactory) {
59 10
            $this->votePlugins[$voteFactory->getCode()] = $voteFactory;
60
61 10
            foreach ($voteFactory->getReplacementTypes() as $replaces) {
62 10
                $this->voteMapping[$replaces] = $voteFactory->getCode();
63
            }
64
        }
65 10
    }
66
67
    /**
68
     * Reset ongoing vote.
69
     */
70 4
    public function reset()
71
    {
72 4
        if ($this->currentVote) {
73 1
            $this->currentVote->reset();
74
        }
75 4
        $this->currentVote = null;
76 4
    }
77
78
    /**
79
     * Cast vote
80
     *
81
     * @param string $login
82
     * @param string $type
83
     */
84 2
    public function castVote($login, $type)
85
    {
86 2
        if ($this->currentVote instanceof AbstractVotePlugin) {
87
            switch ($type) {
88 2
                case Vote::VOTE_YES:
89 1
                    $this->currentVote->castYes($login);
90 1
                    break;
91 1
                case Vote::VOTE_NO:
92 1
                    $this->currentVote->castNo($login);
93 1
                    break;
94
            }
95
        }
96 2
    }
97
98
    /**
99
     * Update the status of the vote.
100
     */
101 4
    public function update()
102
    {
103 4
        if ($this->currentVote) {
104 4
            $vote = $this->currentVote->getCurrentVote();
105 4
            $this->currentVote->update(time());
106
107 4
            switch ($vote->getStatus()) {
108 4 View Code Duplication
                case Vote::STATUS_CANCEL:
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...
109 1
                    $this->dispatcher->dispatch("votemanager.votecancelled",
110 1
                        [$vote->getPlayer(), $vote->getType(), $vote]);
111 1
                    $this->currentVote = null;
112 1
                    $this->reset();
113 1
                    break;
114 3 View Code Duplication
                case Vote::STATUS_FAILED:
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...
115 1
                    $this->dispatcher->dispatch("votemanager.votefailed",
116 1
                        [$vote->getPlayer(), $vote->getType(), $vote]);
117 1
                    $this->currentVote = null;
118 1
                    $this->reset();
119 1
                    break;
120 2 View Code Duplication
                case Vote::STATUS_PASSED:
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...
121 1
                    $this->dispatcher->dispatch("votemanager.votepassed",
122 1
                        [$vote->getPlayer(), $vote->getType(), $vote]);
123 1
                    $this->currentVote = null;
124 1
                    $this->reset();
125 1
                    break;
126
            }
127
        }
128 4
    }
129
130
    /**
131
     * Cancel ongoing vote.
132
     */
133 1
    public function cancel()
134
    {
135 1
        if (!$this->currentVote) {
136 1
            return;
137
        }
138
139 1
        $this->currentVote->cancel();
140 1
        $this->update();
141 1
    }
142
143
    /**
144
     * Pass ongoing vote.
145
     */
146
    public function pass()
147
    {
148
        if (!$this->currentVote) {
149
            return;
150
        }
151
152
        $this->currentVote->pass();
153
        $this->update();
154
    }
155
156
157
    /**
158
     * @return AbstractVotePlugin
159
     */
160 10
    public function getCurrentVote()
161
    {
162 10
        return $this->currentVote;
163
    }
164
165
    /**
166
     * Start a vote.
167
     *
168
     * @param Player $player
169
     * @param string $typeCode
170
     * @param array  $params
171
     */
172 10
    public function startVote(Player $player, $typeCode, $params)
173
    {
174 10
        if ($this->getCurrentVote() !== null) {
175 1
            $this->chatNotification->sendMessage("expansion_votemanager.error.in_progress");
176 1
            return;
177
        }
178
179 10
        if (isset($this->voteMapping[$typeCode])) {
180 9
            $typeCode = $this->voteMapping[$typeCode];
181
        }
182
183 10
        if (!isset($this->votePlugins[$typeCode])) {
184
            // no vote-plugin found for native vote, so return silently
185 1
            return;
186
        }
187
188 9
        $this->currentVote = $this->votePlugins[$typeCode];
189 9
        $this->currentVote->start($player, $params);
190 9
        $this->factory->getConnection()->cancelVote();
191
192 9
        $this->dispatcher->dispatch(
193 9
            "votemanager.votenew",
194 9
            [$player, $this->currentVote->getCode(), $this->currentVote->getCurrentVote()]
195
        );
196 9
    }
197
}
198