EcontAdapter::calculate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VasilDakov\Shipping\Adapter;
6
7
use GuzzleHttp\Client;
8
use Laminas\Diactoros\RequestFactory;
9
use Psr\Http\Client\ClientExceptionInterface;
10
use VasilDakov\Econt\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...
11
use VasilDakov\Econt\Econt;
12
use VasilDakov\Econt\EcontInterface;
13
use VasilDakov\Econt\Request\GetCitiesRequest;
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...
14
use VasilDakov\Shipping\Model\City;
15
use VasilDakov\Shipping\Model\Country;
16
use VasilDakov\Shipping\Request;
17
use VasilDakov\Shipping\Response;
18
use Selective\Transformer\ArrayTransformer;
19
20
use function array_filter;
21
use function str_starts_with;
22
23
final class EcontAdapter implements AdapterInterface
24
{
25
    private const NAME = 'Econt';
26
27
    private ?EcontInterface $client;
28
29 9
    public function __construct(?EcontInterface $client = null)
30
    {
31 9
        if (! $client) {
32 4
            $configuration = new Configuration(
33 4
                username: $_ENV['ECONT_USERNAME'],
34 4
                password: $_ENV['ECONT_PASSWORD'],
35 4
            );
36
37 4
            $client = new Econt(
38 4
                $configuration,
39 4
                new Client(
40 4
                    [
41 4
                        'connect_timeout' => 5,
42 4
                        'read_timeout' => 10,
43 4
                        'verify' => false,
44 4
                    ]
45 4
                ),
46 4
                new RequestFactory()
47 4
            );
48
        }
49
50 9
        $this->client = $client;
51
    }
52
53
54 5
    private function jsonDecode(string $json): array
55
    {
56 5
        return json_decode($json, true);
57
    }
58
59
    /**
60
     * @throws ClientExceptionInterface
61
     */
62
    public function getClientProfiles(): string
63
    {
64
        return $this->client->getClientProfiles();
0 ignored issues
show
Bug introduced by
The method getClientProfiles() does not exist on null. ( Ignorable by Annotation )

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

64
        return $this->client->/** @scrutinizer ignore-call */ getClientProfiles();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
    }
66
67
    /**
68
     * @param Request\GetCountriesRequest $request
69
     * @return Response\GetCountriesResponse
70
     * @throws ClientExceptionInterface
71
     */
72 3
    public function getCountries(Request\GetCountriesRequest $request): Response\GetCountriesResponse
73
    {
74 3
        $json = $this->client->getCountries();
75 3
        $data = $this->jsonDecode($json);
76
77
        // transform properties to Shipping Model
78 3
        $transformer = new ArrayTransformer();
79 3
        $transformer
80 3
            ->map('id', 'id')
81 3
            ->map('name', 'name')
82 3
            ->map('nameEn', 'nameEn')
83 3
            ->map('isoAlpha2', 'code2')
84 3
            ->map('isoAlpha3', 'code3')
85 3
        ;
86 3
        $result['countries'] = $transformer->toArrays($data['countries']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
87
88
        // $hydrator = new \Laminas\Hydrator\ClassMethodsHydrator();
89
        // $hydrator = new \Laminas\Hydrator\ObjectPropertyHydrator();
90
        // $hydrator = new \Laminas\Hydrator\ReflectionHydrator();
91
92 3
        $strategy = new \Laminas\Hydrator\Strategy\CollectionStrategy(
93 3
            new \Laminas\Hydrator\ObjectPropertyHydrator(),
94 3
            Country::class
95 3
        );
96 3
        $array = $strategy->hydrate($result['countries']);
97
98
        // implementing missing econt non-strict country search
99 3
        if (null !== $request->name) {
100 1
            $array = array_filter($array, function (Country $country) use ($request) {
101 1
                return
102 1
                    str_starts_with($country->name, $request->name) ||
0 ignored issues
show
Bug introduced by
It seems like $request->name can also be of type null; however, parameter $needle of str_starts_with() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

102
                    str_starts_with($country->name, /** @scrutinizer ignore-type */ $request->name) ||
Loading history...
Bug introduced by
It seems like $country->name can also be of type null; however, parameter $haystack of str_starts_with() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

102
                    str_starts_with(/** @scrutinizer ignore-type */ $country->name, $request->name) ||
Loading history...
103 1
                    str_starts_with($country->nameEn, $request->name)
104 1
                ;
105 1
            });
106
        }
107
108 3
        return new Response\GetCountriesResponse($array);
109
    }
110
111
112
    /**
113
     * @param Request\GetCitiesRequest $request
114
     * @return Response\GetCitiesResponse
115
     * @throws ClientExceptionInterface
116
     */
117 2
    public function getCities(Request\GetCitiesRequest $request): Response\GetCitiesResponse
118
    {
119 2
        $json = $this->client->getCities(new GetCitiesRequest(countryCode: $request->isoAlpha3));
120
121 2
        $data = $this->jsonDecode($json);
122
123
        // transform rules
124 2
        $transformer = new ArrayTransformer();
125 2
        $transformer
126 2
            ->map('id', 'id')
127 2
            ->map('country', 'country')
128 2
            ->map('postCode', 'postCode')
129 2
            ->map('name', 'name')
130 2
            ->map('nameEn', 'nameEn')
131 2
        ;
132
133
        // transform array to shipping model
134 2
        $result['cities'] = $transformer->toArrays($data['cities']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
135
136
        // hydrate the array
137 2
        $hydrator = new \Laminas\Hydrator\ObjectPropertyHydrator();
138 2
        $strategy = new \Laminas\Hydrator\Strategy\CollectionStrategy(
139 2
            $hydrator,
140 2
            City::class
141 2
        );
142 2
        $hydrator->addStrategy(
143 2
            'country',
144 2
            new \Laminas\Hydrator\Strategy\HydratorStrategy(
145 2
                new \Laminas\Hydrator\ReflectionHydrator(),
146 2
                Country::class
147 2
            )
148 2
        );
149
150 2
        $array = $strategy->hydrate($result['cities']);
151
152 2
        if (null !== $request->name) {
153 1
            $array = array_filter($array, function (City $city) use ($request) {
154 1
                return
155 1
                    str_starts_with($city->name, $request->name) ||
0 ignored issues
show
Bug introduced by
It seems like $request->name can also be of type null; however, parameter $needle of str_starts_with() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

155
                    str_starts_with($city->name, /** @scrutinizer ignore-type */ $request->name) ||
Loading history...
Bug introduced by
It seems like $city->name can also be of type null; however, parameter $haystack of str_starts_with() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

155
                    str_starts_with(/** @scrutinizer ignore-type */ $city->name, $request->name) ||
Loading history...
156 1
                    str_starts_with($city->nameEn, $request->name)
157 1
                    ;
158 1
            });
159
        }
160
161 2
        return new Response\GetCitiesResponse($array);
162
    }
163
164
    /**
165
     */
166
    public function getOffices(Request\GetOfficesRequest $request): Response\GetOfficesResponse
167
    {
168
        //return $this->client->getOffices($data);
169
        return new Response\GetOfficesResponse();
170
    }
171
172
    public function calculate()
173
    {
174
        return $this->client->calculate();
0 ignored issues
show
Bug introduced by
The method calculate() does not exist on VasilDakov\Econt\EcontInterface. ( Ignorable by Annotation )

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

174
        return $this->client->/** @scrutinizer ignore-call */ calculate();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
175
    }
176
177
    public function track(array $data)
178
    {
179
        return $this->client->getShipmentStatuses($data);
180
    }
181
182 1
    public function getName(): string
183
    {
184 1
        return self::NAME;
185
    }
186
}
187