Completed
Push — master ( 922505...49fe4c )
by Boudry
05:20
created

VotesManager::offsetSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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