TokenRepository::retrieve()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
ccs 4
cts 4
cp 1
crap 2
1
<?php
2
3
namespace Bmatovu\AirtelMoney\Auth\Repositories;
4
5
use Bmatovu\AirtelMoney\Models\Token;
6
use Carbon\Carbon;
7
use Illuminate\Database\Eloquent\Model;
8
9
class TokenRepository implements TokenRepositoryInterface
10
{
11 6
    public function create(array $tokenAttrs): Model
12
    {
13 6
        $tokenAttrs['token_type'] = 'Bearer';
14
15 6
        if (isset($tokenAttrs['expires_in'])) {
16 6
            $tokenAttrs['expires_at'] = Carbon::now()->addSeconds($tokenAttrs['expires_in']);
17
        }
18
19 6
        return Token::create($tokenAttrs);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Bmatovu\AirtelMon...en::create($tokenAttrs) could return the type Illuminate\Database\Eloq...gHasThroughRelationship which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Model. Consider adding an additional type-check to rule them out.
Loading history...
20
    }
21
22 1
    public function retrieveAll()
23
    {
24 1
        return Token::get();
25
    }
26
27 8
    public function retrieve(?string $accessToken = null): ?Model
28
    {
29 8
        if ($accessToken) {
30 1
            return Token::where('access_token', $accessToken)->first();
31
        }
32
33 7
        return Token::latest('created_at')->first();
34
    }
35
36 1
    public function update(string $accessToken, array $tokenAttrs): Model
37
    {
38 1
        $token = Token::where('access_token', $accessToken)->firstOrFail();
39
40 1
        $token->update($tokenAttrs);
41
42 1
        return $token->fresh();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $token->fresh() could return the type null which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Model. Consider adding an additional type-check to rule them out.
Loading history...
43
    }
44
45 2
    public function delete(string $accessToken): void
46
    {
47 2
        Token::where('access_token', $accessToken)->delete();
48
    }
49
}
50