Completed
Push — master ( c01904...dcbc34 )
by Boudry
02:56
created

VotesManager::setElection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 28.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
    Condorcet PHP Class, with Schulze Methods and others !
4
5
    By Julien Boudry - MIT LICENSE (Please read LICENSE.txt)
6
    https://github.com/julien-boudry/Condorcet
7
*/
8
declare(strict_types=1);
9
10
11
namespace Condorcet\DataManager;
12
13
use Condorcet\DataManager\ArrayManager;
14
use Condorcet\DataManager\DataContextInterface;
15
use Condorcet\CondorcetException;
16
use Condorcet\Election;
17
use Condorcet\Vote;
18
19
class VotesManager extends ArrayManager
20
{
21
22
/////////// Magic ///////////
23
24 109
    public function __construct (?Election $election = null)
25
    {
26 109
        if ($election !== null) :
27 109
            $this->setElection($election);
28
        endif;
29
30 109
        parent::__construct();
31 109
    }
32
33 109
    public function setElection (Election $election)
34
    {
35 109
        $this->_link[0] = $election;
36 109
    }
37
38
/////////// Data CallBack for external drivers ///////////
39
40
    public function getDataContextObject () : DataContextInterface
41
    {
42
        $context = new Class implements DataContextInterface {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
43
            public $election;
44
45 3
            public function dataCallBack ($data) : Vote
46
            {
47 3
                $vote = new Vote ($data);
48 3
                $this->election->checkVoteCandidate($vote);
49 3
                $vote->registerLink($this->election);
50
51 3
                return $vote;
52
            }
53
54 3
            public function dataPrepareStoringAndFormat ($data) : string
55
            {
56 3
                $data->destroyLink($this->election);
57
58 3
                return (string) $data;
59
            }
60
        };
61
62 4
        $context->election = $this->_link[0] ?? null;
63
64 4
        return $context;
65
    }
66
67
/////////// Array Access - Specials improvements ///////////
68
69 100
    public function offsetSet($offset, $value) : void
70
    {
71 100
        if ($value instanceof Vote) :
72 100
            parent::offsetSet($offset,$value);
73 100
            $this->setStateToVote();
74
        else :
75 1
            throw new CondorcetException (0,'Value must be an instanceof Condorcet\\Vote');
76
        endif;
77 100
    }
78
79 7
    public function offsetUnset($offset) : bool
80
    {
81 7
        if (parent::offsetUnset($offset)) :
82 7
            $this->setStateToVote();
83 7
            return true;
84
        endif;
85 1
        return false;
86
    }
87
88
/////////// Internal Election related methods ///////////
89
90 100
    protected function setStateToVote () : void
91
    {
92 100
        foreach ($this->_link as &$element) :
93 98
            $element->setStateToVote();
94
        endforeach;
95 100
    }
96
97
/////////// Public specific methods ///////////
98
99 5
    public function getVoteKey (Vote $vote) {
100
        // Return False if using with Bdd storing. Futur: Throw a PHP7 Error.
101 5
        return array_search($vote, $this->_Container, true);
102
    }
103
104
    // Get the votes registered list
105 16
    public function getVotesList (?array $tag = null, bool $with = true) : array
106
    {
107 16
        if (($tag = Vote::tagsConvert($tag)) === null) :
108 15
            return $this->getFullDataSet();
109
        else :
110 4
            $search = [];
111
112 4
            foreach ($this as $key => $value) :
113 4
                $noOne = true;
114 4
                foreach ($tag as $oneTag) :
115 4
                    if ( ( $oneTag === $key ) || in_array($oneTag, $value->getTags(),true) ) :
116 4
                        if ($with) :
117 4
                            $search[$key] = $value;
118 4
                            break;
119
                        else :
120 4
                            $noOne = false;
121
                        endif;
122
                    endif;
123
                endforeach;
124
125 4
                if (!$with && $noOne) :
126 4
                    $search[$key] = $value;
127
                endif;
128
            endforeach;
129
130 4
            return $search;
131
        endif;
132
    }
133
134 4
    public function getVotesListAsString () : string
135
    {
136 4
        $simpleList = '';
137
138 4
        $weight = [];
139 4
        $nb = [];
140
141 4
        foreach($this->getVotesList() as $oneVote) :
142 4
            $oneVoteString = $oneVote->getSimpleRanking($this->_link[0]);
143
144 4
            if(!array_key_exists($oneVoteString, $weight)) :
145 4
                $weight[$oneVoteString] = 0;
146
            endif;
147 4
            if(!array_key_exists($oneVoteString, $nb)) :
148 4
                $nb[$oneVoteString] = 0;
149
            endif;
150
151 4
            if ($this->_link[0]->isVoteWeightIsAllowed()) :
152 1
                $weight[$oneVoteString] += $oneVote->getWeight();
153
            else :
154 3
                $weight[$oneVoteString]++;
155
            endif;
156
157 4
            $nb[$oneVoteString]++;
158
        endforeach;
159
160 4
        ksort($weight);
161 4
        arsort($weight);
162
163 4
        $isFirst = true;
164 4
        foreach ($weight as $key => $value) :
165 4
            if (!$isFirst) : $simpleList .= "\n"; endif;
166 4
            $simpleList .= $key.' * '.$nb[$key];
167 4
            $isFirst = false;
168
        endforeach;
169
170 4
        return $simpleList;
171
    }
172
173 10
    public function countVotes (?array $tag, bool $with) : int
174
    {
175 10
        if ($tag === null) :
176 7
            return count($this);
177
        else :
178 5
            $count = 0;
179
180 5
            foreach ($this as $key => $value) :
181 5
                $noOne = true;
182 5
                foreach ($tag as $oneTag) :
183 5
                    if ( ( $oneTag === $key ) || in_array($oneTag, $value->getTags(),true) ) :
184 5
                        if ($with) :
185 5
                            $count++;
186 5
                            break;
187
                        else :
188 5
                            $noOne = false;
189
                        endif;
190
                    endif;
191
                endforeach;
192
193 5
                if (!$with && $noOne) :
194 5
                    $count++;
195
                endif;
196
            endforeach;
197
198 5
            return $count;
199
        endif;
200
    }
201
}
202