Passed
Push — main ( 3246f4...4c0521 )
by Vasil
02:31
created

SpeedyAdapter::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
ccs 12
cts 12
cp 1
crap 2
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VasilDakov\Shipping\Adapter;
6
7
use EventSauce\ObjectHydrator\ObjectMapperUsingReflection;
0 ignored issues
show
Bug introduced by
The type EventSauce\ObjectHydrato...ctMapperUsingReflection 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...
8
use Psr\Http\Client\ClientExceptionInterface;
9
use Selective\Transformer\ArrayTransformer;
10
use VasilDakov\Shipping\Model\City;
11
use VasilDakov\Shipping\Model\Country;
12
use VasilDakov\Shipping\Request\GetCountriesRequest;
13
use VasilDakov\Speedy\Configuration;
14
use VasilDakov\Speedy\Service\Location\Country\FindCountryRequest;
15
use VasilDakov\Speedy\Service\Location\Site\FindSiteRequest;
16
use VasilDakov\Speedy\Service\Location\Site\FindSiteResponse;
17
use VasilDakov\Speedy\Speedy;
18
use GuzzleHttp\Client;
19
use Laminas\Diactoros\RequestFactory;
20
use VasilDakov\Shipping\Response;
21
use VasilDakov\Shipping\Request;
22
23
/**
24
 * SpeedyAdapter
25
 *
26
 * @author    Vasil Dakov <[email protected]>
27
 * @copyright 2009-2024 Neutrino.bg
28
 * @version   1.0
29
 */
30
final class SpeedyAdapter implements AdapterInterface
31
{
32
    private const NAME = 'Speedy';
33
34
    private ?Speedy $client;
35
36 4
    public function __construct(?Speedy $client = null)
37
    {
38 4
        if (null === $client) {
39 4
            $client = new Speedy(
40 4
                new Configuration(
41 4
                    username: $_ENV['SPEEDY_USERNAME'],
42 4
                    password: $_ENV['SPEEDY_PASSWORD'],
43 4
                    language: $_ENV['SPEEDY_LANGUAGE']
44 4
                ),
45 4
                new Client(),
46 4
                new RequestFactory()
47 4
            );
48
        }
49 4
        $this->client = $client;
50
    }
51
52
    /**
53
     * @return string
54
     */
55 1
    public function getName(): string
56
    {
57 1
        return self::NAME;
58
    }
59
60
    /**
61
     * @param GetCountriesRequest $request
62
     * @return Response\GetCountriesResponse
63
     * @throws ClientExceptionInterface
64
     */
65
    public function getCountries(Request\GetCountriesRequest $request): Response\GetCountriesResponse
66
    {
67
        $json = $this->client->findCountry(
0 ignored issues
show
Bug introduced by
The method findCountry() 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

67
        /** @scrutinizer ignore-call */ 
68
        $json = $this->client->findCountry(

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...
68
            new FindCountryRequest(name: $request->name)
0 ignored issues
show
Bug introduced by
It seems like $request->name can also be of type null; however, parameter $name of VasilDakov\Speedy\Servic...yRequest::__construct() 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

68
            new FindCountryRequest(/** @scrutinizer ignore-type */ name: $request->name)
Loading history...
69
        );
70
        $data = json_decode($json, true);
0 ignored issues
show
Bug introduced by
$json of type VasilDakov\Speedy\Servic...try\FindCountryResponse is incompatible with the type string expected by parameter $json of json_decode(). ( Ignorable by Annotation )

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

70
        $data = json_decode(/** @scrutinizer ignore-type */ $json, true);
Loading history...
71
72
        $transformer = new ArrayTransformer();
73
        $transformer
74
            ->map('id', 'id')
75
            ->map('name', 'name')
76
            ->map('nameEn', 'nameEn')
77
            ->map('isoAlpha2', 'isoAlpha2')
78
            ->map('isoAlpha3', 'isoAlpha3')
79
        ;
80
81
        $result = [];
82
        foreach ($data['countries'] as $country) {
83
            $result['countries'][] = $transformer->toArray($country);
84
        }
85
86
        $strategy = new \Laminas\Hydrator\Strategy\CollectionStrategy(
87
            new \Laminas\Hydrator\ObjectPropertyHydrator(),
88
            Country::class
89
        );
90
        $array = $strategy->hydrate($result['countries']);
91
92
        return new Response\GetCountriesResponse($array);
93
    }
94
95
    /**
96
     * @param Request\GetCitiesRequest $request
97
     * @return Response\GetCitiesResponse
98
     * @throws ClientExceptionInterface
99
     */
100
    public function getCities(Request\GetCitiesRequest $request): Response\GetCitiesResponse
101
    {
102
        $object = new FindSiteRequest(
103
            countryId: $request->countryId,
0 ignored issues
show
Bug introduced by
It seems like $request->countryId can also be of type null and string; however, parameter $countryId of VasilDakov\Speedy\Servic...eRequest::__construct() does only seem to accept integer, 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
            /** @scrutinizer ignore-type */ countryId: $request->countryId,
Loading history...
104
            name: $request->name
105
        );
106
107
        $result = $this->client->findSite($object);
108
109
        $transformer = new ArrayTransformer();
110
        $transformer
111
            ->map('id', 'id')
112
            ->map('countryId', 'countryId')
113
            ->map('name', 'name')
114
            ->map('nameEn', 'nameEn')
115
            ->map('postCode', 'postCode')
116
        ;
117
118
        $records = [];
119
        foreach ($result->getSites() as $site) {
120
            $records['cities'][] = $transformer->toArray($site->toArray());
121
        }
122
123
        return new Response\GetCitiesResponse($records);
124
    }
125
126
    public function getOffices(array $data)
127
    {
128
        // TODO: Implement getOffices() method.
129
    }
130
131
    public function track(array $data)
132
    {
133
        // TODO: Implement track() method.
134
    }
135
}
136