Passed
Push — master ( 2d74f9...711d75 )
by Alexander
03:54 queued 40s
created

Token::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
ccs 8
cts 8
cp 1
cc 1
eloc 13
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 array $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 23
    public static function tableName()
41
    {
42 23
        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 23
    public function behaviors()
57
    {
58
        return [
59 23
            'timestamp' => [
60
                'class' => TimestampBehavior::class,
61
                'updatedAtAttribute' => null,
62 23
                'value' => function () {
63 13
                    return Carbon::now()->toDateTimeString();
64 23
                },
65
            ],
66
            'type' => [
67
                'class' => AttributeBehavior::class,
68
                'attributes' => [
69 23
                    ActiveRecord::EVENT_INIT => ['type',],
70 23
                    ActiveRecord::EVENT_BEFORE_VALIDATE => ['type',],
71
                ],
72
                /** @see Token::getType() */
73 23
                'value' => [$this, 'getType']
74
            ]
75
        ];
76
    }
77
78
    /**
79
     * @return array
80
     */
81 13
    public function rules()
82
    {
83
        return [
84 13
            [['token', 'recipient',], 'required'],
85
            [['token', 'recipient',], 'safe'],
86
            [['verify_count', 'delivery_count'], 'integer', 'min' => 0,],
87
            ['verify_count', 'default', 'value' => 0,],
88
            ['delivery_count', 'default', 'value' => 0,],
89
            ['data', 'safe',],
90
        ];
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    abstract public static function getType(): string;
97
98
    /**
99
     * @return string
100
     */
101 5
    public function getRecipient(): string
102
    {
103 5
        return $this->recipient;
104
    }
105
106
    /**
107
     * @return string
108
     */
109 7
    public function getToken(): string
110
    {
111 7
        return $this->token;
112
    }
113
114
    /**
115
     * @return array
116
     */
117 4
    public function getData(): array
118
    {
119 4
        return $this->data;
120
    }
121
122
    /**
123
     * Count of sent attempts
124
     *
125
     * @return int
126
     */
127 4
    public function getDeliveryCount(): int
128
    {
129 4
        return $this->delivery_count ?? 0;
130
    }
131
132
    /**
133
     * Count of verify attempts
134
     *
135
     * @return int
136
     */
137 5
    public function getVerifyCount(): int
138
    {
139 5
        return $this->verify_count ?? 0;
140
    }
141
142
    /**
143
     * @param string $token
144
     * @return TokenRecordInterface
145
     */
146 9
    public function setToken(string $token): TokenRecordInterface
147
    {
148 9
        $this->token = $token;
149 9
        return $this;
150
    }
151
152
    /**
153
     * @param string $recipient
154
     * @return TokenRecordInterface
155
     */
156 8
    public function setRecipient(string $recipient): TokenRecordInterface
157
    {
158 8
        $this->recipient = $recipient;
159 8
        return $this;
160
    }
161
162
    /**
163
     * @param array $data
164
     * @return TokenRecordInterface
165
     */
166 7
    public function setData(array $data): TokenRecordInterface
167
    {
168 7
        $this->data = $data;
169 7
        return $this;
170
    }
171
172
    /**
173
     * @return TokenRecordInterface
174
     */
175 5
    public function increaseVerifyCount(): TokenRecordInterface
176
    {
177 5
        $this->verify_count++;
178 5
        return $this;
179
    }
180
181
    /**
182
     * @return TokenRecordInterface
183
     */
184 2
    public function increaseDeliveryCount(): TokenRecordInterface
185
    {
186 2
        $this->delivery_count++;
187 2
        return $this;
188
    }
189
190 1
    public function __toString(): string
191
    {
192 1
        return $this->token;
193
    }
194
}
195