ParsePusher::push()   C
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 34
Code Lines 20

Duplication

Lines 6
Ratio 17.65 %

Importance

Changes 4
Bugs 2 Features 1
Metric Value
c 4
b 2
f 1
dl 6
loc 34
rs 5.3846
cc 8
eloc 20
nc 7
nop 2
1
<?php
2
3
namespace WZRD\Push;
4
5
use Parse\ParseInstallation;
6
use Parse\ParsePush;
7
use Parse\ParseQuery;
8
use WZRD\Contracts\Push\Notification as NotificationContract;
9
use WZRD\Contracts\Push\Pusher;
10
11
class ParsePusher implements Pusher
12
{
13
    /**
14
     * Push message.
15
     *
16
     * @param WZRD\Contracts\Push\Notification $notification
17
     * @param array                            $options
18
     *
19
     * Parse's specifics options :
20
     * <code>
21
     * $array = array(
22
     *     'parse_query' => ParseInstallation::query(),
23
     *     'parse_channels' => array('channel1', 'channel2'),
24
     *     'parse_expiration_time' => new DateTime(),
25
     *     'parse_push_time' => new DateTime()
26
     * );
27
     * </code>
28
     */
29
    public function push(NotificationContract $notification, array $options = [])
30
    {
31
        // Check the supported platforms
32
        if (count(array_intersect($notification->getTargetedPlatforms(), $this->getSupportedPlatforms())) == 0) {
33
            return;
34
        }
35
36
        // Platforms & devices options
37
        $devices = $platforms_options = [];
38 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...
39
            if (in_array($platform, $this->getSupportedPlatforms())) {
40
                $platforms_options = array_merge($platforms_options, $data['options']);
41
                $devices           = array_merge($devices, $data['devices']);
42
            }
43
        }
44
45
        // Query the devices
46
        if (empty($options['parse_query'])) {
47
            $query = $this->parseQuery();
48
            $query->equalTo('deviceToken', $devices);
49
        } else {
50
            $query = $options['parse_query'];
51
        }
52
53
        // Push
54
        $this->parsePushSend([
55
            'where'           => $query,
56
            'channels'        => !empty($options['parse_channels']) ? $options['parse_channels'] : null,
57
            'expiration_time' => !empty($options['parse_expiration_time']) ? $options['parse_expiration_time'] : null,
58
            'push_time'       => !empty($options['parse_push_time']) ? $options['parse_push_time'] : null,
59
            'data'            => array_merge($notification->getData(), $platforms_options),
60
            'alert'           => $notification->getMessage(),
61
        ]);
62
    }
63
64
    /**
65
     * Create Parse query.
66
     *
67
     * @return Parse\ParseQuery
68
     */
69
    protected function parseQuery()
70
    {
71
        return ParseInstallation::query();
72
    }
73
74
    /**
75
     * Do Parse Push.
76
     *
77
     * @param array $data
78
     */
79
    protected function parsePushSend($data)
80
    {
81
        ParsePush::send($data);
82
    }
83
84
    /**
85
     * Get supported platforms.
86
     *
87
     * @return array
88
     */
89
    public function getSupportedPlatforms()
90
    {
91
        return ['ios', 'android', 'winphone'];
92
    }
93
}
94