Passed
Push — main ( 3b4045...a8b0f9 )
by Vasil
03:08
created

Econt::createRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 3
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VasilDakov\Econt;
6
7
use Fig\Http\Message\RequestMethodInterface;
8
use Psr\Http\Client\ClientExceptionInterface;
9
use Psr\Http\Client\ClientInterface;
10
use Psr\Http\Message\RequestFactoryInterface;
11
use Psr\Http\Message\RequestInterface;
12
use VasilDakov\Econt\Request;
13
14
/**
15
 * Class Econt
16
 *
17
 * @author Vasil Dakov <[email protected]>
18
 */
19
final class Econt implements EcontInterface
20
{
21
    public const API_URL = 'https://demo.econt.com';
22
23 10
    public function __construct(
24
        private readonly Configuration $configuration,
0 ignored issues
show
Bug introduced by
The type VasilDakov\Econt\Configuration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
        private readonly ClientInterface $client,
26
        private readonly RequestFactoryInterface $factory
27
    ) {
28 10
    }
29
30
    /**
31
     * @throws ClientExceptionInterface
32
     */
33 2
    public function getClientProfiles(): string
34
    {
35 2
        $request = $this->createRequest(
36 2
            RequestMethodInterface::METHOD_POST,
37 2
            self::API_URL . '/ee/services/Profile/ProfileService.getClientProfiles.json',
38 2
            []
39 2
        );
40
41 2
        $response = $this->client->sendRequest($request);
42
43 2
        return $response->getBody()->getContents();
44
    }
45
46
    /**
47
     * @throws ClientExceptionInterface
48
     */
49 4
    public function getCountries(): string
50
    {
51 4
        $request = $this->createRequest(
52 4
            RequestMethodInterface::METHOD_POST,
53 4
            self::API_URL . '/ee/services/Nomenclatures/NomenclaturesService.getCountries.json',
54 4
            []
55 4
        );
56
57 4
        $response = $this->client->sendRequest($request);
58
59 4
        return $response->getBody()->getContents();
60
    }
61
62
    /**
63
     * @throws ClientExceptionInterface
64
     */
65 2
    public function getCities(Request\GetCitiesRequest $object): string
0 ignored issues
show
Bug introduced by
The type VasilDakov\Econt\Request\GetCitiesRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
    {
67 2
        $request = $this->createRequest(
68 2
            RequestMethodInterface::METHOD_POST,
69 2
            self::API_URL . '/ee/services/Nomenclatures/NomenclaturesService.getCities.json',
70 2
            $object->toArray()
71 2
        );
72
73 2
        $response = $this->client->sendRequest($request);
74
75 2
        return $response->getBody()->getContents();
76
    }
77
78
    public function getOffices(array $data): string
79
    {
80
        $request = $this->createRequest(
81
            RequestMethodInterface::METHOD_POST,
82
            self::API_URL . '/ee/services/Nomenclatures/NomenclaturesService.getOffices.json',
83
            [
84
                Constants::COUNTRY_CODE => $data['countryCode'],
85
                Constants::CITY_ID => $data['cityId'],
86
            ]
87
        );
88
89
        $response = $this->client->sendRequest($request);
90
91
        return $response->getBody()->getContents();
92
    }
93
94
95
    public function getShipmentStatuses(array $data): string
96
    {
97
        $request = $this->createRequest(
98
            RequestMethodInterface::METHOD_POST,
99
            self::API_URL . '/ee/services/Shipments/ShipmentService.getShipmentStatuses.json',
100
            $data
101
        );
102
103
        $response = $this->client->sendRequest($request);
104
105
        return $response->getBody()->getContents();
106
    }
107
108
109
    /**
110
     * @param string $method
111
     * @param string $uri
112
     * @param array $data
113
     * @return RequestInterface
114
     */
115 8
    private function createRequest(string $method, string $uri, array $data): RequestInterface
116
    {
117 8
        $request = $this->factory->createRequest($method, $uri);
118
119 8
        $request = $request->withAddedHeader('Content-Type', 'application/json');
120 8
        $request = $request->withAddedHeader(
121 8
            'Authorization',
122 8
            'Basic ' . base64_encode(
123 8
                $this->configuration->username . ":" . $this->configuration->password
124 8
            )
125 8
        );
126
127 8
        $request->getBody()->write(json_encode($data));
128
129 8
        return $request;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $request returns the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\RequestInterface.
Loading history...
130
    }
131
132
    public function getStreets(array $data): string
133
    {
134
        return '';
135
    }
136
137
    public function getQuarters(array $data): string
138
    {
139
        return '';
140
    }
141
142
    public function createLabel(array $data): string
143
    {
144
        return '';
145
    }
146
147
    public function createLabels(array $data): string
148
    {
149
        return '';
150
    }
151
152
    public function deleteLabels(array $data): string
153
    {
154
        return '';
155
    }
156
}
157