| Total Complexity | 8 |
| Total Lines | 34 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 11 | abstract class ASubject implements ISubject |
||
| 12 | { |
||
| 13 | public function __construct( |
||
| 14 | protected ?IObserver $observer = null, |
||
| 15 | ) { |
||
| 16 | } |
||
| 17 | |||
| 18 | public function notify(): void |
||
| 19 | { |
||
| 20 | $this->observer?->update($this); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function attach(IObserver $observer): void |
||
| 24 | { |
||
| 25 | if ($this->observer !== null) { |
||
| 26 | throw new InvalidArgumentException('Observer is already attached.'); |
||
| 27 | } |
||
| 28 | |||
| 29 | $this->assertNotSelf($observer); |
||
| 30 | |||
| 31 | $this->observer = $observer; |
||
| 32 | } |
||
| 33 | |||
| 34 | protected function assertNotSelf(object $obj): void |
||
| 35 | { |
||
| 36 | if ($obj === $this) { |
||
| 37 | throw new InvalidArgumentException('Object can not be self.'); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 41 | public function detach(IObserver $observer): void |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 |