AppsKeys   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 32
dl 0
loc 110
rs 10
c 3
b 0
f 1
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 16 1
A getSource() 0 3 1
A validateAppsKeys() 0 19 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\Models;
5
6
use Canvas\Http\Exception\UnauthorizedException;
7
8
/**
9
 * Class CompanyBranches.
10
 *
11
 * @package Canvas\Models
12
 *
13
 */
14
class AppsKeys extends AbstractModel
15
{
16
    /**
17
     *
18
     * @var integer
19
     */
20
    public $client_id;
21
22
    /**
23
     *
24
     * @var integer
25
     */
26
    public $client_secret_id;
27
28
    /**
29
     *
30
     * @var integer
31
     */
32
    public $apps_id;
33
34
    /**
35
     *
36
     * @var integer
37
     */
38
    public $users_id;
39
40
    /**
41
     *
42
     * @var string
43
     */
44
    public $last_used_date;
45
46
    /**
47
     *
48
     * @var string
49
     */
50
    public $created_at;
51
52
    /**
53
     *
54
     * @var string
55
     */
56
    public $updated_at;
57
58
    /**
59
     *
60
     * @var integer
61
     */
62
    public $is_deleted;
63
64
    /**
65
     * Initialize method for model.
66
     */
67
    public function initialize()
68
    {
69
        $this->setSource('apps_keys');
70
71
        $this->belongsTo(
72
            'users_id',
73
            Users::class,
74
            'id',
75
            ['alias' => 'users']
76
        );
77
78
        $this->belongsTo(
79
            'apps_id',
80
            Apps::class,
81
            'id',
82
            ['alias' => 'apps']
83
        );
84
    }
85
86
    /**
87
     * Returns table name mapped in the model.
88
     *
89
     * @return string
90
     */
91
    public function getSource() : string
92
    {
93
        return 'apps_keys';
94
    }
95
96
    /**
97
     * Validate Apps Keys by client id, client secret id and apps key.
98
     *
99
     * @param string $clientId
100
     * @param string $clientSecretId
101
     * @param int $appsId
102
     *
103
     * @return AppsKeys
104
     */
105
    public static function validateAppsKeys(string $clientId, string $clientSecretId, int $appsId) : self
106
    {
107
        $appkeys = AppsKeys::findFirst([
108
            'conditions' => 'client_id = ?0 and client_secret_id = ?1 and apps_id = ?2 and is_deleted = 0',
109
            'bind' => [
110
                $clientId,
111
                $clientSecretId,
112
                $appsId
113
            ]
114
        ]);
115
116
        if (!$appkeys) {
117
            throw new UnauthorizedException('Wrong Client Id or Client Secret Id given');
118
        }
119
120
        $appkeys->last_used_date = date('Y-m-d H:i:s');
121
        $appkeys->updateOrFail();
122
123
        return $appkeys;
124
    }
125
}
126