Completed
Push — master ( dcbc34...facd47 )
by Boudry
02:36
created

VotesManager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
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) : void
80
    {
81 7
        if (parent::offsetUnset($offset)) :
82
            $this->setStateToVote();
83
        endif;
84 7
    }
85
86
/////////// Internal Election related methods ///////////
87
88 100
    protected function setStateToVote () : void
89
    {
90 100
        foreach ($this->_link as &$element) :
91 98
            $element->setStateToVote();
92
        endforeach;
93 100
    }
94
95
/////////// Public specific methods ///////////
96
97 5
    public function getVoteKey (Vote $vote) {
98
        // Return False if using with Bdd storing. Futur: Throw a PHP7 Error.
99 5
        return array_search($vote, $this->_Container, true);
100
    }
101
102
    // Get the votes registered list
103 16
    public function getVotesList (?array $tag = null, bool $with = true) : array
104
    {
105 16
        if (($tag = Vote::tagsConvert($tag)) === null) :
106 15
            return $this->getFullDataSet();
107
        else :
108 4
            $search = [];
109
110 4
            foreach ($this as $key => $value) :
111 4
                $noOne = true;
112 4
                foreach ($tag as $oneTag) :
113 4
                    if ( ( $oneTag === $key ) || in_array($oneTag, $value->getTags(),true) ) :
114 4
                        if ($with) :
115 4
                            $search[$key] = $value;
116 4
                            break;
117
                        else :
118 4
                            $noOne = false;
119
                        endif;
120
                    endif;
121
                endforeach;
122
123 4
                if (!$with && $noOne) :
124 4
                    $search[$key] = $value;
125
                endif;
126
            endforeach;
127
128 4
            return $search;
129
        endif;
130
    }
131
132 4
    public function getVotesListAsString () : string
133
    {
134 4
        $simpleList = '';
135
136 4
        $weight = [];
137 4
        $nb = [];
138
139 4
        foreach($this->getVotesList() as $oneVote) :
140 4
            $oneVoteString = $oneVote->getSimpleRanking($this->_link[0]);
141
142 4
            if(!array_key_exists($oneVoteString, $weight)) :
143 4
                $weight[$oneVoteString] = 0;
144
            endif;
145 4
            if(!array_key_exists($oneVoteString, $nb)) :
146 4
                $nb[$oneVoteString] = 0;
147
            endif;
148
149 4
            if ($this->_link[0]->isVoteWeightIsAllowed()) :
150 1
                $weight[$oneVoteString] += $oneVote->getWeight();
151
            else :
152 3
                $weight[$oneVoteString]++;
153
            endif;
154
155 4
            $nb[$oneVoteString]++;
156
        endforeach;
157
158 4
        ksort($weight);
159 4
        arsort($weight);
160
161 4
        $isFirst = true;
162 4
        foreach ($weight as $key => $value) :
163 4
            if (!$isFirst) : $simpleList .= "\n"; endif;
164 4
            $simpleList .= $key.' * '.$nb[$key];
165 4
            $isFirst = false;
166
        endforeach;
167
168 4
        return $simpleList;
169
    }
170
171 10
    public function countVotes (?array $tag, bool $with) : int
172
    {
173 10
        if ($tag === null) :
174 7
            return count($this);
175
        else :
176 5
            $count = 0;
177
178 5
            foreach ($this as $key => $value) :
179 5
                $noOne = true;
180 5
                foreach ($tag as $oneTag) :
181 5
                    if ( ( $oneTag === $key ) || in_array($oneTag, $value->getTags(),true) ) :
182 5
                        if ($with) :
183 5
                            $count++;
184 5
                            break;
185
                        else :
186 5
                            $noOne = false;
187
                        endif;
188
                    endif;
189
                endforeach;
190
191 5
                if (!$with && $noOne) :
192 5
                    $count++;
193
                endif;
194
            endforeach;
195
196 5
            return $count;
197
        endif;
198
    }
199
}
200