Passed
Push — main ( 2eda8e...89a318 )
by Carlos C
02:30 queued 11s
created

constructSplSubjectWithObservers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EngineWorks\ProgressStatus;
6
7
use SplObserver;
8
9
/**
10
 * Implementation of SplSubject interface using the ObserversSet object storage.
11
 *
12
 * @implements \SplSubject<\SplObserver>
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 25
    }
23
24 4
    public function attach(SplObserver $observer): void
25
    {
26 4
        $this->observers->attach($observer);
27 4
    }
28
29 1
    public function detach(SplObserver $observer): void
30
    {
31 1
        $this->observers->detach($observer);
32 1
    }
33
34 23
    public function notify(): void
35
    {
36 23
        foreach ($this->getObservers() as $observer) {
37 3
            $observer->update($this);
38
        }
39 23
    }
40
41 24
    private function getObservers(): ObserversSet
42
    {
43 24
        return $this->observers;
44
    }
45
}
46