Passed
Pull Request — master (#4)
by Alexander
03:54
created

Token::behaviors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
ccs 9
cts 9
cp 1
cc 1
eloc 17
nc 1
nop 0
crap 1
1
<?php
2
3
4
namespace Wearesho\Yii\Models;
5
6
use yii\behaviors\AttributeBehavior;
7
use yii\behaviors\TimestampBehavior;
8
use yii\db\ActiveRecord;
9
10
use Carbon\Carbon;
11
12
use paulzi\jsonBehavior\JsonBehavior;
13
use paulzi\jsonBehavior\JsonField;
14
use paulzi\jsonBehavior\JsonValidator;
15
16
use Wearesho\Yii\Interfaces\TokenQueryInterface;
17
use Wearesho\Yii\Interfaces\TokenRecordInterface;
18
use Wearesho\Yii\Queries\TokenQuery;
19
20
/**
21
 * Class Token
22
 * @package Wearesho\Yii\Models
23
 *
24
 * @property-read int $id
25
 *
26
 * @property string $token
27
 * @property string $recipient
28
 * @property JsonField $data
29
 *
30
 * @property int $delivery_count
31
 * @property int $verify_count
32
 *
33
 * @property string $created_at
34
 */
35
abstract class Token extends ActiveRecord implements TokenRecordInterface
36
{
37
    /**
38
     * @return string
39
     */
40 24
    public static function tableName()
41
    {
42 24
        return 'token';
43
    }
44
45
    /**
46
     * @return TokenQueryInterface
47
     */
48 12
    public static function find(): TokenQueryInterface
49
    {
50 12
        return new TokenQuery(get_called_class());
51
    }
52
53
    /**
54
     * @return array
55
     */
56 24
    public function behaviors()
57
    {
58
        return [
59 24
            'jsonData' => [
60
                'class' => JsonBehavior::class,
61
                'attributes' => ['data'],
62
            ],
63
            'timestamp' => [
64
                'class' => TimestampBehavior::class,
65
                'attributes' => [
66 24
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
67
                ],
68 24
                'value' => function () {
69 13
                    return Carbon::now()->toDateTimeString();
70 24
                },
71
            ],
72
            'type' => [
73
                'class' => AttributeBehavior::class,
74
                'attributes' => [
75 24
                    ActiveRecord::EVENT_INIT => ['type',],
76 24
                    ActiveRecord::EVENT_BEFORE_VALIDATE => ['type',],
77
                ],
78
                /** @see Token::getType() */
79 24
                'value' => [$this, 'getType']
80
            ]
81
        ];
82
    }
83
84
    /**
85
     * @return array
86
     */
87 13
    public function rules()
88
    {
89
        return [
90 13
            [['token', 'recipient',], 'required'],
91
            [['token', 'recipient',], 'safe'],
92
            [['verify_count', 'delivery_count'], 'integer', 'min' => 0,],
93
            ['verify_count', 'default', 'value' => 0,],
94
            ['delivery_count', 'default', 'value' => 0,],
95
            ['data', JsonValidator::class,]
96
        ];
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    abstract public static function getType(): string;
103
104
    /**
105
     * @return string
106
     */
107 5
    public function getRecipient(): string
108
    {
109 5
        return $this->recipient;
110
    }
111
112
    /**
113
     * @return string
114
     */
115 7
    public function getToken(): string
116
    {
117 7
        return $this->token;
118
    }
119
120
    /**
121
     * @return array
122
     */
123 4
    public function getData(): array
124
    {
125 4
        return $this->data->toArray();
126
    }
127
128
    /**
129
     * Count of sent attempts
130
     *
131
     * @return int
132
     */
133 4
    public function getDeliveryCount(): int
134
    {
135 4
        return $this->delivery_count ?? 0;
136
    }
137
138
    /**
139
     * Count of verify attempts
140
     *
141
     * @return int
142
     */
143 2
    public function getVerifyCount(): int
144
    {
145 2
        return $this->verify_count ?? 0;
146
    }
147
148
    /**
149
     * @param string $token
150
     * @return TokenRecordInterface
151
     */
152 9
    public function setToken(string $token): TokenRecordInterface
153
    {
154 9
        $this->token = $token;
155 9
        return $this;
156
    }
157
158
    /**
159
     * @param string $recipient
160
     * @return TokenRecordInterface
161
     */
162 8
    public function setRecipient(string $recipient): TokenRecordInterface
163
    {
164 8
        $this->recipient = $recipient;
165 8
        return $this;
166
    }
167
168
    /**
169
     * @param array $data
170
     * @return TokenRecordInterface
171
     */
172 7
    public function setData(array $data): TokenRecordInterface
173
    {
174 7
        $this->data->set($data);
175 7
        return $this;
176
    }
177
178
    /**
179
     * @return TokenRecordInterface
180
     */
181 3
    public function increaseVerifyCount(): TokenRecordInterface
182
    {
183 3
        $this->verify_count++;
184 3
        return $this;
185
    }
186
187
    /**
188
     * @return TokenRecordInterface
189
     */
190 2
    public function increaseDeliveryCount(): TokenRecordInterface
191
    {
192 2
        $this->delivery_count++;
193 2
        return $this;
194
    }
195
196 1
    public function __toString(): string
197
    {
198 1
        return $this->token;
199
    }
200
}
201