Completed
Pull Request — master (#736)
by
unknown
09:44
created

FluentAuthCode::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 4
crap 1
1
<?php
2
3
/*
4
 * This file is part of OAuth 2.0 Laravel.
5
 *
6
 * (c) Luca Degasperi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LucaDegasperi\OAuth2Server\Storage;
13
14
use Carbon\Carbon;
15
use League\OAuth2\Server\Entity\AuthCodeEntity;
16
use League\OAuth2\Server\Entity\ScopeEntity;
17
use League\OAuth2\Server\Storage\AuthCodeInterface;
18
19
/**
20
 * This is the fluent auth code class.
21
 *
22
 * @author Luca Degasperi <[email protected]>
23
 */
24
class FluentAuthCode extends AbstractFluentAdapter implements AuthCodeInterface
25
{
26
    /**
27
     * Get the auth code.
28
     *
29
     * @param  string $code
30
     *
31
     * @return \League\OAuth2\Server\Entity\AuthCodeEntity
0 ignored issues
show
Documentation introduced by
Should the return type not be AuthCodeEntity|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
32
     */
33 12 View Code Duplication
    public function get($code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35 12
        $result = $this->getConnection()->table('oauth_auth_codes')
36 12
            ->where('oauth_auth_codes.id', $code)
37 12
            ->where('oauth_auth_codes.expire_time', '>=', time())
38 12
            ->first();
39
40 12
        if (is_null($result)) {
41 6
            return;
42
        }
43
44 6
        return (new AuthCodeEntity($this->getServer()))
45 6
            ->setId($result->id)
46 6
            ->setRedirectUri($result->redirect_uri)
47 6
            ->setExpireTime((int) $result->expire_time);
48
    }
49
50
    /**
51
     * Get the scopes for an access token.
52
     *
53
     * @param \League\OAuth2\Server\Entity\AuthCodeEntity $token The auth code
54
     *
55
     * @return array Array of \League\OAuth2\Server\Entity\ScopeEntity
56
     */
57 3 View Code Duplication
    public function getScopes(AuthCodeEntity $token)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59 3
        $result = $this->getConnection()->table('oauth_auth_code_scopes')
60 3
            ->select('oauth_scopes.*')
61 3
            ->join('oauth_scopes', 'oauth_auth_code_scopes.scope_id', '=', 'oauth_scopes.id')
62 3
            ->where('oauth_auth_code_scopes.auth_code_id', $token->getId())
63 3
            ->get();
64
65 3
        $scopes = [];
66
67 3
        foreach ($result as $scope) {
68 3
            $scopes[] = (new ScopeEntity($this->getServer()))->hydrate([
69 3
               'id' => $scope->id,
70 3
                'description' => $scope->description,
71 3
            ]);
72 3
        }
73
74 3
        return $scopes;
75
    }
76
77
    /**
78
     * Associate a scope with an access token.
79
     *
80
     * @param  \League\OAuth2\Server\Entity\AuthCodeEntity $token The auth code
81
     * @param  \League\OAuth2\Server\Entity\ScopeEntity $scope The scope
82
     *
83
     * @return void
84
     */
85 3 View Code Duplication
    public function associateScope(AuthCodeEntity $token, ScopeEntity $scope)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87 3
        $this->getConnection()->table('oauth_auth_code_scopes')->insert([
88 3
            'auth_code_id' => $token->getId(),
89 3
            'scope_id' => $scope->getId(),
90 3
            'created_at' => Carbon::now(),
91 3
            'updated_at' => Carbon::now(),
92 3
        ]);
93 3
    }
94
95
    /**
96
     * Delete an access token.
97
     *
98
     * @param  \League\OAuth2\Server\Entity\AuthCodeEntity $token The access token to delete
99
     *
100
     * @return void
101
     */
102 3
    public function delete(AuthCodeEntity $token)
103
    {
104 3
        $this->getConnection()->table('oauth_auth_codes')
105 3
        ->where('oauth_auth_codes.id', $token->getId())
106 3
        ->delete();
107 3
    }
108
109
    /**
110
     * Create an auth code.
111
     *
112
     * @param string $token The token ID
113
     * @param int $expireTime Token expire time
114
     * @param int $sessionId Session identifier
115
     * @param string $redirectUri Client redirect uri
116
     *
117
     * @return void
118
     */
119 3
    public function create($token, $expireTime, $sessionId, $redirectUri)
120
    {
121 3
        $this->getConnection()->table('oauth_auth_codes')->insert([
122 3
            'id' => $token,
123 3
            'session_id' => $sessionId,
124 3
            'redirect_uri' => $redirectUri,
125 3
            'expire_time' => $expireTime,
126 3
            'created_at' => Carbon::now(),
127 3
            'updated_at' => Carbon::now(),
128 3
        ]);
129 3
    }
130
}
131