AbstractConnector   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 22.73%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 20
c 1
b 0
f 0
dl 0
loc 85
ccs 5
cts 22
cp 0.2273
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A webHookSavingData() 0 2 1
A setConnectorId() 0 3 1
A __construct() 0 3 1
A getConnectorId() 0 3 1
A webHookContentType() 0 3 1
A webHookException() 0 15 2
A webHookResponse() 0 9 2
1
<?php
2
3
namespace Joesama\Webhook\Connectors;
4
5
use GuzzleHttp\Exception\BadResponseException;
6
use GuzzleHttp\Exception\TransferException;
7
use GuzzleHttp\Psr7\Response;
8
use GuzzleHttp\RequestOptions;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
abstract class AbstractConnector
13
{
14
    /**
15
     * Web Hook connector id.
16
     *
17
     * @var string
18
     */
19
    private string $webHookConnectorId;
20
21
    /**
22
     * Initiate connector constructor
23
     */
24
    public function __construct()
25 1
    {
26
        $this->setConnectorId(strtolower(class_basename($this)));
27 1
    }
28
29
    /**
30
     * Define request content type.
31
     */
32
    public function webHookContentType(): string
33
    {
34
        return RequestOptions::JSON;
35
    }
36
37
    /**
38
     * Define additional handling HTTP request response.
39
     *
40
     * @return mixed
41
     */
42
    public function webHookResponse(ResponseInterface $response, RequestInterface $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
    public function webHookResponse(ResponseInterface $response, /** @scrutinizer ignore-unused */ RequestInterface $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        $responseContent = $response->getBody()->getContents();
45
46
        if (($jsonResponse = json_decode($responseContent, true)) === null) {
47
            return $responseContent;
48
        }
49
50
        return $jsonResponse;
51
    }
52
53
    /**
54
     * Define additional handling for exceptions.
55
     *
56
     * @return mixed
57
     */
58
    public function webHookException(TransferException $exception, RequestInterface $request): mixed
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

58
    public function webHookException(TransferException $exception, /** @scrutinizer ignore-unused */ RequestInterface $request): mixed

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
        if ($exception instanceof BadResponseException) {
61
            $response = new Response(
62
                $exception->getResponse()->getStatusCode(),
63
                $exception->getResponse()->getHeaders(),
64
                $exception->getResponse()->getBody()->getContents(),
65
                $exception->getResponse()->getProtocolVersion(),
66
                $exception->getResponse()->getReasonPhrase()
67
            );
68
        } else {
69
            $response = new Response(500, [], $exception->getMessage());
70
        }
71
72
        return $response;
73
    }
74
75
    /**
76
     * Save data to data storage.
77
     */
78
    public function webHookSavingData(array $logData): void
0 ignored issues
show
Unused Code introduced by
The parameter $logData is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

78
    public function webHookSavingData(/** @scrutinizer ignore-unused */ array $logData): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
    {
80
        //@TODO: Implement saving logic here..
81
    }
82
83
    /**
84
     * Set connector id.
85
     */
86
    public function setConnectorId(string $connectorId): void
87
    {
88 1
        $this->webHookConnectorId = $connectorId;
89
    }
90 1
91 1
    /**
92
     * Set connector id.
93
     */
94
    public function getConnectorId(): ?string
95
    {
96
        return $this->webHookConnectorId;
97
    }
98
}
99