Watching   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A listWatchers() 0 5 1
A listSubscriptions() 0 11 2
A getRepositorySubscription() 0 5 1
A deleteRepositorySubscription() 0 12 2
A userSubscriptions() 0 12 2
A watchRepository() 0 12 2
A stopWatchingRepository() 0 12 2
A setRepositorySubscription() 0 8 1
1
<?php
2
namespace FlexyProject\GitHub\Receiver\Activity;
3
4
use Symfony\Component\HttpFoundation\Request;
5
6
/**
7
 * The Watching API class registers the user to receive notifications on new discussions, as well as events in the
8
 * user’s activity feed.
9
 *
10
 * @link    https://developer.github.com/v3/activity/watching/
11
 * @package GitHub\Receiver\Activity
12
 */
13
class Watching extends AbstractActivity
14
{
15
16
    /**
17
     * List watchers
18
     *
19
     * @link https://developer.github.com/v3/activity/watching/#list-watchers
20
     * @return array
21
     */
22
    public function listWatchers(): array
23
    {
24
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/subscribers',
25
            $this->getActivity()->getOwner(), $this->getActivity()->getRepo()));
26
    }
27
28
    /**
29
     * List repositories being watched
30
     *
31
     * @link https://developer.github.com/v3/activity/watching/#list-repositories-being-watched
32
     *
33
     * @param string $username
34
     *
35
     * @return array
36
     */
37
    public function listSubscriptions(string $username = null): array
38
    {
39
        if (null !== $username) {
40
            return $this->getApi()->request($this->getApi()->sprintf('/users/:username/subscriptions',
41
                $this->getActivity()->getOwner(), $this->getActivity()->getRepo(), $username));
42
        }
43
44
        return $this->getApi()->request($this->getApi()
45
                                             ->sprintf('/user/subscriptions', $this->getActivity()->getOwner(),
46
                                                 $this->getActivity()->getRepo()));
47
    }
48
49
    /**
50
     * Get a Repository Subscription
51
     *
52
     * @link https://developer.github.com/v3/activity/watching/#get-a-repository-subscription
53
     * @return array
54
     */
55
    public function getRepositorySubscription(): array
56
    {
57
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/subscription',
58
            $this->getActivity()->getOwner(), $this->getActivity()->getRepo()));
59
    }
60
61
    /**
62
     * Set a Repository Subscription
63
     *
64
     * @link https://developer.github.com/v3/activity/watching/#set-a-repository-subscription
65
     *
66
     * @param bool $subscribed
67
     * @param bool $ignored
68
     *
69
     * @return array
70
     */
71
    public function setRepositorySubscription(bool $subscribed = false, bool $ignored = false): array
72
    {
73
        return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/subscription?:args',
74
            $this->getActivity()->getOwner(), $this->getActivity()->getRepo(), http_build_query([
75
                'subscribed' => $subscribed,
76
                'ignored'    => $ignored
77
            ])), Request::METHOD_PUT);
78
    }
79
80
    /**
81
     * Delete a Repository Subscription
82
     *
83
     * @link https://developer.github.com/v3/activity/watching/#delete-a-repository-subscription
84
     * @return bool
85
     */
86
    public function deleteRepositorySubscription(): bool
87
    {
88
        $this->getApi()->request($this->getApi()
89
                                      ->sprintf('/repos/:owner/:repo/subscription', $this->getActivity()->getOwner(),
90
                                          $this->getActivity()->getRepo()), Request::METHOD_DELETE);
91
92
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
93
            return true;
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * Check if you are watching a repository (LEGACY)
101
     *
102
     * @link https://developer.github.com/v3/activity/watching/#check-if-you-are-watching-a-repository-legacy
103
     * @return bool
104
     */
105
    public function userSubscriptions(): bool
106
    {
107
        $this->getApi()->request($this->getApi()
108
                                      ->sprintf('/user/subscriptions/:owner/:repo', $this->getActivity()->getOwner(),
109
                                          $this->getActivity()->getRepo()));
110
111
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
112
            return true;
113
        }
114
115
        return false;
116
    }
117
118
    /**
119
     * Watch a repository (LEGACY)
120
     *
121
     * @link https://developer.github.com/v3/activity/watching/#watch-a-repository-legacy
122
     * @return bool
123
     */
124
    public function watchRepository(): bool
125
    {
126
        $this->getApi()->request($this->getApi()
127
                                      ->sprintf('/user/subscriptions/:owner/:repo', $this->getActivity()->getOwner(),
128
                                          $this->getActivity()->getRepo()), Request::METHOD_PUT);
129
130
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
131
            return true;
132
        }
133
134
        return false;
135
    }
136
137
    /**
138
     * Stop watching a repository (LEGACY)
139
     *
140
     * @link https://developer.github.com/v3/activity/watching/#stop-watching-a-repository-legacy
141
     * @return bool
142
     */
143
    public function stopWatchingRepository(): bool
144
    {
145
        $this->getApi()->request($this->getApi()
146
                                      ->sprintf('/user/subscriptions/:owner/:repo', $this->getActivity()->getOwner(),
147
                                          $this->getActivity()->getRepo()), Request::METHOD_DELETE);
148
149
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
150
            return true;
151
        }
152
153
        return false;
154
    }
155
}