SplSubjectWithObserversTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 34
ccs 13
cts 13
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A attach() 0 3 1
A constructSplSubjectWithObservers() 0 3 1
A detach() 0 3 1
A getObservers() 0 3 1
A notify() 0 8 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EngineWorks\ProgressStatus;
6
7
use LogicException;
8
use SplObserver;
9
use SplSubject;
10
11
/**
12
 * Implementation of SplSubject interface using the ObserversSet object storage.
13
 */
14
trait SplSubjectWithObserversTrait
15
{
16
    /** @var ObserversSet */
17
    private $observers;
18
19 25
    private function constructSplSubjectWithObservers(): void
20
    {
21 25
        $this->observers = new ObserversSet();
22
    }
23
24 4
    public function attach(SplObserver $observer): void
25
    {
26 4
        $this->observers->attach($observer);
27
    }
28
29 1
    public function detach(SplObserver $observer): void
30
    {
31 1
        $this->observers->detach($observer);
32
    }
33
34 24
    public function notify(): void
35
    {
36 24
        if (! $this instanceof SplSubject) {
37
            /** @psalm-var object $this Psalm identify $this as empty-mixed */
38 1
            throw new LogicException(sprintf('Object %s is not an instance of SplSubject', get_class($this)));
39
        }
40 23
        foreach ($this->getObservers() as $observer) {
41 3
            $observer->update($this);
42
        }
43
    }
44
45 24
    private function getObservers(): ObserversSet
46
    {
47 24
        return $this->observers;
48
    }
49
}
50