Failed Conditions
Push — master ( 05d004...385b2f )
by Maximo
19s queued 13s
created

AppsKeys   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 29
dl 0
loc 105
rs 10
c 2
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 15 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
     * @param string $clientId
99
     * @param string $clientSecretId
100
     * @param int $appsId
101
     * 
102
     * @return AppsKeys
103
     */
104
    public static function validateAppsKeys(string $clientId, string $clientSecretId, int $appsId): self
105
    {
106
        $appkeys = AppsKeys::findFirst([
107
            'conditions' => 'client_id = ?0 and client_secret_id = ?1 and apps_id = ?2 and is_deleted = 0',
108
            'bind' => [$clientId, $clientSecretId, $appsId]
109
        ]);
110
111
        if (!$appkeys){
112
            throw new UnauthorizedException("Wrong Client Id or Client Secret Id given");
113
        }
114
115
        $appkeys->last_used_date = date('Y-m-d H:i:s');
116
        $appkeys->update();
117
118
        return $appkeys;
119
    }
120
}
121