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

ExpoDatabaseDriver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 51
ccs 10
cts 12
cp 0.8333
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 10 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