Completed
Branch dev-2.0 (210bc7)
by Boudry
06:33
created

CondorcetException::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
ccs 5
cts 6
cp 0.8333
crap 3.0416
1
<?php
2
/*
3
    Condorcet PHP - Election manager and results calculator.
4
    Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems.
5
6
    By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt)
7
    https://github.com/julien-boudry/Condorcet
8
*/
9
declare(strict_types=1);
10
11
namespace CondorcetPHP\Condorcet;
12
13
14
// Custom Exeption
15
class CondorcetException extends \Exception
16
{
17
    use CondorcetVersion;
18
19
    public const CODE_RANGE = [0,1000];
20
21
    public const EXCEPTION_CODE = [
22
        1 => 'Bad candidate format',
23
        2 => 'The voting process has already started',
24
        3 => 'This candidate ID is already registered',
25
        4 => 'This candidate ID do not exist',
26
        5 => 'Bad vote format | {{ infos1 }}',
27
        6 => 'You need to specify votes before results',
28
        7 => 'Your Candidate ID is too long > {{ infos1 }}',
29
        8 => 'This method do not exist',
30
        9 => 'The algo class you want has not been defined',
31
        10 => 'The algo class you want is not correct',
32
        11 => 'You try to unserialize an object version older than your actual Class version. This is a problematic thing',
33
        12 => 'You have exceeded the number of votes allowed for this method.',
34
        13 => 'Formatting error: You must specify an integer',
35
        14 => 'parseVote() must take a string (raw or path) as argument',
36
        15 => 'Input must be valid Json format',
37
        16 => 'You have exceeded the maximum number of votes allowed per election ({{ infos1 }}).',
38
        17 => 'Bad tags input format',
39
        18 => 'New vote can\'t match Candidate of his elections',
40
        19 => 'This name is not allowed in because of a namesake in the election in which the object participates.',
41
        20 => 'You need to specify one or more candidates before voting',
42
        21 => 'Bad vote timestamp format',
43
        22 => 'This context is not valid',
44
        23 => 'No Data Handler in use',
45
        24 => 'A Data Handler is already in use',
46
        25 => 'Algo class try to use existing alias',
47
        26 => 'Weight can not be < 1',
48
        27 => 'The vote constraint class you want has not been defined',
49
        28 => 'The vote constraint class you want is not correct',
50
        29 => 'This vote constraint is already registered',
51
        30 => 'You can\'t add constraint after starting to record votes ',
52
        31 => 'Vote object already registred',
53
        32 => 'Invalid Input',
54
55
        // DataManager
56
        50 => 'This entity does not exist.',
57
58
        // Algo.
59
        102 => 'Marquis of Condorcet algortihm can\'t provide a full ranking. But only Winner and Loser.'
60
    ];
61
62
    protected $_infos;
63
64 43
    public function __construct (int $code = 0, string ...$infos)
65
    {
66 43
        if ($code < static::CODE_RANGE[0] || $code > static::CODE_RANGE[1]) :
67
            throw new self (0,'Exception class error');
68
        endif;
69
70 43
        $this->_infos = $infos;
71
72 43
        parent::__construct($this->correspondence($code), $code);
73 43
    }
74
75
    public function __toString () : string
76
    {
77
           return static::class . ": [{$this->code}]: {$this->message} (line: {$this->file}:{$this->line})\n";
78
    }
79
80 43
    protected function correspondence (int $code) : string
81
    {
82
        // Algorithms
83 43
        if ($code === 0 || $code === 101) :
84 8
            return $this->_infos[0] ?? '';
85
        endif;
86
87 36
        if ( array_key_exists($code, static::EXCEPTION_CODE) ) :
88 36
            return str_replace('{{ infos1 }}', $this->_infos[0] ?? '', static::EXCEPTION_CODE[$code]);
89
        else :
90
            return static::EXCEPTION_CODE[0] ?? 'Mysterious Error';
91
        endif;
92
    }
93
}
94