Completed
Push — master ( bd02ec...4ff445 )
by Dmytro
04:14
created

Handler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 13.41 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 11
loc 82
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
B handle() 0 24 5
A createOptions() 0 21 3

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
declare(strict_types=1);
3
4
5
namespace Webhook\Domain\Infrastructure;
6
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Exception\TransferException;
9
use GuzzleHttp\RequestOptions;
10
use Psr\Http\Message\ResponseInterface;
11
use Webhook\Domain\Model\Webhook;
12
13
/**
14
 * Class Handler
15
 *
16
 * @package Webhook\Domain\Infrastructure
17
 */
18
final class Handler implements HandlerInterface
19
{
20
    /** @var  string */
21
    protected $defaultClient = 'Webhook-Client';
22
23
    /** @var Client|null */
24
    protected $client;
25
26
    /**
27
     * Handler constructor.
28
     *
29
     * @param null $client
30
     */
31 12 View Code Duplication
    public function __construct($client = null)
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...
32
    {
33 12
        if (null === $client) {
34 12
            $client = new Client([
35 12
                RequestOptions::TIMEOUT => 10,
36 12
                RequestOptions::ALLOW_REDIRECTS => false,
37
            ]);
38
        }
39
40 12
        $this->client = $client;
41 12
    }
42
43
    /**
44
     * @param Webhook $webhook
45
     *
46
     * @return RequestResult
47
     */
48 11
    public function handle(Webhook $webhook): RequestResult
49
    {
50 11
        $result = RequestResult::success();
51
52
        try {
53
            /** @var ResponseInterface $response */
54 11
            $response = $this->client->post($webhook->getUrl(), $this->createOptions($webhook));
55
56 9
            if ($response->getStatusCode() !== $webhook->getExpectedCode()) {
57 2
                return RequestResult::codeMissMatch($response->getStatusCode());
58
            }
59
60 7
            if ($webhook->getExpectedContent() !== null
61 7
                && strpos($response->getBody()->getContents(), $webhook->getExpectedContent()) === false
62
            ) {
63 7
                return RequestResult::contentMissMatch();
64
            }
65
66 2
        } catch (TransferException $e) {
67 2
            $result = RequestResult::transportError($e->getMessage());
68
        }
69
70 8
        return $result;
71
    }
72
73
    /**
74
     * @param Webhook $webhook
75
     *
76
     * @return array
77
     */
78 11
    private function createOptions(Webhook $webhook): array
79
    {
80 11
        $options = [];
81
82
        $headers = [
83 11
            'Next-Retry'  => $webhook->getNextAttempt()->format('U'),
84 11
            'Retry-Count' => $webhook->getAttempt(),
85 11
            'User-Agent'  => $webhook->getUserAgent() ?: $this->defaultClient
86
        ];
87
88 11
        if ($webhook->isRaw()) {
89 10
            $headers['Content-Type'] = 'application/json';
90 10
            $options[RequestOptions::BODY] = $webhook->getBody();
91
        } else {
92 1
            $options[RequestOptions::FORM_PARAMS] = json_decode($webhook->getBody(), true);
93
        }
94
95 11
        $options[RequestOptions::HEADERS] = $headers;
96
97 11
        return $options;
98
    }
99
}