| Total Complexity | 9 |
| Total Lines | 51 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 15 | abstract class ASubject implements ISubject |
||
| 16 | { |
||
| 17 | public function __construct( |
||
| 18 | protected ?IObserver $observer = null, |
||
| 19 | ) { |
||
| 20 | } |
||
| 21 | |||
| 22 | public function notify(): void |
||
| 23 | { |
||
| 24 | $this->observer?->update($this); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function attach(IObserver $observer): void |
||
| 28 | { |
||
| 29 | $this->assertNotSelf($observer); |
||
| 30 | |||
| 31 | $this->assertObserverIsNotAttached(); |
||
| 32 | |||
| 33 | $this->observer = $observer; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @throws InvalidArgument |
||
| 38 | */ |
||
| 39 | protected function assertNotSelf(object $obj): void |
||
| 40 | { |
||
| 41 | if ($obj === $this) { |
||
| 42 | throw new InvalidArgument( |
||
| 43 | sprintf( |
||
| 44 | 'Object can not be self. %s #%s.', |
||
| 45 | get_debug_type($obj), |
||
| 46 | spl_object_id($obj), |
||
| 47 | ) |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @throws LogicException |
||
| 54 | */ |
||
| 55 | protected function assertObserverIsNotAttached(): void |
||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | public function detach(IObserver $observer): void |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 |