Notifications::cancel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace OneSignal;
6
7
use OneSignal\Resolver\ResolverFactory;
8
use ReflectionMethod;
9
use function count;
10
11
class Notifications extends AbstractApi
12
{
13
    private $resolverFactory;
14
15
    public function __construct(OneSignal $client, ResolverFactory $resolverFactory)
16
    {
17
        parent::__construct($client);
18
19
        $this->resolverFactory = $resolverFactory;
20
    }
21
22
    /**
23
     * Get information about notification with provided ID.
24
     *
25
     * Application authentication key and ID must be set.
26
     *
27
     * @param string $id Notification ID
28
     */
29 View Code Duplication
    public function getOne(string $id): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
30
    {
31
        $request = $this->createRequest('GET', "/notifications/$id?app_id={$this->client->getConfig()->getApplicationId()}");
32
        $request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}");
33
34
        return $this->client->sendRequest($request);
35
    }
36
37
    /**
38
     * Get information about all notifications.
39
     *
40
     * Application authentication key and ID must be set.
41
     *
42
     * @param int $limit  How many notifications to return (max 50)
43
     * @param int $offset Results offset (results are sorted by ID)
44
     * @param int $kind   Kind of notifications returned. Default (not set) is all notification types
0 ignored issues
show
Bug introduced by
There is no parameter named $kind. 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...
45
     */
46
    public function getAll(int $limit = null, int $offset = null/*, int $kind = null */): array
47
    {
48
        if (func_num_args() > 2 && !is_int(func_get_arg(2))) {
49
            trigger_deprecation('norkunas/onesignal-php-api', '2.1.0', 'Method %s() will have a third `int $kind` argument. Not defining it or passing a non integer value is deprecated.', __METHOD__);
50
        } elseif (__CLASS__ !== static::class) {
51
            $r = new ReflectionMethod($this, __FUNCTION__);
52
53
            if (count($r->getParameters()) > 2) {
54
                trigger_deprecation('norkunas/onesignal-php-api', '2.1.0', 'Method %s() will have a third `int $kind` argument. Not defining it or passing a non integer value is deprecated.', __METHOD__);
55
            }
56
        }
57
58
        $query = ['app_id' => $this->client->getConfig()->getApplicationId()];
59
60
        if ($limit !== null) {
61
            $query['limit'] = $limit;
62
        }
63
64
        if ($offset !== null) {
65
            $query['offset'] = $offset;
66
        }
67
68
        if (func_num_args() > 2 && is_int(func_get_arg(2))) {
69
            $query['kind'] = func_get_arg(2);
70
        }
71
72
        $request = $this->createRequest('GET', '/notifications?'.http_build_query($query));
73
        $request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}");
74
75
        return $this->client->sendRequest($request);
76
    }
77
78
    /**
79
     * Send new notification with provided data.
80
     *
81
     * Application authentication key and ID must be set.
82
     */
83 View Code Duplication
    public function add(array $data): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
84
    {
85
        $resolvedData = $this->resolverFactory->createNotificationResolver()->resolve($data);
86
87
        $request = $this->createRequest('POST', '/notifications');
88
        $request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}");
89
        $request = $request->withHeader('Content-Type', 'application/json');
90
        $request = $request->withBody($this->createStream($resolvedData));
91
92
        return $this->client->sendRequest($request);
93
    }
94
95
    /**
96
     * Open notification.
97
     *
98
     * Application authentication key and ID must be set.
99
     *
100
     * @param string $id Notification ID
101
     */
102
    public function open(string $id): array
103
    {
104
        $request = $this->createRequest('PUT', "/notifications/$id");
105
        $request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}");
106
        $request = $request->withHeader('Content-Type', 'application/json');
107
        $request = $request->withBody($this->createStream([
108
            'app_id' => $this->client->getConfig()->getApplicationId(),
109
            'opened' => true,
110
        ]));
111
112
        return $this->client->sendRequest($request);
113
    }
114
115
    /**
116
     * Cancel notification.
117
     *
118
     * Application authentication key and ID must be set.
119
     *
120
     * @param string $id Notification ID
121
     */
122 View Code Duplication
    public function cancel(string $id): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
123
    {
124
        $request = $this->createRequest('DELETE', "/notifications/$id?app_id={$this->client->getConfig()->getApplicationId()}");
125
        $request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}");
126
127
        return $this->client->sendRequest($request);
128
    }
129
130
    /**
131
     * View the devices sent a notification.
132
     *
133
     * Application authentication key and ID must be set.
134
     *
135
     * @param string $id Notification ID
136
     */
137
    public function history(string $id, array $data): array
138
    {
139
        $resolvedData = $this->resolverFactory->createNotificationHistoryResolver()->resolve($data);
140
141
        $request = $this->createRequest('POST', "/notifications/$id/history");
142
        $request = $request->withHeader('Authorization', "Basic {$this->client->getConfig()->getApplicationAuthKey()}");
143
        $request = $request->withHeader('Cache-Control', 'no-cache');
144
        $request = $request->withHeader('Content-Type', 'application/json');
145
        $request = $request->withBody($this->createStream($resolvedData));
146
147
        return $this->client->sendRequest($request);
148
    }
149
}
150