Completed
Pull Request — master (#74)
by
unknown
01:30
created

Notifications::getOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace OneSignal;
4
5
use OneSignal\Resolver\ResolverFactory;
6
7
class Notifications
8
{
9
    const NOTIFICATIONS_LIMIT = 50;
10
11
    /**
12
     * @var OneSignal
13
     */
14
    protected $api;
15
16
    /**
17
     * @var ResolverFactory
18
     */
19
    private $resolverFactory;
20
21
    /**
22
     * Notifications constructor.
23
     *
24
     * @param OneSignal       $api
25
     * @param ResolverFactory $resolverFactory
26
     */
27
    public function __construct(OneSignal $api, ResolverFactory $resolverFactory)
28
    {
29
        $this->api = $api;
30
        $this->resolverFactory = $resolverFactory;
31
    }
32
33
    /**
34
     * Get information about notification with provided ID.
35
     *
36
     * Application authentication key and ID must be set.
37
     *
38
     * @param string $id Notification ID
39
     *
40
     * @return array
41
     */
42 View Code Duplication
    public function getOne($id)
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...
43
    {
44
        $url = '/notifications/'.$id.'?app_id='.$this->api->getConfig()->getApplicationId();
45
46
        return $this->api->request('GET', $url, [
47
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
48
        ]);
49
    }
50
51
    /**
52
     * Get information about all notifications.
53
     *
54
     * Application authentication key and ID must be set.
55
     *
56
     * @param int $limit  How many notifications to return (max 50)
57
     * @param int $offset Results offset (results are sorted by ID)
58
     *
59
     * @return array
60
     */
61 View Code Duplication
    public function getAll($limit = self::NOTIFICATIONS_LIMIT, $offset = 0)
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...
62
    {
63
        $query = [
64
            'limit' => max(1, min(self::NOTIFICATIONS_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))),
65
            'offset' => max(0, filter_var($offset, FILTER_VALIDATE_INT)),
66
            'app_id' => $this->api->getConfig()->getApplicationId(),
67
        ];
68
69
        return $this->api->request('GET', '/notifications?'.http_build_query($query), [
70
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
71
        ]);
72
    }
73
74
    /**
75
     * Send new notification with provided data.
76
     *
77
     * Application authentication key and ID must be set.
78
     *
79
     * @param array $data
80
     *
81
     * @return array
82
     */
83 View Code Duplication
    public function add(array $data)
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
        $data = $this->resolverFactory->createNotificationResolver()->resolve($data);
86
87
        return $this->api->request('POST', '/notifications', [
88
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
89
        ], json_encode($data));
90
    }
91
92
    /**
93
     * Open notification.
94
     *
95
     * Application authentication key and ID must be set.
96
     *
97
     * @param string $id Notification ID
98
     *
99
     * @return array
100
     */
101
    public function open($id)
102
    {
103
        return $this->api->request('PUT', '/notifications/'.$id, [
104
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
105
        ], json_encode([
106
            'app_id' => $this->api->getConfig()->getApplicationId(),
107
            'opened' => true,
108
        ]));
109
    }
110
111
    /**
112
     * Cancel notification.
113
     *
114
     * Application authentication key and ID must be set.
115
     *
116
     * @param string $id Notification ID
117
     *
118
     * @return array
119
     */
120 View Code Duplication
    public function cancel($id)
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...
121
    {
122
        $url = '/notifications/'.$id.'?app_id='.$this->api->getConfig()->getApplicationId();
123
124
        return $this->api->request('DELETE', $url, [
125
            'Authorization' => 'Basic '.$this->api->getConfig()->getApplicationAuthKey(),
126
        ]);
127
    }
128
}
129