Hooks   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 125
Duplicated Lines 8 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

7 Methods

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