Completed
Pull Request — master (#12)
by
unknown
06:28 queued 02:32
created

ExpoDatabaseDriver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 31.25%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 62
ccs 5
cts 16
cp 0.3125
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 9 1
A retrieve() 0 4 1
A forget() 0 21 3
1
<?php
2
3
namespace NotificationChannels\ExpoPushNotifications\Repositories;
4
5
use ExponentPhpSDK\ExpoRepository;
6
use NotificationChannels\ExpoPushNotifications\Models\Interest;
7
8
class ExpoDatabaseDriver implements ExpoRepository
9
{
10
    /**
11
     * Stores an Expo token with a given identifier.
12
     *
13
     * @param $key
14
     * @param $value
15
     *
16
     * @return bool
17
     */
18 1
    public function store($key, $value): bool
19
    {
20 1
        $interest = Interest::firstOrCreate([
21 1
            'key' => $key,
22 1
            'value' => $value,
23
        ]);
24
25 1
        return $interest instanceof Interest;
26
    }
27
28
    /**
29
     * Retrieves an Expo token with a given identifier.
30
     *
31
     * @param string $key
32
     *
33
     * @return array
34
     */
35
    public function retrieve(string $key)
36
    {
37
        return Interest::where('key', $key)->pluck('value')->toArray();
38
    }
39
40
    /**
41
     * Removes an Expo token with a given identifier.
42
     *
43
     * @param string $key
44
     * @param string $value
45
     *
46
     * @return bool
47
     */
48
    public function forget(string $key, string $value = null): bool
49
    {
50
        // Delete interest
51
        $delete = Interest::where('key', $key);
52
53
        if (isset($value)) {
54
            // Only delete this token
55
            $delete->where('value', $value);
56
        }
57
58
        $delete->delete();
59
60
        // Check if our interest exist
61
        $count = Interest::where('key', $key);
62
63
        if (isset($value)) {
64
            $count->where('value', $value);
65
        }
66
67
        return $count->count() === 0;
68
    }
69
}
70