Completed
Push — master ( 5d1103...9c9133 )
by Sherif
02:40
created

PushNotificationDeviceRepository::broadcast()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 23
rs 8.5906
cc 5
eloc 13
nc 16
nop 3
1
<?php namespace App\Modules\V1\Notifications\Repositories;
2
3
use App\Modules\V1\Core\AbstractRepositories\AbstractRepository;
4
5
class PushNotificationDeviceRepository extends AbstractRepository
6
{
7
      /**
8
      * Return the model full namespace.
9
      * 
10
      * @return string
11
      */
12
      protected function getModel()
13
      {
14
          return 'App\Modules\V1\Notifications\PushNotificationDevice';
15
      }
16
17
    /**
18
     * Fetch the given users devices and broadcast the given
19
     * message to all of them.
20
     *
21
     * @param  array   $users_ids
22
     * @param  string  $messageText
23
     * @param  boolean $activeOnly
24
     * @return void
25
     */
26
    public function broadcast($users_ids, $messageText, $activeOnly = false)
27
    {
28
        $devicesArray = [];
29
        $devices      = $this->model->whereIn('user_id', $users_ids);
30
        $devices      = $activeOnly ? $device->where('active', 1)->get() : $devices->get();
0 ignored issues
show
Bug introduced by
The variable $device seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
31
        foreach ($devices as $device) 
32
        {
33
            $devicesArray[$device->device_type][] = \PushNotification::Device($device->device_token, array('badge' => 5));
34
        }
35
        
36
        $message = $this->constructMessage($messageText);
37
        if (array_key_exists('ios', $devicesArray)) 
38
        {
39
            $iosDevices = \PushNotification::DeviceCollection($devicesArray['ios']);
40
            $this->push('ios', $iosDevices, $message);
0 ignored issues
show
Documentation introduced by
$message is of type object, but the function expects a string.

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...
41
        }
42
43
        if (array_key_exists('android', $devicesArray)) 
44
        {
45
            $androidDevices = \PushNotification::DeviceCollection($devicesArray['android']);
46
            $this->push('android', $androidDevices, $message);
0 ignored issues
show
Documentation introduced by
$message is of type object, but the function expects a string.

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...
47
        }
48
    }
49
50
51
    /**
52
     * Push the given message to the given devices.
53
     *
54
     * @param  string    $type
55
     * @param  colletion $devices
56
     * @param  string    $message
57
     * @return void
58
     */
59
    public function push($type, $devices, $message)
60
    {
61
        $collection = \PushNotification::app($type)->to($devices)->send($message);
62
        foreach ($collection->pushManager as $push) 
63
        {
64
            $response[] = $push->getAdapter()->getResponse();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
65
        }
66
    }
67
68
    /**
69
     * Construct the notification message.
70
     *
71
     * @param  string $messageText
72
     * @param  array  $options
73
     * @return object
74
     */
75
    protected function constructMessage($messageText, $options = [])
76
    {
77
        return \PushNotification::Message($messageText, $options);
78
    }
79
}
80