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

Notifications   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 116
Duplicated Lines 31.03 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 36
loc 116
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getOne() 8 8 1
A getAll() 12 12 1
A add() 8 8 1
A open() 0 9 1
A cancel() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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