Passed
Push — 3.0 ( 1eb04a...9ca0ec )
by Vermeulen
02:07
created

SubjectList::getSubjectList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
     * @var \SplSubject[] $subjectList List of all subjects declared
22
     */
23
    protected $subjectList = [];
24
    
25
    /**
26
     * Getter accessor to property subjectList
27
     * 
28
     * @return \SplSubject[]
29
     */
30
    public function getSubjectList(): array
31
    {
32
        return $this->subjectList;
33
    }
34
    
35
    /**
36
     * Obtain a subject object with this name
37
     * 
38
     * @param string $subjectName The name of the subject object
39
     * 
40
     * @return \SplSubject
41
     * 
42
     * @throws \Exception If the subject name not exist
43
     */
44
    public function getSubjectByName(string $subjectName): \SplSubject
45
    {
46
        if (!array_key_exists($subjectName, $this->subjectList)) {
47
            throw new Exception(
48
                'The subject '.$subjectName.' is not in the list.',
49
                self::ERR_SUBJECT_NAME_NOT_EXIST
50
            );
51
        }
52
        
53
        return $this->subjectList[$subjectName];
54
    }
55
56
        
57
    /**
58
     * Add a new subject to the list
59
     * 
60
     * @param \SplSubject $subject The new subject to add
61
     * @param string|null $subjectName (default null) The subject name, if null,
62
     * the name of the class will be used
63
     * 
64
     * @return $this
65
     */
66
    public function addSubject(\SplSubject $subject, $subjectName = null): self
67
    {
68
        if ($subjectName === null) {
69
            $subjectName = get_class($subject);
70
        }
71
        
72
        $this->subjectList[$subjectName] = $subject;
73
        
74
        return $this;
75
    }
76
    
77
    /**
78
     * Remove a subject from the list
79
     * 
80
     * @param \SplSubject $subject The subject to remove from the list
81
     * 
82
     * @return $this
83
     * 
84
     * @throws \Exception If the subject has not been found into the list
85
     */
86
    public function removeSubject(\SplSubject $subject): self
87
    {
88
        $key = array_search($subject, $this->subjectList, true);
89
        
90
        if ($key === false) {
91
            throw new Exception(
92
                'The subject has not been found.',
93
                self::ERR_SUBJECT_NOT_FOUND
94
            );
95
        }
96
        
97
        unset($this->subjectList[$key]);
98
99
        return $this;
100
    }
101
}
102