CondorcetException::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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