| Total Complexity | 6 |
| Total Lines | 51 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 12 | class ReadableBehavior extends Behavior |
||
| 13 | { |
||
| 14 | |||
| 15 | public $readAttribute = 'read_at'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Mark the notification as read. |
||
| 19 | * @throws \Exception |
||
| 20 | * @throws \Throwable |
||
| 21 | */ |
||
| 22 | public function markAsRead() |
||
| 23 | { |
||
| 24 | /** @var ActiveRecordInterface $model */ |
||
| 25 | $model = $this->owner; |
||
| 26 | if (is_null($model->{$this->readAttribute})) { |
||
| 27 | $model->update(false, [$this->readAttribute => date('Y-m-d H:i:s')]); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Mark the notification as unread. |
||
| 33 | * @return void |
||
| 34 | * @throws \Exception |
||
| 35 | * @throws \Throwable |
||
| 36 | */ |
||
| 37 | public function markAsUnread() |
||
| 38 | { |
||
| 39 | /** @var ActiveRecordInterface $model */ |
||
| 40 | $model = $this->owner; |
||
| 41 | if (!is_null($model->{$this->readAttribute})) { |
||
| 42 | $model->update(false, [$this->readAttribute => null]); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Determine if a notification has been read. |
||
| 48 | * |
||
| 49 | * @return bool |
||
| 50 | */ |
||
| 51 | public function isRead() |
||
| 52 | { |
||
| 53 | return $this->owner->{$this->readAttribute} !== null; |
||
| 54 | } |
||
| 55 | /** |
||
| 56 | * Determine if a notification has not been read. |
||
| 57 | * |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public function isUnread() |
||
| 63 | } |
||
| 64 | } |