Notifications   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 167
Duplicated Lines 6.59 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 11
loc 167
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A listNotifications() 0 10 1
A listRepositoryNotifications() 0 11 1
A markAsRead() 0 6 1
A viewThread() 0 4 1
A markThreadAsRead() 0 5 1
A getThreadSubscription() 0 5 1
A setThreadSubscription() 0 5 1
A deleteThreadSubscription() 11 11 2
A markAsReadInRepository() 0 7 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
namespace FlexyProject\GitHub\Receiver\Activity;
3
4
use DateTime;
5
use Symfony\Component\HttpFoundation\Request;
6
7
/**
8
 * The Notifications API class lets your view notifications of new comments are delivered to users and mark them as
9
 * read.
10
 *
11
 * @link    https://developer.github.com/v3/activity/notifications/
12
 * @package GitHub\Receiver\Activity
13
 */
14
class Notifications extends AbstractActivity
15
{
16
17
    /**
18
     * List your notifications
19
     *
20
     * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications
21
     *
22
     * @param bool   $all
23
     * @param bool   $participating
24
     * @param string $since
25
     * @param string $before
26
     *
27
     * @return array
28
     * @throws \Exception
29
     */
30
    public function listNotifications(bool $all = false, bool $participating = false, string $since = 'now',
31
                                      string $before = null): array
32
    {
33
        return $this->getApi()->request($this->getApi()->sprintf('/notifications?:args', http_build_query([
34
            'all'           => $all,
35
            'participating' => $participating,
36
            'since'         => (new DateTime($since))->format(DateTime::ATOM),
37
            'before'        => (new DateTime($before))->format(DateTime::ATOM)
38
        ])));
39
    }
40
41
    /**
42
     * List your notifications in a repository
43
     *
44
     * @link https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository
45
     *
46
     * @param bool   $all
47
     * @param bool   $participating
48
     * @param string $since
49
     * @param string $before
50
     *
51
     * @return array
52
     * @throws \Exception
53
     */
54
    public function listRepositoryNotifications(bool $all = false, bool $participating = false, string $since = 'now',
55
                                                string $before = null): array
56
    {
57
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args',
58
            $this->getActivity()->getOwner(), $this->getActivity()->getRepo(), http_build_query([
59
                'all'           => $all,
60
                'participating' => $participating,
61
                'since'         => (new DateTime($since))->format(DateTime::ATOM),
62
                'before'        => (new DateTime($before))->format(DateTime::ATOM)
63
            ])));
64
    }
65
66
    /**
67
     * Mark as read
68
     *
69
     * @link https://developer.github.com/v3/activity/notifications/#mark-as-read
70
     *
71
     * @param string $lastReadAt
72
     *
73
     * @return array
74
     */
75
    public function markAsRead(string $lastReadAt = 'now'): array
76
    {
77
        return $this->getApi()->request($this->getApi()->sprintf('/notifications?:args',
78
            http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])),
79
            Request::METHOD_PUT);
80
    }
81
82
    /**
83
     * Mark notifications as read in a repository
84
     *
85
     * @link https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository
86
     *
87
     * @param string $lastReadAt
88
     *
89
     * @return array
90
     */
91
    public function markAsReadInRepository(string $lastReadAt = 'now'): array
92
    {
93
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/notifications?:args',
94
            $this->getActivity()->getOwner(), $this->getActivity()->getRepo(),
95
            http_build_query(['last_read_at' => (new DateTime($lastReadAt))->format(DateTime::ATOM)])),
96
            Request::METHOD_PUT);
97
    }
98
99
    /**
100
     * View a single thread
101
     *
102
     * @link https://developer.github.com/v3/activity/notifications/#view-a-single-thread
103
     *
104
     * @param int $id
105
     *
106
     * @return array
107
     */
108
    public function viewThread(int $id): array
109
    {
110
        return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id', (string)$id));
111
    }
112
113
    /**
114
     * Mark a thread as read
115
     *
116
     * @link https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read
117
     *
118
     * @param int $id
119
     *
120
     * @return array
121
     */
122
    public function markThreadAsRead(int $id): array
123
    {
124
        return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id', (string)$id),
125
            Request::METHOD_PATCH);
126
    }
127
128
    /**
129
     * Get a Thread Subscription
130
     *
131
     * @link https://developer.github.com/v3/activity/notifications/#get-a-thread-subscription
132
     *
133
     * @param int $id
134
     *
135
     * @return array
136
     */
137
    public function getThreadSubscription(int $id): array
138
    {
139
        return $this->getApi()->request($this->getApi()
140
                                             ->sprintf('/notifications/threads/:id/subscription', (string)$id));
141
    }
142
143
    /**
144
     * Set a Thread Subscription
145
     *
146
     * @link https://developer.github.com/v3/activity/notifications/#set-a-thread-subscription
147
     *
148
     * @param int  $id
149
     * @param bool $subscribed
150
     * @param bool $ignored
151
     *
152
     * @return array
153
     */
154
    public function setThreadSubscription(int $id, bool $subscribed = false, bool $ignored = false): array
155
    {
156
        return $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id/subscription?:args', $id,
157
            http_build_query(['subscribed' => $subscribed, 'ignored' => $ignored])), Request::METHOD_PUT);
158
    }
159
160
    /**
161
     * Delete a Thread Subscription
162
     *
163
     * @link https://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription
164
     *
165
     * @param int $id
166
     *
167
     * @return bool
168
     */
169 View Code Duplication
    public function deleteThreadSubscription(int $id): bool
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...
170
    {
171
        $this->getApi()->request($this->getApi()->sprintf('/notifications/threads/:id/subscription', (string)$id),
172
            Request::METHOD_DELETE);
173
174
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
175
            return true;
176
        }
177
178
        return false;
179
    }
180
}