Helper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ParcelValue\Api;
6
7
use WebServCo\Api\JsonApi\Document;
8
use WebServCo\Framework\Http\CurlClient;
9
use WebServCo\Framework\Http\Method;
10
use WebServCo\Framework\Interfaces\LoggerInterface;
11
use WebServCo\Framework\Interfaces\ResponseInterface;
12
13
class Helper
14
{
15
    protected LoggerInterface $logger;
16
    protected CurlClient $curlClient;
17
18
    public function __construct(LoggerInterface $logger, string $jwt)
19
    {
20
        $this->logger = $logger;
21
        $this->curlClient = new CurlClient($this->logger);
22
        $this->curlClient->setRequestHeader('Accept', Document::CONTENT_TYPE);
23
        $this->curlClient->setRequestHeader('Authorization', \sprintf('Bearer %s', $jwt));
24
    }
25
26
    /**
27
    * @param array<string,mixed>|string $requestData
28
    */
29
    public function getResponse(string $url, string $method, $requestData): ResponseInterface
30
    {
31
        switch ($method) {
32
            case Method::POST:
33
                $this->curlClient->setRequestContentType(Document::CONTENT_TYPE);
34
                $this->curlClient->setRequestData($requestData);
35
                break;
36
            case Method::GET:
37
            case Method::HEAD:
38
                break;
39
            default:
40
                throw new \WebServCo\Framework\Exceptions\NotImplementedException('Functionality not implemented.');
41
        }
42
        $this->curlClient->setMethod($method);
43
        return $this->curlClient->retrieve($url);
44
    }
45
}
46