Completed
Branch v1 (ffe92e)
by Julián
02:44
created

Notification   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 10
c 7
b 1
f 0
lcom 2
cbo 3
dl 0
loc 163
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getMessage() 0 4 1
A setMessage() 0 6 1
A getReceivers() 0 4 1
A addReceiver() 0 6 1
A clearReceivers() 0 6 1
A getResults() 0 4 1
A addResult() 0 4 1
A clearResults() 0 6 1
1
<?php
2
/**
3
 * Push notification services abstraction (http://github.com/juliangut/tify)
4
 *
5
 * @link https://github.com/juliangut/tify for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/tify/blob/master/LICENSE
8
 */
9
10
namespace Jgut\Tify;
11
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Jgut\Tify\Receiver\AbstractReceiver;
14
15
/**
16
 * Notification handler.
17
 */
18
class Notification
19
{
20
    use ParameterTrait;
21
22
    /**
23
     * Default notification parameters.
24
     *
25
     * @see https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG
26
     *          /Chapters/TheNotificationPayload.html
27
     * @see https://developers.google.com/cloud-messaging/http-server-ref#downstream-http-messages-json
28
     *
29
     * @var array
30
     */
31
    protected $defaultParameters = [
32
        // APNS
33
        'badge' => null,
34
        'sound' => null,
35
        'content_available' => null,
36
        'category' => null,
37
        'url-args' => null,
38
        'expire' => null,
39
40
        // GCM
41
        'collapse_key' => null,
42
        'delay_while_idle' => null,
43
        'time_to_live' => 2419200,
44
        'restricted_package_name' => null,
45
        'dry_run' => null,
46
    ];
47
48
    /**
49
     * @var \Jgut\Tify\Message
50
     */
51
    protected $message;
52
53
    /**
54
     * @var \Jgut\Tify\Receiver\AbstractReceiver[]
55
     */
56
    protected $receivers = [];
57
58
    /**
59
     * Notification results.
60
     *
61
     * @var \Jgut\Tify\Result[]
62
     */
63
    protected $results;
64
65
    /**
66
     * Notification constructor.
67
     *
68
     * @param \Jgut\Tify\Message                 $message
69
     * @param \Jgut\Tify\Receiver\ApnsReceiver[] $receivers
70
     * @param array                              $parameters
71
     *
72
     * @throws \InvalidArgumentException
73
     */
74
    public function __construct(Message $message, array $receivers = [], array $parameters = [])
75
    {
76
        $this->message = $message;
77
78
        foreach ($receivers as $receiver) {
79
            $this->addReceiver($receiver);
80
        }
81
82
        $this->setParameters(array_merge($this->defaultParameters, $parameters));
83
84
        $this->results = new ArrayCollection;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type array<integer,object<Jgut\Tify\Result>> of property $results.

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...
85
    }
86
87
    /**
88
     * Get message.
89
     *
90
     * @return \Jgut\Tify\Message
91
     */
92
    public function getMessage()
93
    {
94
        return $this->message;
95
    }
96
97
    /**
98
     * Set message.
99
     *
100
     * @param \Jgut\Tify\Message $message
101
     *
102
     * @throws \InvalidArgumentException
103
     *
104
     * @return $this
105
     */
106
    public function setMessage(Message $message)
107
    {
108
        $this->message = $message;
109
110
        return $this;
111
    }
112
113
    /**
114
     * Retrieve list of receivers.
115
     *
116
     * @return \Jgut\Tify\Receiver\AbstractReceiver[]
117
     */
118
    public function getReceivers()
119
    {
120
        return array_values($this->receivers);
121
    }
122
123
    /**
124
     * Add receiver.
125
     *
126
     * @param \Jgut\Tify\Receiver\AbstractReceiver $receiver
127
     *
128
     * @return $this
129
     */
130
    public function addReceiver(AbstractReceiver $receiver)
131
    {
132
        $this->receivers[$receiver->getToken()] = $receiver;
133
134
        return $this;
135
    }
136
137
    /**
138
     * Remove all receivers.
139
     *
140
     * @return $this
141
     */
142
    public function clearReceivers()
143
    {
144
        $this->receivers = [];
145
146
        return $this;
147
    }
148
149
    /**
150
     * Retrieve results.
151
     *
152
     * @return \Jgut\Tify\Result[]
153
     */
154
    public function getResults()
155
    {
156
        return $this->results->toArray();
0 ignored issues
show
Bug introduced by
The method toArray cannot be called on $this->results (of type array<integer,object<Jgut\Tify\Result>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
157
    }
158
159
    /**
160
     * Add push result.
161
     *
162
     * @param \Jgut\Tify\Result $result
163
     */
164
    public function addResult(Result $result)
165
    {
166
        $this->results->add($result);
0 ignored issues
show
Bug introduced by
The method add cannot be called on $this->results (of type array<integer,object<Jgut\Tify\Result>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
167
    }
168
169
    /**
170
     * Clear push results.
171
     *
172
     * @return $this
173
     */
174
    public function clearResults()
175
    {
176
        $this->results->clear();
0 ignored issues
show
Bug introduced by
The method clear cannot be called on $this->results (of type array<integer,object<Jgut\Tify\Result>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
177
178
        return $this;
179
    }
180
}
181