Completed
Pull Request — master (#6)
by Mark
04:53
created

IonicPushMessage::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 12
1
<?php
2
3
namespace NotificationChannels\IonicPushNotifications;
4
5
class IonicPushMessage
6
{
7
    /** @var string */
8
    protected $sendTo = 'tokens';
9
10
    /** @var string */
11
    protected $profile;
12
13
    /** @var string */
14
    protected $title = '';
15
16
    /** @var string */
17
    protected $message = '';
18
19
    /** @var string */
20
    protected $sound = '';
21
22
    /** @var array */
23
    protected $payload = [];
24
25
    /** @var array */
26
    protected $iosData = [];
27
28
    /** @var array */
29
    protected $androidData = [];
30
31
    /**
32
     * @param array $data
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
33
     *
34
     * @return static
35
     */
36
    public static function create($title, $message)
37
    {
38
        return new static($title, $message);
39
    }
40
41
    /**
42
     * @param string $title
43
     * @param string $message
44
     */
45
    public function __construct($title, $message)
46
    {
47
        $this->title = $title;
48
        $this->message = $message;
49
    }
50
51
    /**
52
     * Set the security profile to use.
53
     *
54
     * @param  string  $profile
55
     *
56
     * @return $this
57
     */
58
    public function profile($profile)
59
    {
60
        $this->profile = $profile;
61
62
        return $this;
63
    }
64
65
    /**
66
     * Set the method of targeting users - tokens (default), user_ids, or emails.
67
     *
68
     * @param  string  $profile
0 ignored issues
show
Bug introduced by
There is no parameter named $profile. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
69
     *
70
     * @return $this
71
     */
72
    public function sendTo($sendTo)
73
    {
74
        $this->sendTo = $sendTo;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Set the security sound to use.
81
     *
82
     * @param  string  $sound
83
     *
84
     * @return $this
85
     */
86
    public function sound($sound)
87
    {
88
        $this->sound = $sound;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Send custom key/value data with your notifications.
95
     *
96
     * @param  array  $payload
97
     *
98
     * @return $this
99
     */
100
    public function payload($payload)
101
    {
102
        $this->payload = $payload;
103
104
        return $this;
105
    }
106
107
    /**
108
     * Dynamically add device specific data.
109
     *
110
     * @param string $method
111
     * @param array  $args
112
     *
113
     * @return object
114
     */
115
    public function __call($method, $args)
116
    {
117
        if (substr($method, 0, 3) == 'ios') {
118
            $key = snake_case(substr($method, 3));
119
120
            $this->iosData[$key] = $args[0];
121
        } elseif (substr($method, 0, 7) == 'android') {
122
            $key = snake_case(substr($method, 7));
123
124
            $this->androidData[$key] = $args[0];
125
        }
126
127
        return $this;
128
    }
129
130
    /**
131
     * Get the method we want to use to send messages.
132
     *    
133
     * @return string
134
     */
135
    public function getSendToType()
136
    {
137
        return $this->sendTo;
138
    }
139
140
    /**
141
     * @return array
142
     */
143
    public function toArray()
144
    {
145
        $data = [
146
            'profile' => $this->profile,
147
            'notification' => [
148
                'title' => $this->title,
149
                'message' => $this->message,
150
                'sound' => $this->sound,
151
            ],
152
        ];
153
154
        if (! empty($this->iosData)) {
155
            $data['notification']['ios'] = $this->iosData;
156
        }
157
158
        if (! empty($this->androidData)) {
159
            $data['notification']['android'] = $this->androidData;
160
        }
161
162
        if (! empty($this->payload)) {
163
            $data['notification']['payload'] = $this->payload;
164
        }
165
166
        return $data;
167
    }
168
}
169