Completed
Push — master ( 453b10...bec1da )
by Tobias
04:06
created

Webhook   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 136
Duplicated Lines 21.32 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 97.62%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 2
dl 29
loc 136
ccs 41
cts 42
cp 0.9762
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A verifyWebhookSignature() 0 15 5
A index() 0 7 1
A show() 0 8 1
A create() 15 15 1
A update() 14 14 1
A delete() 0 9 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
/*
4
 * Copyright (C) 2013 Mailgun
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Mailgun\Api;
11
12
use Http\Client\HttpClient;
13
use Mailgun\Assert;
14
use Mailgun\Hydrator\Hydrator;
15
use Mailgun\Model\Webhook\CreateResponse;
16
use Mailgun\Model\Webhook\DeleteResponse;
17
use Mailgun\Model\Webhook\IndexResponse;
18
use Mailgun\Model\Webhook\ShowResponse;
19
use Mailgun\Model\Webhook\UpdateResponse;
20
use Mailgun\RequestBuilder;
21
22
/**
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
class Webhook extends HttpApi
26
{
27
    /**
28
     * @var string
29
     */
30
    private $apiKey;
31
32
    /**
33
     * @param HttpClient     $httpClient
34
     * @param RequestBuilder $requestBuilder
35
     * @param Hydrator       $hydrator
36
     * @param string         $apiKey
37
     */
38 8
    public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator, $apiKey)
39
    {
40 8
        parent::__construct($httpClient, $requestBuilder, $hydrator);
41 8
        $this->apiKey = $apiKey;
42 8
    }
43
44
    /**
45
     * This function verifies the webhook signature with your API key to to see if it is authentic.
46
     *
47
     * If this function returns FALSE, you must not process the request.
48
     * You should reject the request with status code 403 Forbidden.
49
     *
50
     * @param int    $timestamp
51
     * @param string $token
52
     * @param string $signature
53
     *
54
     * @return bool
55
     */
56 3
    public function verifyWebhookSignature($timestamp, $token, $signature)
57
    {
58 3
        if (empty($timestamp) || empty($token) || empty($signature)) {
59 1
            return false;
60
        }
61
62 2
        $hmac = hash_hmac('sha256', $timestamp.$token, $this->apiKey);
63
64 2
        if (function_exists('hash_equals')) {
65
            // hash_equals is constant time, but will not be introduced until PHP 5.6
66
            return hash_equals($hmac, $signature);
67
        } else {
68 2
            return $hmac === $signature;
69
        }
70
    }
71
72
    /**
73
     * @param string $domain
74
     *
75
     * @return IndexResponse
76
     */
77 1
    public function index($domain)
78
    {
79 1
        Assert::notEmpty($domain);
80 1
        $response = $this->httpGet(sprintf('/v3/domains/%s/webhooks', $domain));
81
82 1
        return $this->hydrateResponse($response, IndexResponse::class);
83
    }
84
85
    /**
86
     * @param string $domain
87
     * @param string $webhook
88
     *
89
     * @return ShowResponse
90
     */
91 1
    public function show($domain, $webhook)
92
    {
93 1
        Assert::notEmpty($domain);
94 1
        Assert::notEmpty($webhook);
95 1
        $response = $this->httpGet(sprintf('/v3/domains/%s/webhooks/%s', $domain, $webhook));
96
97 1
        return $this->hydrateResponse($response, ShowResponse::class);
98
    }
99
100
    /**
101
     * @param string $domain
102
     * @param string $id
103
     * @param string $url
104
     *
105
     * @return CreateResponse
106
     */
107 1 View Code Duplication
    public function create($domain, $id, $url)
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 1
        Assert::notEmpty($domain);
110 1
        Assert::notEmpty($id);
111 1
        Assert::notEmpty($url);
112
113
        $params = [
114 1
            'id' => $id,
115 1
            'url' => $url,
116 1
        ];
117
118 1
        $response = $this->httpPost(sprintf('/v3/domains/%s/webhooks', $domain), $params);
119
120 1
        return $this->hydrateResponse($response, CreateResponse::class);
121
    }
122
123
    /**
124
     * @param string $domain
125
     * @param string $id
126
     * @param string $url
127
     *
128
     * @return UpdateResponse
129
     */
130 1 View Code Duplication
    public function update($domain, $id, $url)
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 1
        Assert::notEmpty($domain);
133 1
        Assert::notEmpty($id);
134 1
        Assert::notEmpty($url);
135
136
        $params = [
137 1
            'url' => $url,
138 1
        ];
139
140 1
        $response = $this->httpPut(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id), $params);
141
142 1
        return $this->hydrateResponse($response, UpdateResponse::class);
143
    }
144
145
    /**
146
     * @param string $domain
147
     * @param string $id
148
     *
149
     * @return DeleteResponse
150
     */
151 1
    public function delete($domain, $id)
152
    {
153 1
        Assert::notEmpty($domain);
154 1
        Assert::notEmpty($id);
155
156 1
        $response = $this->httpDelete(sprintf('/v3/domains/%s/webhooks/%s', $domain, $id));
157
158 1
        return $this->hydrateResponse($response, DeleteResponse::class);
159
    }
160
}
161