Completed
Push — master ( 922b0c...88b497 )
by Boudry
03:58
created

ResultManager::getResult()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6.0033

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 21
cts 22
cp 0.9545
rs 8.4751
c 0
b 0
f 0
cc 6
eloc 27
nc 7
nop 2
crap 6.0033
1
<?php
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
namespace Condorcet\ElectionProcess;
11
12
use Condorcet\Candidate;
13
use Condorcet\Condorcet;
14
use Condorcet\Election;
15
use Condorcet\CondorcetException;
16
use Condorcet\CondorcetVersion;
17
use Condorcet\Result;
18
use Condorcet\Algo\Pairwise;
19
use Condorcet\DataManager\VotesManager;
20
use Condorcet\Timer\Chrono as Timer_Chrono;
21
22
23
// Base Condorcet class
24
class ResultManager
25
{
26
    /////////// STATIC ///////////
27
28 75
    protected static function formatResultOptions (array $arg) : array
29
    {
30
        // About tag filter
31 75
        if (isset($arg['tags'])):
32 2
            $arg['%tagFilter'] = true;
33
34 2
            if ( !isset($arg['withTag']) || !is_bool($arg['withTag']) ) :
35 2
                $arg['withTag'] = true;
36
            endif;
37
        else:
38 75
            $arg['%tagFilter'] = false;
39
        endif;
40
41 75
        return $arg;
42
    }
43
44
45
/////////// CONSTRUCTOR ///////////
46
47
    use CondorcetVersion;
48
49
    protected $_Election;
50
51
    // Result
52
    protected $_Pairwise;
53
    protected $_Calculator;
54
55
        //////
56
57 80
    public function __construct (Election $election)
58
    {
59 80
        $this->setElection($election);
60 80
    }
61
62 1
    public function __clone ()
63
    {
64 1
        if ($this->_Pairwise !== null) :
65 1
            $this->_Pairwise = clone $this->_Pairwise;
66 1
            $this->_Pairwise->setElection($this->_Election);
67
        endif;
68 1
    }
69
70 80
    public function setElection (Election $election) : void
71
    {
72 80
        $this->_Election = $election;
73 80
    }
74
75
/////////// PUBLIC ///////////
76
77
    // Generic function for default result with ability to change default object method
78 75
    public function getResult ($method, array $options = []) : Result
79
    {
80 75
        $options = self::formatResultOptions($options);
81
82
        // Filter if tag is provided & return
83 75
        if ($options['%tagFilter']) :
84 2
            $chrono = new Timer_Chrono ($this->_Election->getTimerManager(), 'GetResult with filter');
85
86 2
            $filter = new Election;
87
88 2
            foreach ($this->_Election->getCandidatesList() as $candidate) :
89 2
                $filter->addCandidate($candidate);
90
            endforeach;
91
92 2
            foreach ($this->_Election->getVotesList($options['tags'], $options['withTag']) as $vote) :
93 2
                $filter->addVote($vote);
94
            endforeach;
95
96 2
            unset($chrono);
97
98 2
            return $filter->getResult($method);
99
        endif;
100
101
            ////// Start //////
102
103
        // Prepare
104 75
        $this->makePairwise();
105
106
            //////
107
108 75
        $chrono = new Timer_Chrono ($this->_Election->getTimerManager());
109
110 75
        if ($method === true) :
111 7
            $this->initResult(Condorcet::getDefaultMethod());
112 7
            $result = $this->_Calculator[Condorcet::getDefaultMethod()]->getResult();
113 71
        elseif ($method = Condorcet::isAuthMethod((string) $method)) :
114 71
            $this->initResult($method);
115 69
            $result = $this->_Calculator[$method]->getResult();
116
        else :
117
            throw new CondorcetException(8,$method);
118
        endif;
119
120 72
        $chrono->setRole('GetResult for '.$method);
121
122 72
        return $result;
123
    }
124
125
126 78
    public function getWinner (?string $substitution)
127
    {
128 78
        $algo = Condorcet::condorcetBasicSubstitution($substitution);
129
130
            //////
131
132 78
        if ($algo === Condorcet::CONDORCET_BASIC_CLASS) :
133 76
            new Timer_Chrono ($this->_Election->getTimerManager(), 'GetWinner for CondorcetBasic');
134 76
            $this->initResult($algo);
135 76
            $result = $this->_Calculator[$algo]->getWinner();
136
137 76
            return $this->formatWinner($result);
138
        else :
139 50
            return $this->getResult($algo)->getWinner();
140
        endif;
141
    }
142
143
144 75
    public function getLoser (?string $substitution)
145
    {
146 75
        $algo = Condorcet::condorcetBasicSubstitution($substitution);
147
148
            //////
149
150 75
        if ($algo === Condorcet::CONDORCET_BASIC_CLASS) :
151 75
            new Timer_Chrono ($this->_Election->getTimerManager(), 'GetLoser for CondorcetBasic');
152 75
            $this->initResult($algo);
153 75
            $result = $this->_Calculator[$algo]->getLoser();
154
155 75
            return $this->formatWinner($result);
156
        else :
157 2
            return $this->getResult($algo)->getLoser();
158
        endif;
159
    }
160
161
162
    public function computeResult ($method) : void
163
    {
164
        $this->getResult($method);
165
    }
166
167
168
    //:: INTERNAL :://
169
170
171
    // Prepare to compute results & caching system
172 80
    protected function makePairwise () : void
173
    {
174 80
        if ($this->_Pairwise === null) :
175 80
            $this->_Pairwise = new Pairwise ($this->_Election);
176
        endif;
177 80
    }
178
179
180 80
    protected function initResult (string $class) : void
181
    {
182 80
        if ( !isset($this->_Calculator[$class]) ) :
183 80
            $this->_Calculator[$class] = new $class($this->_Election);
184
        endif;
185 78
    }
186
187 77
    protected function formatWinner (?int $result)
188
    {
189 77
        return ($result === null) ? null : $this->_Election->getCandidateId($result);
190
    }
191
192
    //:: GET RAW DATA :://
193
194 77
    public function getPairwise () : Pairwise
195
    {
196 77
        $this->makePairwise();
197
198 77
        return $this->_Pairwise;
199
    }
200
201
}
202