Passed
Push — master ( 5d259d...069a18 )
by Conrad
02:00
created

src/Models/Client.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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