Completed
Pull Request — master (#307)
by
unknown
03:53
created

VoteService::startVote()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0378

Importance

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