Completed
Push — master ( 4a903b...b725ab )
by Tobias
03:23 queued 53s
created

Webhook::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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