Completed
Branch dev-2.0 (18c912)
by Boudry
03:14
created

VotesManager::preDeletedTask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
    Condorcet PHP - Election manager and results calculator.
4
    Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.
5
6
    By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt)
7
    https://github.com/julien-boudry/Condorcet
8
*/
9
declare(strict_types=1);
10
11
12
namespace CondorcetPHP\Condorcet\DataManager;
13
14
15
use CondorcetPHP\Condorcet\CondorcetException;
16
use CondorcetPHP\Condorcet\Election;
17
use CondorcetPHP\Condorcet\Vote;
18
19
20
class VotesManager extends ArrayManager
21
{
22
23
/////////// Magic ///////////
24
25 145
    public function __construct (Election $election)
26
    {
27 145
        $this->setElection($election);
28
29 145
        parent::__construct();
30 145
    }
31
32 145
    public function setElection (Election $election) : void
33
    {
34 145
        $this->_link[0] = $election;
35 145
    }
36
37 96
    public function getElection () : Election
38
    {
39 96
        return $this->_link[0];
40
    }
41
42
/////////// Data CallBack for external drivers ///////////
43
44 7
    public function getDataContextObject () : DataContextInterface
45
    {
46
        $context = new Class implements DataContextInterface {
47
            public $election;
48
49 5
            public function dataCallBack ($data) : Vote
50
            {
51 5
                $vote = new Vote ($data);
52 5
                $this->election->checkVoteCandidate($vote);
53 5
                $vote->registerLink($this->election);
54
55 5
                return $vote;
56
            }
57
58 6
            public function dataPrepareStoringAndFormat ($data) : string
59
            {
60 6
                $data->destroyLink($this->election);
61
62 6
                return (string) $data;
63
            }
64
        };
65
66 7
        $context->election = $this->_link[0] ?? null;
67
68 7
        return $context;
69
    }
70
71 11
    protected function preDeletedTask ($object) : void
72
    {
73 11
        $object->destroyLink($this->_link[0]);
74 11
    }
75
76
/////////// Array Access - Specials improvements ///////////
77
78 123
    public function offsetSet($offset, $value) : void
79
    {
80 123
        if ($value instanceof Vote) :
81 123
            parent::offsetSet($offset,$value);
82 123
            $this->setStateToVote();
83
        else :
84 1
            throw new CondorcetException (0,'Value must be an instanceof CondorcetPHP\\Vote');
85
        endif;
86 123
    }
87
88 7
    public function offsetUnset($offset) : void
89
    {
90 7
        parent::offsetUnset($offset);
91 7
        $this->setStateToVote();
92 7
    }
93
94
/////////// Internal Election related methods ///////////
95
96 123
    protected function setStateToVote () : void
97
    {
98 123
        foreach ($this->_link as $element) :
99 123
            $element->setStateToVote();
100
        endforeach;
101 123
    }
102
103
/////////// Get Votes Methods ///////////
104
105 6
    public function getVoteKey (Vote $vote) {
106 6
        ($r = array_search($vote, $this->_Container, true)) !== false || ($r = array_search($vote, $this->_Cache, true));
107
108 6
        return $r;
109
    }
110
111 2
    protected function getFullVotesListGenerator () : \Generator
112
    {
113 2
        foreach ($this as $voteKey => $vote) :
114 2
            yield $voteKey => $vote;
115
        endforeach;
116 2
    }
117
118 7
    protected function getPartialVotesListGenerator (array $tag, bool $with) : \Generator
119
    {
120 7
        foreach ($this as $voteKey => $vote) :
121 7
            $noOne = true;
122 7
            foreach ($tag as $oneTag) :
123 7
                if ( ( $oneTag === $voteKey ) || in_array($oneTag, $vote->getTags(),true) ) :
124 7
                    if ($with) :
125 7
                        yield $voteKey => $vote;
126 7
                        break;
127
                    else :
128 7
                        $noOne = false;
129
                    endif;
130
                endif;
131
            endforeach;
132
133 7
            if (!$with && $noOne) :
134 7
                yield $voteKey => $vote;
135
            endif;
136
        endforeach;
137 7
    }
138
139
    // Get the votes list
140 14
    public function getVotesList ($tag = null, bool $with = true) : array
141
    {
142 14
        if ($tag === null) :
143 12
            return $this->getFullDataSet();
144
        else :
145 6
            $search = [];
146
147 6
            foreach ($this->getPartialVotesListGenerator($tag,$with) as $voteKey => $vote) :
148 6
                $search[$voteKey] = $vote;
149
            endforeach;
150
151 6
            return $search;
152
        endif;
153
    }
154
155
    // Get the votes list as a generator object
156 2
    public function getVotesListGenerator ($tag = null, bool $with = true) : \Generator
157
    {
158 2
        if ($tag === null) :
159 2
            return $this->getFullVotesListGenerator();
160
        else :
161 2
            return $this->getPartialVotesListGenerator($tag,$with);
162
        endif;
163
    }
164
165 93
    public function getVotesValidUnderConstraintGenerator () : \Generator
166
    {
167 93
        foreach ($this as $voteKey => $oneVote) :
168 93
            if (!$this->getElection()->testIfVoteIsValidUnderElectionConstraints($oneVote)) :
169 1
                continue;
170
            endif;
171
172 93
            yield $voteKey => $oneVote;
173
        endforeach;
174 93
    }
175
176 5
    public function getVotesListAsString () : string
177
    {
178 5
        $simpleList = '';
179
180 5
        $weight = [];
181 5
        $nb = [];
182
183 5
        foreach ($this as $oneVote) :
184 5
            $oneVoteString = $oneVote->getSimpleRanking($this->_link[0]);
185
186 5
            if(!array_key_exists($oneVoteString, $weight)) :
187 5
                $weight[$oneVoteString] = 0;
188
            endif;
189 5
            if(!array_key_exists($oneVoteString, $nb)) :
190 5
                $nb[$oneVoteString] = 0;
191
            endif;
192
193 5
            if ($this->getElection()->isVoteWeightIsAllowed()) :
194 1
                $weight[$oneVoteString] += $oneVote->getWeight();
195
            else :
196 4
                $weight[$oneVoteString]++;
197
            endif;
198
199 5
            $nb[$oneVoteString]++;
200
        endforeach;
201
202 5
        ksort($weight);
203 5
        arsort($weight);
204
205 5
        $isFirst = true;
206 5
        foreach ($weight as $key => $value) :
207 5
            if (!$isFirst) :
208 5
                $simpleList .= "\n";
209
            endif;
210 5
            $voteString = ($key === '') ? '{{EMPTY_VOTE_IN_CONTEXT}}' : $key;
211 5
            $simpleList .= $voteString.' * '.$nb[$key];
212 5
            $isFirst = false;
213
        endforeach;
214
215 5
        return $simpleList;
216
    }
217
218 12
    public function countVotes (?array $tag, bool $with) : int
219
    {
220 12
        if ($tag === null) :
221 9
            return count($this);
222
        else :
223 5
            $count = 0;
224
225 5
            foreach ($this as $key => $value) :
226 5
                $noOne = true;
227 5
                foreach ($tag as $oneTag) :
228 5
                    if ( ( $oneTag === $key ) || in_array($oneTag, $value->getTags(),true) ) :
229 5
                        if ($with) :
230 5
                            $count++;
231 5
                            break;
232
                        else :
233 5
                            $noOne = false;
234
                        endif;
235
                    endif;
236
                endforeach;
237
238 5
                if (!$with && $noOne) :
239 5
                    $count++;
240
                endif;
241
            endforeach;
242
243 5
            return $count;
244
        endif;
245
    }
246
247 1
    public function countInvalidVoteWithConstraints () : int
248
    {
249 1
        $count = 0;
250
251 1
        foreach ($this as $oneVote) :
252 1
            if(!$this->getElection()->testIfVoteIsValidUnderElectionConstraints($oneVote)) :
253 1
                $count++;
254
            endif;
255
        endforeach;
256
257 1
        return $count;
258
    }
259
260 7
    public function sumVotesWeight (bool $constraint = false) : int
261
    {
262 7
        $sum = 0;
263
264 7
        foreach ($this as $oneVote) :
265 7
            if ( !$constraint || $this->getElection()->testIfVoteIsValidUnderElectionConstraints($oneVote) ) :
266 7
                $sum += ($this->getElection()->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1;
267
            endif;
268
        endforeach;
269
270 7
        return $sum;
271
    }
272
}
273