ZendGcmPusher   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 6.98 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 1
cbo 4
dl 6
loc 86
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
D push() 6 45 9
A getSupportedPlatforms() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace WZRD\Push;
4
5
use WZRD\Contracts\Push\Notification as NotificationContract;
6
use WZRD\Contracts\Push\Pusher;
7
use Zend\Http\Client as Http;
8
use ZendService\Google\Gcm\Client;
9
use ZendService\Google\Gcm\Message;
10
11
class ZendGcmPusher implements Pusher
12
{
13
    /**
14
     * Zend GCM.
15
     *
16
     * @var ZendService\Google\Gcm\Client
17
     */
18
    private $client;
19
20
    /**
21
     * Construct with Zend GCM Client instance.
22
     *
23
     * @param ZendService\Google\Gcm\Client $client
24
     */
25
    public function __construct(Client $client)
26
    {
27
        $client->setHttpClient(new Http(null, [
28
            'adapter'       => 'Zend\Http\Client\Adapter\Socket',
29
            'sslverifypeer' => false,
30
        ]));
31
32
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<ZendService\Google\Gcm\Client> is incompatible with the declared type object<WZRD\Push\ZendService\Google\Gcm\Client> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
34
35
    /**
36
     * Push message.
37
     *
38
     * @param WZRD\Contracts\Push\Notification $notification
39
     * @param array                            $options
40
     */
41
    public function push(NotificationContract $notification, array $options = [])
42
    {
43
        // Check the supported platforms
44
        if (count(array_intersect($notification->getTargetedPlatforms(), $this->getSupportedPlatforms())) == 0) {
45
            return;
46
        }
47
48
        // Platforms & devices options
49
        $devices = $platforms_options = [];
50 View Code Duplication
        foreach ($notification->getDevices() as $platform => $data) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
            if (in_array($platform, $this->getSupportedPlatforms())) {
52
                $platforms_options = array_merge($platforms_options, $data['options']);
53
                $devices           = array_merge($devices, $data['devices']);
54
            }
55
        }
56
57
        // Prepare message
58
        $message = new Message();
59
        $message->setRegistrationIds($devices);
60
        $message->setData(array_merge($notification->getData(), [
61
            'message' => $notification->getMessage(),
62
        ]));
63
64
        if (!empty($platforms_options['collapse_key'])) {
65
            $message->setCollapseKey($platforms_options['collapse_key']);
66
        }
67
68
        if (!empty($platforms_options['restricted_package_name'])) {
69
            $message->setRestrictedPackageName($platforms_options['restricted_package_name']);
70
        }
71
72
        if (!empty($platforms_options['delay_while_idle'])) {
73
            $message->setDelayWhileIdle(true);
74
        }
75
76
        if (!empty($platforms_options['ttl'])) {
77
            $message->setTimeToLive($platforms_options['ttl']);
78
        }
79
80
        if (!empty($platforms_options['dry_run'])) {
81
            $message->setDryRun(true);
82
        }
83
84
        $this->client->send($message);
85
    }
86
87
    /**
88
     * Get supported platforms.
89
     *
90
     * @return array
91
     */
92
    public function getSupportedPlatforms()
93
    {
94
        return ['android'];
95
    }
96
}
97