Client   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 1
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasGrantType() 0 6 2
A onBeforeWrite() 0 12 3
A generateSecret() 0 4 1
1
<?php
2
3
namespace AdvancedLearning\Oauth2Server\Models;
4
5
use function base64_encode;
6
use SilverStripe\ORM\DataObject;
7
8
/**
9
 * Stores ClientEntity information.
10
 *
11
 * @package AdvancedLearning\Oauth2Server\Models
12
 *
13
 * @property string $Grants
14
 * @property string $Name
15
 * @property string $Secret
16
 * @property string $Identifier
17
 */
18
class Client extends DataObject
19
{
20
    private static $table_name = 'OauthClient';
0 ignored issues
show
Comprehensibility introduced by Conrad Dobbs
Consider using a different property name as you override a private property of the parent class.
Loading history...
21
22
    private static $db = [
0 ignored issues
show
Comprehensibility introduced by Conrad Dobbs
Consider using a different property name as you override a private property of the parent class.
Loading history...
23
        'Name' => 'Varchar(100)',
24
        'Grants' => 'Varchar(255)',
25
        'Secret' => 'Varchar(255)',
26
        'Identifier' => 'Varchar(255)'
27
    ];
28
29
    private static $summary_fields = [
0 ignored issues
show
Comprehensibility introduced by Conrad Dobbs
Consider using a different property name as you override a private property of the parent class.
Loading history...
30
        'Name'
31
    ];
32
33
    /**
34
     * Checks whether this ClientEntity has the given grant type.
35
     *
36
     * @param string $grantType The grant type to check.
37
     *
38
     * @return boolean
39
     */
40
    public function hasGrantType($grantType)
41
    {
42
        $grants = explode(',', $this->Grants);
43
44
        return !empty($grants) && in_array($grantType, $grants);
45
    }
46
47
    /**
48
     * On before write. Generate a secret if we don't have one.
49
     */
50
    public function onBeforeWrite()
51
    {
52
        parent::onBeforeWrite();
53
54
        if (empty($this->Secret)) {
55
            $this->Secret = $this->generateSecret();
56
        }
57
58
        if (empty($this->Identifier)) {
59
            $this->Identifier = $this->generateSecret();
60
        }
61
    }
62
63
    /**
64
     * Generate a random secret.
65
     *
66
     * @return string
67
     */
68
    protected function generateSecret()
69
    {
70
        return base64_encode(random_bytes(32));
71
    }
72
}
73