Completed
Push — master ( 503ff2...5d1103 )
by Sherif
03:04
created

constructMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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
     * Set the notification notified to all.
19
     *
20
     * @param  array  $users_ids
21
     * @param  string $messageText
22
     * @return void
23
     */
24
    public function broadcast($users_ids, $messageText)
25
    {
26
		$devicesArray = [];
27
		$devices      = \Core::notifications()->model->whereIn('user_id', $users_ids);
28
    	foreach ($devices as $device) 
29
    	{
30
    		$devicesArray[$device->deivce_type] = \PushNotification::Device($device->device_token, array('badge' => 5));
31
    	}
32
    	
33
		$androidDevices = \PushNotification::DeviceCollection($devicesArray['ios']);
34
		$iosDevices     = \PushNotification::DeviceCollection($devicesArray['android']);
35
		$message        = constructMessage($messageText);
36
37
		$this->push('android', $androidDevices, $message);
38
		$this->push('ios', $iosDevices, $message);
39
    }
40
41
42
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
43
     * Set the notification notified to true.
44
     *
45
     * @param  string    $type
46
     * @param  colletion $devices
47
     * @param  string    $message
48
     * @return object
49
     */
50
    public function push($type, $devices, $message)
51
    {
52
		$collection = \PushNotification::app($type)->to($devices)->send($message);
53
    	foreach ($collection->pushManager as $push) 
54
    	{
55
    		$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...
56
    	}
57
    	dd($response);
0 ignored issues
show
Bug introduced by
The variable $response does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
58
    }
59
60
    /**
61
     * Construct the notification message.
62
     *
63
     * @param  string $messageText
64
     * @param  array  $options
65
     * @return object
66
     */
67
    protected function constructMessage($messageText, $options = [])
68
    {
69
    	$message = \PushNotification::Message($messageText, $options);
0 ignored issues
show
Unused Code introduced by
$message is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
    }
71
}
72