Completed
Push — master ( 6340e7...ed7926 )
by Boudry
02:54
created

Linkable::countLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
use CondorcetPHP\Condorcet\Throwable\{CondorcetException, CondorcetInternalException};
14
15
trait Linkable
16
{
17
    private $_link = [];
18
19 1
    public function __clone ()
20
    {
21 1
        $this->destroyAllLink();
22 1
    }
23
24 155
    public function haveLink (Election $election) : bool
25
    {
26 155
        return in_array($election, $this->_link, true);
27
    }
28
29 54
    public function countLinks () : int
30
    {
31 54
        return count($this->_link);
32
    }
33
34 54
    public function getLinks () : ?array
35
    {
36 54
        return !empty($this->_link) ? $this->_link : null;
37
    }
38
39
    // Internal
40
        # Dot not Overloading ! Do not Use !
41
42 155
    public function registerLink (Election $election) : void
43
    {
44 155
        if ( !$this->haveLink($election) ) :
45 155
            $this->_link[] = $election;
46
        else :
47 1
            throw new CondorcetInternalException ('Link is already registered.');
48
        endif;
49 155
    }
50
51 14
    public function destroyLink (Election $election) : bool
52
    {
53 14
        $destroyKey = array_search($election, $this->_link, true);
54
55 14
        if ($destroyKey !== false) :
56 13
            unset($this->_link[$destroyKey]);
57 13
            return true;
58
        else :
59 8
            return false;
60
        endif;
61
    }
62
63 77
    protected function destroyAllLink () : void
64
    {
65 77
        $this->_link = [];
66 77
    }
67
}
68