Completed
Push — master ( 9ecb5e...97e3cb )
by Sherif
13:30
created

PushNotificationDeviceRepository::push()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 23
rs 9.0856
c 2
b 0
f 1
cc 3
eloc 14
nc 3
nop 4
1
<?php namespace App\Modules\V1\Notifications\Repositories;
2
3
use App\Modules\V1\Core\AbstractRepositories\AbstractRepository;
4
use LaravelFCM\Message\OptionsBuilder;
5
use LaravelFCM\Message\PayloadDataBuilder;
6
use LaravelFCM\Message\PayloadNotificationBuilder;
7
8
class PushNotificationDeviceRepository extends AbstractRepository
9
{
10
    /**
11
    * Return the model full namespace.
12
    * 
13
    * @return string
14
    */
15
    protected function getModel()
16
    {
17
        return 'App\Modules\V1\Notifications\PushNotificationDevice';
18
    }
19
20
    /**
21
     * Register the given device to the logged in user.
22
     *
23
     * @param  string $data
24
     * @return void
25
     */
26
    public function registerDevice($data)
27
    {
28
        $data['access_token'] = \Auth::user()->token();
29
        $data['user_id']      = \Auth::id();
30
        if ($device = $this->model->where('device_token', $data['device_token'])->where('user_id', $data['user_id'])->first()) 
31
        {
32
            $data['id'] = $device->id;
33
        }
34
35
        return $this->save($data);
0 ignored issues
show
Documentation introduced by
$data is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
    }
37
38
    /**
39
     * Generate the given message data.
40
     *
41
     * @param  string $title
42
     * @param  string $message
43
     * @param  string $data
44
     * @return void
45
     */
46
    public function generateMessageData($title, $message, $data = [])
47
    {
48
        $optionBuilder       = new OptionsBuilder();
49
        $notificationBuilder = new PayloadNotificationBuilder($title);
50
        $dataBuilder         = new PayloadDataBuilder();
51
52
        $optionBuilder->setTimeToLive(60*20);
53
        $notificationBuilder->setBody($message);
54
        $dataBuilder->addData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 46 can also be of type string; however, LaravelFCM\Message\PayloadDataBuilder::addData() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
55
56
        $options             = $optionBuilder->build();
57
        $notification        = $notificationBuilder->build();
58
        $data                = $dataBuilder->build();
59
60
        return compact('options', 'notification', 'data');
61
    }
62
}
63