Passed
Push — main ( f356f2...2c2ce3 )
by Vasil
02:30
created

EcontAdapter::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 23
ccs 18
cts 18
cp 1
crap 2
rs 9.8666
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 GuzzleHttp\Handler\CurlFactory;
9
use GuzzleHttp\Handler\CurlHandler;
10
use Laminas\Diactoros\RequestFactory;
11
use Psr\Http\Client\ClientExceptionInterface;
12
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...
13
use VasilDakov\Econt\Econt;
14
use VasilDakov\Econt\EcontInterface;
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
class EcontAdapter implements AdapterInterface
24
{
25
    private const NAME = 'Econt';
26
27
    private ?EcontInterface $client;
28
29 4
    public function __construct(?EcontInterface $client = null)
30
    {
31
32 4
        if (! $client) {
33 1
            $configuration = new Configuration(
34 1
                username: $_ENV['ECONT_USERNAME'],
35 1
                password: $_ENV['ECONT_PASSWORD'],
36 1
            );
37
38 1
            $client = new Econt(
39 1
                $configuration,
40 1
                new Client(
41 1
                    [
42 1
                        'connect_timeout' => 5,
43 1
                        'read_timeout' => 10,
44 1
                        'verify' => false,
45 1
                    ]
46 1
                ),
47 1
                new RequestFactory()
48 1
            );
49
        }
50
51 4
        $this->client = $client;
52
    }
53
54
    /**
55
     * @throws ClientExceptionInterface
56
     */
57
    public function getClientProfiles(): string
58
    {
59
        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

59
        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...
60
    }
61
62
63
    /**
64
     * @param Request\GetCountriesRequest $request
65
     * @return Response\GetCountriesResponse
66
     * @throws ClientExceptionInterface
67
     */
68 3
    public function getCountries(
69
        Request\GetCountriesRequest $request
70
    ): Response\GetCountriesResponse
71
    {
72 3
        $json = $this->client->getCountries();
73 3
        $data = json_decode($json, true);
74
75
        // transform properties to Shipping Model
76 3
        $transformer = new ArrayTransformer();
77 3
        $transformer
78 3
            ->map('id', 'id')
79 3
            ->map('name', 'name')
80 3
            ->map('nameEn', 'nameEn')
81 3
            ->map('isoAlpha2', 'code2')
82 3
            ->map('isoAlpha3', 'code3')
83 3
        ;
84 3
        $result = [];
85 3
        foreach ($data['countries'] as $country) {
86 3
            $result['countries'][] = $transformer->toArray($country);
87
        }
88
89
        // $hydrator = new \Laminas\Hydrator\ClassMethodsHydrator();
90
        // $hydrator = new \Laminas\Hydrator\ObjectPropertyHydrator();
91
        // $hydrator = new \Laminas\Hydrator\ReflectionHydrator();
92
93 3
        $strategy = new \Laminas\Hydrator\Strategy\CollectionStrategy(
94 3
            new \Laminas\Hydrator\ObjectPropertyHydrator(),
95 3
            Country::class
96 3
        );
97 3
        $array = $strategy->hydrate($result['countries']);
98
99
        // implementing missing econt non-strict country search
100 3
        if (null !== $request->name) {
101 1
            $array = array_filter($array, function (Country $country) use ($request) {
102 1
                return
103 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

103
                    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

103
                    str_starts_with(/** @scrutinizer ignore-type */ $country->name, $request->name) ||
Loading history...
104 1
                    str_starts_with($country->nameEn, $request->name)
105 1
                ;
106 1
            });
107
        }
108
109 3
        return new Response\GetCountriesResponse($array);
110
    }
111
112
113
    /**
114
     * @throws ClientExceptionInterface
115
     */
116
    public function getCities(array $data): string
117
    {
118
        return $this->client->getCities($data);
0 ignored issues
show
Bug introduced by
$data of type array is incompatible with the type VasilDakov\Econt\Request\GetCitiesRequest expected by parameter $object of VasilDakov\Econt\EcontInterface::getCities(). ( Ignorable by Annotation )

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

118
        return $this->client->getCities(/** @scrutinizer ignore-type */ $data);
Loading history...
119
    }
120
121
    /**
122
     */
123
    public function getOffices(array $data): string
124
    {
125
        return $this->client->getOffices($data);
0 ignored issues
show
Bug introduced by
$data of type array is incompatible with the type VasilDakov\Econt\Request\GetOfficesRequest expected by parameter $object of VasilDakov\Econt\EcontInterface::getOffices(). ( Ignorable by Annotation )

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

125
        return $this->client->getOffices(/** @scrutinizer ignore-type */ $data);
Loading history...
126
    }
127
128
    public function calculate()
129
    {
130
        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

130
        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...
131
    }
132
133
    public function track(array $data)
134
    {
135
        return $this->client->getShipmentStatuses($data);
136
    }
137
138
    public function getName(): string
139
    {
140
        return self::NAME;
141
    }
142
}
143