Completed
Push — master ( 0f5433...3fbd33 )
by David
01:51
created

Webhook   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 26.85 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 97.5%

Importance

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