Hooks   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 130
Duplicated Lines 16.92 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 22
loc 130
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A listHooks() 0 4 1
A getSingleHook() 0 4 1
A pingHook() 11 11 2
A deleteHook() 11 11 2
A createHook() 0 10 1
A editHook() 0 10 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\Organizations;
3
4
use FlexyProject\GitHub\AbstractApi;
5
use Symfony\Component\HttpFoundation\Request;
6
7
/**
8
 * Hooks API class allow you to receive HTTP POST payloads whenever certain events happen within the organization.
9
 *
10
 * @package FlexyProject\GitHub\Receiver\Organizations
11
 */
12
class Hooks extends AbstractOrganizations
13
{
14
15
    /**
16
     * List hooks
17
     *
18
     * @link https://developer.github.com/v3/orgs/hooks/#list-hooks
19
     *
20
     * @param string $org
21
     *
22
     * @return array
23
     * @throws \Exception
24
     */
25
    public function listHooks(string $org): array
26
    {
27
        return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks', $org));
28
    }
29
30
    /**
31
     * Get single hook
32
     *
33
     * @link https://developer.github.com/v3/orgs/hooks/#get-single-hook
34
     *
35
     * @param string $org
36
     * @param int    $id
37
     *
38
     * @return array
39
     * @throws \Exception
40
     */
41
    public function getSingleHook(string $org, int $id): array
42
    {
43
        return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks/:id', $org, (string)$id));
44
    }
45
46
    /**
47
     * Create a hook
48
     *
49
     * @link https://developer.github.com/v3/orgs/hooks/#create-a-hook
50
     *
51
     * @param string       $org
52
     * @param string       $name
53
     * @param string|array $config
54
     * @param array        $events
55
     * @param bool         $active
56
     *
57
     * @return array
58
     * @throws \Exception
59
     */
60
    public function createHook(string $org, string $name, $config, array $events = [AbstractApi::EVENTS_PUSH],
61
                               bool $active = false): array
62
    {
63
        return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks', $org), Request::METHOD_POST, [
64
                'name'   => $name,
65
                'config' => $config,
66
                'events' => $events,
67
                'active' => $active
68
            ]);
69
    }
70
71
    /**
72
     * Edit a hook
73
     *
74
     * @link https://developer.github.com/v3/orgs/hooks/#edit-a-hook
75
     *
76
     * @param string       $org
77
     * @param int          $id
78
     * @param string|array $config
79
     * @param array        $events
80
     * @param bool         $active
81
     *
82
     * @return array
83
     * @throws \Exception
84
     */
85
    public function editHook(string $org, int $id, $config, array $events = [AbstractApi::EVENTS_PUSH],
86
                             bool $active = false): array
87
    {
88
        return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks/:id', $org, (string)$id),
89
            Request::METHOD_PATCH, [
90
                'config' => $config,
91
                'events' => $events,
92
                'active' => $active
93
            ]);
94
    }
95
96
    /**
97
     * Ping a hook
98
     *
99
     * @link https://developer.github.com/v3/orgs/hooks/#ping-a-hook
100
     *
101
     * @param string $org
102
     * @param int    $id
103
     *
104
     * @return bool
105
     * @throws \Exception
106
     */
107 View Code Duplication
    public function pingHook(string $org, 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...
108
    {
109
        $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks/:id/pings', $org, (string)$id),
110
            Request::METHOD_POST);
111
112
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
113
            return true;
114
        }
115
116
        return false;
117
    }
118
119
    /**
120
     * Delete a hook
121
     *
122
     * @link https://developer.github.com/v3/orgs/hooks/#delete-a-hook
123
     *
124
     * @param string $org
125
     * @param int    $id
126
     *
127
     * @return bool
128
     * @throws \Exception
129
     */
130 View Code Duplication
    public function deleteHook(string $org, 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...
131
    {
132
        $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks/:id', $org, (string)$id),
133
            Request::METHOD_DELETE);
134
135
        if ($this->getApi()->getHeaders()['Status'] == '204 No Content') {
136
            return true;
137
        }
138
139
        return false;
140
    }
141
}