TokenRepository::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * TokenRepository.
4
 */
5
6
namespace Bmatovu\MtnMomo\Repositories;
7
8
use Bmatovu\MtnMomo\Models\Token;
9
use Bmatovu\OAuthNegotiator\Repositories\TokenRepositoryInterface;
10
use Carbon\Carbon;
11
12
/**
13
 * Token repository.
14
 */
15
class TokenRepository implements TokenRepositoryInterface
16
{
17
    /**
18
     * Product.
19
     *
20
     * @var string
21
     */
22
    protected $product;
23
24
    /**
25
     * Constructor.
26
     *
27
     * @param string $product
28
     */
29 10
    public function __construct($product)
30
    {
31 10
        $this->product = $product;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function create(array $attributes)
38
    {
39 1
        $attributes['token_type'] = 'Bearer';
40 1
        $attributes['product'] = $this->product;
41
42 1
        if (isset($attributes['expires_in'])) {
43 1
            $attributes['expires_at'] = Carbon::now()->addSeconds($attributes['expires_in']);
44
        }
45
46 1
        return Token::create($attributes);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 2
    public function retrieveAll()
53
    {
54 2
        return Token::where('product', $this->product)->get();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function retrieve($access_token = null)
61
    {
62 2
        if ($access_token) {
63 1
            return Token::query()
64 1
                ->where('access_token', $access_token)
65 1
                ->where('product', $this->product)
66 1
                ->first();
67
        }
68
69 1
        return Token::query()
70 1
            ->where('product', $this->product)
71 1
            ->latest('created_at')
72 1
            ->first();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 1
    public function update($access_token, array $attributes)
79
    {
80 1
        $token = Token::query()
81 1
            ->where('access_token', $access_token)
82 1
            ->where('product', $this->product)
83 1
            ->first();
84
85 1
        $token->update($attributes);
86
87 1
        return $token->fresh();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1
    public function delete($access_token)
94
    {
95 1
        Token::query()
96 1
            ->where('access_token', $access_token)
97 1
            ->where('product', $this->product)
98 1
            ->delete();
99
    }
100
}
101