Issues (14)

src/models/Notification.php (4 issues)

1
<?php
2
3
4
namespace tuyakhov\notifications\models;
5
6
7
use tuyakhov\notifications\behaviors\ReadableBehavior;
8
use tuyakhov\notifications\messages\DatabaseMessage;
9
use yii\behaviors\TimestampBehavior;
10
use yii\db\ActiveRecord;
11
use yii\db\Expression;
12
use yii\helpers\ArrayHelper;
13
use yii\helpers\Json;
14
15
/**
16
 * Database notification model
17
 * @property string $level
18
 * @property string $subject
19
 * @property string $notifiable_type
20
 * @property int $notifiable_id
21
 * @property string $body
22
 * @property string $data
23
 * @property DatabaseMessage $message
24
 * @property string $read_at
25
 * @property $notifiable
26
 * @method  void markAsRead()
27
 * @method  void markAsUnread()
28
 * @method  bool read()
29
 * @method  bool unread()
30
 */
31
class Notification extends ActiveRecord
32
{
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function rules()
38
    {
39
        return [
40
            [['level', 'notifiable_type', 'subject', 'body', 'data'], 'string'],
41
            ['notifiable_id', 'integer'],
42
        ];
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function behaviors()
49
    {
50
        return [
51
            [
52
                'class' => TimestampBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by Anton Tuyakhov
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

52
                'class' => /** @scrutinizer ignore-deprecated */ TimestampBehavior::className(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
53
                'value' => new Expression('CURRENT_TIMESTAMP'),
54
            ],
55
            ReadableBehavior::className()
0 ignored issues
show
Deprecated Code introduced by Anton Tuyakhov
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

55
            /** @scrutinizer ignore-deprecated */ ReadableBehavior::className()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
56
        ];
57
    }
58
59
    /**
60
     * @return \yii\db\ActiveQuery
61
     */
62
    public function getNotifiable()
63
    {
64
        return $this->hasOne($this->notifiable_type, ['id' => 'notifiable_id']);
65
    }
66
67
68
    /**
69
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by Anton Tuyakhov
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
70
     * @return mixed
71
     */
72
    public function data($key = null)
73
    {
74
        $data = Json::decode($this->data);
75
        if ($key === null) {
0 ignored issues
show
The condition $key === null is always true.
Loading history...
76
            return $data;
77
        }
78
        return ArrayHelper::getValue($data, $key);
79
    }
80
81
}