GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SubjectList   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubjectList() 0 3 1
A getSubjectByName() 0 10 2
A addSubject() 0 19 4
A removeSubject() 0 14 2
1
<?php
2
3
namespace BFW\Core;
4
5
use \Exception;
6
7
class SubjectList
8
{
9
    /**
10
     * @const ERR_SUBJECT_NAME_NOT_EXIST Exception code if a subject name is
11
     * not found.
12
     */
13
    const ERR_SUBJECT_NAME_NOT_EXIST = 1206001;
14
    
15
    /**
16
     * @const ERR_SUBJECT_NOT_FOUND Exception code if a subject is not found.
17
     */
18
    const ERR_SUBJECT_NOT_FOUND = 1206002;
19
    
20
    /**
21
     * @const ERR_ADD_SUBJECT_ALREADY_EXIST Exception code if during the add of
22
     * a new subject, a subject into the list already have the defined name.
23
     */
24
    const ERR_ADD_SUBJECT_ALREADY_EXIST = 1206003;
25
    
26
    /**
27
     * @var \SplSubject[] $subjectList List of all subjects declared
28
     */
29
    protected $subjectList = [];
30
    
31
    /**
32
     * Getter accessor to property subjectList
33
     * 
34
     * @return \SplSubject[]
35
     */
36
    public function getSubjectList(): array
37
    {
38
        return $this->subjectList;
39
    }
40
    
41
    /**
42
     * Obtain a subject object with this name
43
     * 
44
     * @param string $subjectName The name of the subject object
45
     * 
46
     * @return \SplSubject
47
     * 
48
     * @throws \Exception If the subject name not exist
49
     */
50
    public function getSubjectByName(string $subjectName): \SplSubject
51
    {
52
        if (!array_key_exists($subjectName, $this->subjectList)) {
53
            throw new Exception(
54
                'The subject '.$subjectName.' is not in the list.',
55
                self::ERR_SUBJECT_NAME_NOT_EXIST
56
            );
57
        }
58
        
59
        return $this->subjectList[$subjectName];
60
    }
61
62
        
63
    /**
64
     * Add a new subject to the list
65
     * 
66
     * @param \SplSubject $subject The new subject to add
67
     * @param string|null $subjectName (default null) The subject name, if null,
68
     * the name of the class will be used
69
     * 
70
     * @return $this
71
     */
72
    public function addSubject(\SplSubject $subject, $subjectName = null): self
73
    {
74
        if ($subjectName === null) {
75
            $subjectName = get_class($subject);
76
        }
77
        
78
        if (
79
            array_key_exists($subjectName, $this->subjectList) &&
80
            $this->subjectList[$subjectName] !== $subject
81
        ) {
82
            throw new \Exception(
83
                'A subject with the name '.$subjectName.' already exist.',
84
                self::ERR_ADD_SUBJECT_ALREADY_EXIST
85
            );
86
        }
87
        
88
        $this->subjectList[$subjectName] = $subject;
89
        
90
        return $this;
91
    }
92
    
93
    /**
94
     * Remove a subject from the list
95
     * 
96
     * @param \SplSubject $subject The subject to remove from the list
97
     * 
98
     * @return $this
99
     * 
100
     * @throws \Exception If the subject has not been found into the list
101
     */
102
    public function removeSubject(\SplSubject $subject): self
103
    {
104
        $key = array_search($subject, $this->subjectList, true);
105
        
106
        if ($key === false) {
107
            throw new Exception(
108
                'The subject has not been found.',
109
                self::ERR_SUBJECT_NOT_FOUND
110
            );
111
        }
112
        
113
        unset($this->subjectList[$key]);
114
115
        return $this;
116
    }
117
}
118