Notifications::getOne()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace vasadibt\onesignal\service;
4
5
6
use vasadibt\onesignal\resolver\ResolverFactory;
7
8
/**
9
 * Class Notifications
10
 * @package vasadibt\onesignal\service
11
 */
12
class Notifications extends Request
13
{
14
    const NOTIFICATIONS_LIMIT = 50;
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public $methodName = self::NOTIFICATIONS;
20
21
    /**
22
     * Get information about notification with provided ID.
23
     *
24
     * @param string $id Notification ID
25
     *
26
     * @return array
27
     * @throws \GuzzleHttp\Exception\GuzzleException
28
     * @throws \yii\web\BadRequestHttpException
29
     */
30
    public function getOne($id)
31
    {
32
        return $this->get($id);
33
    }
34
35
    /**
36
     * Get information about all notifications.
37
     *
38
     * @param int $limit How many notifications to return (max 50)
39
     * @param int $offset Results offset (results are sorted by ID)
40
     *
41
     * @return array
42
     * @throws \GuzzleHttp\Exception\GuzzleException
43
     * @throws \yii\web\BadRequestHttpException
44
     */
45
    public function getAll($limit = self::NOTIFICATIONS_LIMIT, $offset = 0)
46
    {
47
        return $this->get(null, [
48
            'limit' => max(1, min(self::NOTIFICATIONS_LIMIT, filter_var($limit, FILTER_VALIDATE_INT))),
49
            'offset' => max(0, filter_var($offset, FILTER_VALIDATE_INT)),
50
        ]);
51
    }
52
53
    /**
54
     * Send new notification with provided data.
55
     *
56
     * @param array $data
57
     *
58
     * @return array
59
     * @throws \GuzzleHttp\Exception\GuzzleException
60
     * @throws \yii\web\BadRequestHttpException
61
     */
62
    public function add(array $data)
63
    {
64
        return $this->post(null, ResolverFactory::createNotificationResolver($this->api)->resolve($data));
65
    }
66
67
    /**
68
     * Open notification.
69
     *
70
     * @param string $id Notification ID
71
     *
72
     * @return array
73
     * @throws \GuzzleHttp\Exception\GuzzleException
74
     * @throws \yii\web\BadRequestHttpException
75
     */
76
    public function open($id)
77
    {
78
        return $this->put($id, ['opened' => true]);
79
    }
80
81
    /**
82
     * Cancel notification.
83
     *
84
     * @param string $id Notification ID
85
     *
86
     * @return array
87
     * @throws \GuzzleHttp\Exception\GuzzleException
88
     * @throws \yii\web\BadRequestHttpException
89
     */
90
    public function cancel($id)
91
    {
92
        return $this->delete($id);
93
    }
94
}
95