Completed
Push — master ( d972fa...aafada )
by Dmytro
13:29
created

Handler::createOptions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 1
crap 3
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
}