HasPushSubscriptions::updatePushSubscription()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
ccs 0
cts 24
cp 0
rs 8.439
cc 5
eloc 19
nc 3
nop 9
crap 30

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace NotificationChannels\WebPush;
4
5
trait HasPushSubscriptions
6
{
7
    /**
8
     * Get the user's subscriptions.
9
     *
10
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
11
     */
12
    public function pushSubscriptions()
13
    {
14
        return $this->hasMany(PushSubscription::class);
0 ignored issues
show
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
15
    }
16
17
    /**
18
     * Update (or create) user subscription.
19
     *
20
     * @param  string $endpoint
21
     * @param  string|null $key
22
     * @param  string|null $token
23
     * @return \NotificationChannels\WebPush\PushSubscription
24
     */
25
    public function updatePushSubscription($endpoint, $key = null, $token = null, $dd = null, $push_subscription_id = null, $name = null, $language = null, $device = null, $country = null)
26
    {
27
        $subscription = PushSubscription::findByEndpoint($endpoint);
28
        if ($subscription && $this->pushSubscriptionBelongsToUser($subscription)) {
29
            $subscription->public_key = $key;
30
            $subscription->auth_token = $token;
31
            $subscription->browser = $dd;
32
            $subscription->push_subscription_id = $push_subscription_id;
33
            $subscription->name = $name;
34
            $subscription->language = $language;
35
            $subscription->device = $device;
36
            $subscription->country = $country;
37
            $subscription->save();
38
39
            return $subscription;
40
        }
41
42
        if ($subscription && ! $this->pushSubscriptionBelongsToUser($subscription)) {
43
            $subscription->delete();
44
        }
45
46
        return $this->pushSubscriptions()->save(new PushSubscription([
47
            'endpoint' => $endpoint,
48
            'public_key' => $key,
49
            'auth_token' => $token,
50
        ]));
51
    }
52
53
    /**
54
     * Determine if the given subscription belongs to this user.
55
     *
56
     * @param  \NotificationChannels\WebPush\PushSubscription $subscription
57
     * @return bool
58
     */
59
    public function pushSubscriptionBelongsToUser($subscription)
60
    {
61
        return (int) $subscription->user_id === 1;
62
    }
63
64
    /**
65
     * Delete subscription by endpoint.
66
     *
67
     * @param  string $endpoint
68
     * @return void
69
     */
70
    public function deletePushSubscription($endpoint)
71
    {
72
        $this->pushSubscriptions()
73
            ->where('endpoint', $endpoint)
74
            ->delete();
75
    }
76
77
    /**
78
     * Get all subscriptions.
79
     *
80
     * @return \Illuminate\Database\Eloquent\Collection
81
     */
82
    public function routeNotificationForWebPush()
83
    {
84
        return $this->pushSubscriptions;
0 ignored issues
show
Bug introduced by
The property pushSubscriptions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
85
    }
86
}
87