Token   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 114
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 41 1
A toProjectConfig() 0 11 1
A find() 0 5 1
A isActive() 0 4 2
A hasExpired() 0 5 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\patron\records;
10
11
use Craft;
12
use craft\helpers\DateTimeHelper;
13
use craft\validators\DateTimeValidator;
14
use DateTime;
15
use flipbox\craft\ember\helpers\ModelHelper;
16
use flipbox\craft\ember\records\ActiveRecordWithId;
17
use flipbox\craft\ember\records\StateAttributeTrait;
18
use flipbox\patron\queries\TokenActiveQuery;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @property string $accessToken
25
 * @property string $refreshToken
26
 * @property DateTime|null $dateExpires
27
 * @property array $values
28
 */
29
class Token extends ActiveRecordWithId
30
{
31
    use StateAttributeTrait,
32
        ProviderAttributeTrait;
33
34
    /**
35
     * The table alias
36
     */
37
    const TABLE_ALIAS = 'patron_tokens';
38
39
    /**
40
     * @inheritdoc
41
     */
42
    protected $getterPriorityAttributes = [
43
        'providerId'
44
    ];
45
46
    /*******************************************
47
     * QUERY
48
     *******************************************/
49
50
    /**
51
     * @inheritdoc
52
     * @return TokenActiveQuery
53
     * @throws \yii\base\InvalidConfigException
54
     */
55
    public static function find()
56
    {
57
        /** @noinspection PhpIncompatibleReturnTypeInspection */
58
        return Craft::createObject(TokenActiveQuery::class, [get_called_class()]);
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function rules()
65
    {
66
        return array_merge(
67
            parent::rules(),
68
            $this->stateRules(),
69
            $this->providerRules(),
70
            [
71
                [
72
                    [
73
                        'accessToken',
74
                        'refreshToken'
75
                    ],
76
                    'unique'
77
                ],
78
                [
79
                    [
80
                        'dateExpires'
81
                    ],
82
                    DateTimeValidator::class
83
                ],
84
                [
85
                    [
86
                        'providerId',
87
                        'accessToken'
88
                    ],
89
                    'required'
90
                ],
91
                [
92
                    [
93
                        'accessToken',
94
                        'values',
95
                        'dateExpires'
96
                    ],
97
                    'safe',
98
                    'on' => [
99
                        ModelHelper::SCENARIO_DEFAULT
0 ignored issues
show
Deprecated Code introduced by
The constant flipbox\craft\ember\help...elper::SCENARIO_DEFAULT has been deprecated with message: Use `yii\base\Model::SCENARIO_DEFAULT`

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

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

Loading history...
100
                    ]
101
                ]
102
            ]
103
        );
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function isActive(): bool
110
    {
111
        return $this->isEnabled() && !$this->hasExpired();
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function hasExpired(): bool
118
    {
119
        $dateExpires = $this->dateExpires ?: new DateTime('now');
120
        return DateTimeHelper::isInThePast($dateExpires);
121
    }
122
123
124
    /*******************************************
125
     * PROJECT CONFIG
126
     *******************************************/
127
128
    /**
129
     * Return an array suitable for Craft's Project config
130
     */
131
    public function toProjectConfig(): array
132
    {
133
        return $this->toArray([
134
            'accessToken',
135
            'refreshToken',
136
            'providerId',
137
            'values',
138
            'enabled',
139
            'dateUpdated'
140
        ]);
141
    }
142
}
143