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

ExpoDatabaseDriver::retrieve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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