PushNotification::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
namespace nstdio\notymo;
3
use Closure;
4
5
/**
6
 * Class PushNotificationComponent
7
 */
8
class PushNotification implements PushNotificationInterface, LifeCycleCallback
9
{
10
    /**
11
     * @var PushNotificationInterface[]
12
     */
13
    private $notificationImpl = array();
14
15
    /**
16
     * PushNotification constructor.
17
     *
18
     * @param array $config Configuration for notification implementations.
19
     *
20
     * Available keys.
21
     * - skipApns Whether use APNS notifications.
22
     * - skipGcm  Whether use GCM notifications
23
     * - apns
24
     *      - live
25
     *      - cert
26
     *      - sandboxCert
27
     * - gcm
28
     *      - apiKey Google Api key for GCM service.
29
     */
30 3
    public function __construct($config = array())
31
    {
32 3
        $this->notificationImpl = array();
33
34 3
        $this->initApns($config);
35 2
        $this->initGcm($config);
36 1
    }
37
38
    /**
39
     * @param $config
40
     */
41 3
    private function initApns($config)
42
    {
43 3
        if (!isset($config['skipApns'])) {
44 2
            if (!isset($config['apns'])) {
45 1
                throw new \InvalidArgumentException("Configuration required for APNSNotification.");
46
            }
47
48 1
            $args = array_merge(array('live' => false, 'cert' => null, 'sandboxCert' => null), $config['apns']);
49 1
            $this->notificationImpl['apns'] = new APNSNotification($args['live'], $args['cert'], $args['sandboxCert']);
0 ignored issues
show
Bug introduced by
It seems like $args['live'] can also be of type null; however, nstdio\notymo\APNSNotification::__construct() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Security Bug introduced by
It seems like $args['cert'] can also be of type false; however, nstdio\notymo\APNSNotification::__construct() does only seem to accept string|null, did you maybe forget to handle an error condition?
Loading history...
Security Bug introduced by
It seems like $args['sandboxCert'] can also be of type false; however, nstdio\notymo\APNSNotification::__construct() does only seem to accept string|null, did you maybe forget to handle an error condition?
Loading history...
50 1
        }
51 2
    }
52
53
    /**
54
     * @param $config
55
     */
56 2
    private function initGcm($config)
57
    {
58 2
        if (!isset($config['skipGcm'])) {
59 2
            if (!isset($config['gcm']) || !isset($config['gcm']['apiKey'])) {
60 1
                throw new \InvalidArgumentException("Configuration required for GCMNotification.");
61
            }
62 1
            $this->notificationImpl['gcm'] = new GCMNotification($config['gcm']['apiKey']);
63 1
        }
64 1
    }
65
66
    /**
67
     *
68
     */
69
    public function send()
70
    {
71
        $this->invokeMethod('send');
72
    }
73
74
    private function invokeMethod($method, $args = array())
75
    {
76
        foreach ($this->notificationImpl as $item) {
77
            call_user_func_array(array($item, $method), $args);
78
        }
79
    }
80
81
    public function enqueue(MessageInterface $message)
82
    {
83
        if ($message->getType() === MessageInterface::TYPE_IOS && isset($this->notificationImpl['apns'])) {
84
            $this->notificationImpl['apns']->enqueue($message);
85
        } elseif ($message->getType() === MessageInterface::TYPE_ANDROID && isset($this->notificationImpl['gcm'])) {
86
            $this->notificationImpl['gcm']->enqueue($message);
87
        }
88
    }
89
90
    public function setStreamWrapper(Connection $wrapper)
91
    {
92
        throw new \RuntimeException('Not yet implemented.');
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function onComplete(\Closure $param)
99
    {
100
        $this->invokeMethod('onSent', array($param));
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function onEachSent(\Closure $callback)
107
    {
108
        $this->invokeMethod('onEachSent', array($callback));
109
    }
110
111
    /**
112
     * Removes all callbacks.
113
     */
114
    public function detach()
115
    {
116
        $this->invokeMethod('detach');
117
    }
118
119
    /**
120
     * @param int $count
121
     */
122
    public function setRetryCount($count)
123
    {
124
        $this->invokeMethod('setRetryCount', array($count));
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130
    public function onError(Closure $callback)
131
    {
132
        $this->invokeMethod('onError', array($callback));
133
    }
134
}
135