ZendApnsPusher   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 7.32 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 2
dl 6
loc 82
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C push() 6 34 7
A feedback() 0 6 1
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 ZendService\Apple\Apns\Client\Message as Client;
8
use ZendService\Apple\Apns\Message;
9
10
class ZendApnsPusher implements Pusher
11
{
12
    /**
13
     * Zend APNS.
14
     *
15
     * @var ZendService\Apple\Apns\Client\Message
16
     */
17
    private $client;
18
19
    /**
20
     * Construct with Zend APNS Client instance.
21
     *
22
     * @param ZendService\Apple\Apns\Client\Message $client
23
     */
24
    public function __construct(Client $client)
25
    {
26
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<ZendService\Apple\Apns\Client\Message> is incompatible with the declared type object<WZRD\Push\ZendSer...le\Apns\Client\Message> 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...
27
    }
28
29
    /**
30
     * Push message.
31
     *
32
     * @param WZRD\Contracts\Push\Notification $notification
33
     * @param array                            $options
34
     */
35
    public function push(NotificationContract $notification, array $options = [])
36
    {
37
        // Check the supported platforms
38
        if (count(array_intersect($notification->getTargetedPlatforms(), $this->getSupportedPlatforms())) == 0) {
39
            return;
40
        }
41
42
        // Platforms & devices options
43
        $devices = $platforms_options = [];
44 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...
45
            if (in_array($platform, $this->getSupportedPlatforms())) {
46
                $platforms_options = array_merge($platforms_options, $data['options']);
47
                $devices           = array_merge($devices, $data['devices']);
48
            }
49
        }
50
51
        // Push !
52
        foreach ($devices as $device) {
53
            $message = new Message();
54
            $message->setToken($device);
55
            $message->setCustom($notification->getData());
56
            $message->setAlert($notification->getMessage());
57
58
            if (!empty($platforms_options['badge'])) {
59
                $message->setBadge($platforms_options['badge']);
60
            }
61
62
            if (!empty($platforms_options['sound'])) {
63
                $message->setSound($platforms_options['sound']);
64
            }
65
66
            $this->client->send($message);
67
        }
68
    }
69
70
    /**
71
     * Listen feedback service.
72
     *
73
     * @return array
74
     */
75
    public function feedback()
76
    {
77
        $feedback = $this->client->feedback();
78
79
        return $feedback;
80
    }
81
82
    /**
83
     * Get supported platforms.
84
     *
85
     * @return array
86
     */
87
    public function getSupportedPlatforms()
88
    {
89
        return ['ios'];
90
    }
91
}
92