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

Notifications::resolve()   D

Complexity

Conditions 18
Paths 1

Size

Total Lines 225
Code Lines 189

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 225
rs 4.7996
c 0
b 0
f 0
cc 18
eloc 189
nc 1
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Notifications::cancel() 8 8 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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