Completed
Push — master ( 47c10a...c931cd )
by De Cramer
13s
created

VoteService::reset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
cc 2
eloc 4
nc 2
nop 0
crap 2
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\Storage\Data\Player;
11
use Maniaplanet\DedicatedServer\Connection;
12
13
class VoteService
14
{
15
    /** @var Console */
16
    protected $console;
17
18
    /** @var Connection */
19
    protected $connection;
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
     * VoteManager constructor.
38
     * @param Console $console
39
     * @param Connection $connection
40
     * @param ChatNotification $chatNotification
41
     * @param Dispatcher $dispatcher
42
     * @param AbstractVotePlugin[] $voteFactories
43
     */
44 10
    public function __construct(
45
        Console $console,
46
        Connection $connection,
47
        ChatNotification $chatNotification,
48
        Dispatcher $dispatcher,
49
        $voteFactories
50
    ) {
51 10
        $this->console = $console;
52 10
        $this->connection = $connection;
53 10
        $this->dispatcher = $dispatcher;
54 10
        $this->chatNotification = $chatNotification;
55
56 10
        foreach ($voteFactories as $voteFactory) {
57 10
            $this->votePlugins[$voteFactory->getCode()] = $voteFactory;
58
59 10
            foreach ($voteFactory->getReplacementTypes() as $replaces) {
60 10
                $this->voteMapping[$replaces] = $voteFactory->getCode();
61
            }
62
        }
63 10
    }
64
65
    /**
66
     * Reset ongoing vote.
67
     */
68 4
    public function reset()
69
    {
70 4
        if ($this->currentVote) {
71 1
            $this->currentVote->reset();
72
        }
73 4
        $this->currentVote = null;
74 4
    }
75
76
    /**
77
     * Cast vote
78
     *
79
     * @param string $login
80
     * @param string $type
81
     */
82 2
    public function castVote($login, $type)
83
    {
84 2
        if ($this->currentVote instanceof AbstractVotePlugin) {
85
            switch ($type) {
86 2
                case Vote::VOTE_YES:
87 1
                    $this->currentVote->castYes($login);
88 1
                    break;
89 1
                case Vote::VOTE_NO:
90 1
                    $this->currentVote->castNo($login);
91 1
                    break;
92
            }
93
        }
94 2
    }
95
96
    /**
97
     * Update the status of the vote.
98
     */
99 4
    public function update()
100
    {
101 4
        if ($this->currentVote) {
102 4
            $vote = $this->currentVote->getCurrentVote();
103 4
            $this->currentVote->update(time());
104
105 4
            switch ($vote->getStatus()) {
106 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...
107 1
                    $this->dispatcher->dispatch("votemanager.votecancelled",
108 1
                        [$vote->getPlayer(), $vote->getType(), $vote]);
109 1
                    $this->currentVote = null;
110 1
                    $this->reset();
111 1
                    break;
112 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...
113 1
                    $this->dispatcher->dispatch("votemanager.votefailed",
114 1
                        [$vote->getPlayer(), $vote->getType(), $vote]);
115 1
                    $this->currentVote = null;
116 1
                    $this->reset();
117 1
                    break;
118 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...
119 1
                    $this->dispatcher->dispatch("votemanager.votepassed",
120 1
                        [$vote->getPlayer(), $vote->getType(), $vote]);
121 1
                    $this->currentVote = null;
122 1
                    $this->reset();
123 1
                    break;
124
            }
125
        }
126 4
    }
127
128
    /**
129
     * Cancel ongoing vote.
130
     */
131 1
    public function cancel()
132
    {
133 1
        if (!$this->currentVote) {
134 1
            return;
135
        }
136
137 1
        $this->currentVote->cancel();
138 1
        $this->update();
139 1
    }
140
141
    /**
142
     * @return AbstractVotePlugin
143
     */
144 10
    public function getCurrentVote()
145
    {
146 10
        return $this->currentVote;
147
    }
148
149
    /**
150
     * Start a vote.
151
     *
152
     * @param Player $player
153
     * @param string $typeCode
154
     * @param array  $params
155
     */
156 10
    public function startVote(Player $player, $typeCode, $params)
157
    {
158 10
        if ($this->getCurrentVote() !== null) {
159 1
            $this->chatNotification->sendMessage("expansion_votemanager.error.in_progress");
160 1
            return;
161
        }
162
163 10
        if (isset($this->voteMapping[$typeCode])) {
164 9
            $typeCode = $this->voteMapping[$typeCode];
165
        }
166
167 10
        if (!isset($this->votePlugins[$typeCode])) {
168 1
            $this->chatNotification->sendMessage("|error| Unknown vote type : $typeCode");
169 1
            return;
170
        }
171
172 9
        $this->currentVote = $this->votePlugins[$typeCode];
173 9
        $this->currentVote->start($player, $params);
174 9
        $this->connection->cancelVote();
175
176 9
        $this->dispatcher->dispatch(
177 9
            "votemanager.votenew",
178 9
            [$player, $this->currentVote->getCode(), $this->currentVote->getCurrentVote()]
179
        );
180 9
    }
181
}
182