ApiRequest::setBaseUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Parkwayprojects\PayWithBank3D\Controllers;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Facades\Config;
7
use Parkwayprojects\PayWithBank3D\Exceptions\CouldNotProcess;
8
9
abstract class ApiRequest
10
{
11
    protected $baseUrl;
12
    /**
13
     * @var mixed
14
     */
15
    private $secretKey;
16
17
    private $publicKey;
18
19
    protected $client;
20
    private $response;
21
22
    public function __construct()
23
    {
24
        $this->setBaseUrl();
25
        $this->setConstant();
26
        $this->checkConstant();
27
        $this->prepareRequest();
28
    }
29
30
    public function setBaseUrl()
31
    {
32
        $this->baseUrl = Config::get('paywithbank3d.mode') === 'test' ? Config::get('paywithbank3d.testUrl') : Config::get('paywithbank3d.liveURL');
33
    }
34
35
    public function setConstant()
36
    {
37
        $this->secretKey = Config::get('paywithbank3d.secretKey');
38
        $this->publicKey = Config::get('paywithbank3d.publicKey');
39
    }
40
41
    protected function checkConstant()
42
    {
43
        if (! $this->secretKey) {
44
            throw CouldNotProcess::notSetSecretKey();
45
        }
46
47
        if (! $this->publicKey) {
48
            throw CouldNotProcess::notSetPublicKey();
49
        }
50
51
        if (! $this->baseUrl) {
52
            throw CouldNotProcess::notSetUrl();
53
        }
54
    }
55
56
    protected function prepareRequest()
57
    {
58
        $this->client = new Client([
59
            'base_uri' => $this->baseUrl,
60
            'auth' => [$this->publicKey, $this->secretKey],
61
            'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
62
        ]);
63
    }
64
65
    public function getResponse()
66
    {
67
        $response = new Response($this->response);
0 ignored issues
show
Compatibility introduced by
$this->response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
68
        $json = $response->toJSON();
69
70
        return json_decode($json, true);
71
    }
72
73
    protected function performGetRequest($relativeUrl)
74
    {
75
        $this->response = $this->client->request('GET', $relativeUrl);
76
77
        return $this->getResponse();
78
    }
79
80
    protected function performPostRequest($relativeUrl, $data)
81
    {
82
        $this->response = $this->client->request('POST', $relativeUrl, ['json'=> $data]);
83
84
        return $this->getResponse();
85
    }
86
}
87