Completed
Push — master ( bad662...5b32ef )
by Aly
11:55
created

ExpoDatabaseDriver::forget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
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 3
    public function store($key, $value): bool
19
    {
20 3
        $interest = Interest::firstOrCreate([
21 3
            'key' => $key,
22 3
            'value' => $value,
23
        ]);
24
25 3
        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 2
    public function forget(string $key, string $value = null): bool
49
    {
50 2
        $query = Interest::where('key', $key);
51
52 2
        if ($value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
53 1
            $query->where('value', $value);
54
        }
55
56 2
        return $query->delete() > 0;
57
    }
58
}
59