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

CondorcetException::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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;
11
12
use Condorcet\Condorcet;
13
use Condorcet\CondorcetVersion;
14
15
// Custom Exeption
16
class CondorcetException extends \Exception
17
{
18
    use CondorcetVersion;
19
20
    protected $_infos;
21
22 12
    public function __construct (int $code = 0, $infos = '')
23
    {
24 12
        $this->_infos = $infos;
25
26 12
        parent::__construct($this->correspondence($code), $code);
27 12
    }
28
29 1
    public function __toString () : string
30
    {
31 1
           return __CLASS__ . ": [{$this->code}]: {$this->message} (line: {$this->file}:{$this->line})\n";
32
    }
33
34 12
    protected function correspondence (int $code) : string
35
    {
36 12
        $error = [];
37
38
        // Common
39 12
        $error[1] = 'Bad candidate format';
40 12
        $error[2] = 'The voting process has already started';
41 12
        $error[3] = 'This candidate ID is already registered';
42 12
        $error[4] = 'This candidate ID do not exist';
43 12
        $error[5] = 'Bad vote format | '.$this->_infos;
44 12
        $error[6] = 'You need to specify votes before results';
45 12
        $error[7] = 'Your Candidate ID is too long > ' . Election::MAX_LENGTH_CANDIDATE_ID;
46 12
        $error[8] = 'This method do not exist';
47 12
        $error[9] = 'The algo class you want has not been defined';
48 12
        $error[10] = 'The algo class you want is not correct';
49 12
        $error[11] = 'You try to unserialize an object version older than your actual Class version. This is a problematic thing';
50 12
        $error[12] = 'You have exceeded the number of votes allowed for this method.';
51 12
        $error[13] = 'Formatting error: You must specify an integer';
52 12
        $error[14] = 'parseVote() must take a string (raw or path) as argument';
53 12
        $error[15] = 'Input must be valid Json format';
54 12
        $error[16] = 'You have exceeded the maximum number of votes allowed per election ('.$this->_infos.').';
55 12
        $error[17] = 'Bad tags input format';
56 12
        $error[18] = 'New vote can\'t match Candidate of his elections';
57 12
        $error[19] = 'This name is not allowed in because of a namesake in the election in which the object participates.';
58 12
        $error[20] = 'You need to specify one or more candidates before voting';
59 12
        $error[21] = 'Bad vote timestamp format';
60 12
        $error[22] = 'This context is not valid';
61 12
        $error[23] = 'No Data Handler in use';
62 12
        $error[24] = 'A Data Handler is already in use';
63 12
        $error[25] = 'Algo class try to use existing alias';
64 12
        $error[26] = 'Weight can not be < 1';
65
66 12
        $error[30] = 'Candidate not in Ranking';
67
68
69
        // DataManager
70 12
        $error[30] = 'This entity does not exist.';
71
72
        // Algorithms
73 12
        if ($code === 101) :
74 2
            $error[101] = $this->_infos;
75
        endif;
76 12
        $error[102] = 'Marquis of Condorcet algortihm can\'t provide a full ranking. But only Winner and Loser.';
77
78
79 12
        if ( array_key_exists($code, $error) ) :
80 10
            return $error[$code];
81
        else :
82 2
            return (!is_null($this->_infos)) ? $this->_infos : 'Mysterious Error';
83
        endif;
84
    }
85
}
86