Passed
Push — main ( 4c0521...6ec8da )
by Vasil
14:26
created

SpeedyAdapter::jsonDecode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Office\FindOfficeRequest;
16
use VasilDakov\Speedy\Service\Location\Office\FindOfficeResponse;
17
use VasilDakov\Speedy\Service\Location\Site\FindSiteRequest;
18
use VasilDakov\Speedy\Service\Location\Site\FindSiteResponse;
19
use VasilDakov\Speedy\Speedy;
20
use GuzzleHttp\Client;
21
use Laminas\Diactoros\RequestFactory;
22
use VasilDakov\Shipping\Response;
23
use VasilDakov\Shipping\Request;
24
25
/**
26
 * SpeedyAdapter
27
 *
28
 * @author    Vasil Dakov <[email protected]>
29
 * @copyright 2009-2024 Neutrino.bg
30
 * @version   1.0
31
 */
32
final class SpeedyAdapter implements AdapterInterface
33
{
34
    private const NAME = 'Speedy';
35
36
    private ?Speedy $client;
37
38 7
    public function __construct(?Speedy $client = null)
39
    {
40 7
        if (null === $client) {
41 7
            $client = new Speedy(
42 7
                new Configuration(
43 7
                    username: $_ENV['SPEEDY_USERNAME'],
44 7
                    password: $_ENV['SPEEDY_PASSWORD'],
45 7
                    language: $_ENV['SPEEDY_LANGUAGE']
46 7
                ),
47 7
                new Client(),
48 7
                new RequestFactory()
49 7
            );
50
        }
51 7
        $this->client = $client;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 1
    public function getName(): string
58
    {
59 1
        return self::NAME;
60
    }
61
62
    /**
63
     * @param GetCountriesRequest $request
64
     * @return Response\GetCountriesResponse
65
     * @throws ClientExceptionInterface
66
     */
67 1
    public function getCountries(Request\GetCountriesRequest $request): Response\GetCountriesResponse
68
    {
69 1
        $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

69
        /** @scrutinizer ignore-call */ 
70
        $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...
70 1
            new FindCountryRequest(name: 'Bulgaria')
71 1
        );
72 1
        $data = json_decode($json, true);
73
74 1
        $transformer = new ArrayTransformer();
75 1
        $transformer
76 1
            ->map('id', 'id')
77 1
            ->map('name', 'name')
78 1
            ->map('nameEn', 'nameEn')
79 1
            ->map('isoAlpha2', 'isoAlpha2')
80 1
            ->map('isoAlpha3', 'isoAlpha3')
81 1
        ;
82
83 1
        $result['countries'] = $transformer->toArray($data);
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...
84
85 1
        $strategy = new \Laminas\Hydrator\Strategy\CollectionStrategy(
86 1
            new \Laminas\Hydrator\ObjectPropertyHydrator(),
87 1
            Country::class
88 1
        );
89 1
        $array = $strategy->hydrate($result['countries']);
90
91 1
        return new Response\GetCountriesResponse($array);
92
    }
93
94
    /**
95
     * @param Request\GetCitiesRequest $request
96
     * @return Response\GetCitiesResponse
97
     * @throws ClientExceptionInterface
98
     */
99 1
    public function getCities(Request\GetCitiesRequest $request): Response\GetCitiesResponse
100
    {
101 1
        $object = new FindSiteRequest(
102 1
            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

102
            /** @scrutinizer ignore-type */ countryId: $request->countryId,
Loading history...
103 1
            name: $request->name
104 1
        );
105
106 1
        $json = $this->client->findSite($object);
107 1
        $data = $this->jsonDecode($json);
108
109 1
        $transformer = new ArrayTransformer();
110 1
        $transformer
111 1
            ->map('id', 'id')
112 1
            ->map('countryId', 'countryId')
113 1
            ->map('name', 'name')
114 1
            ->map('nameEn', 'nameEn')
115 1
            ->map('postCode', 'postCode')
116 1
        ;
117
118 1
        $result['cities'] = $transformer->toArrays($data['sites']);
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...
119
120 1
        return new Response\GetCitiesResponse($result);
121
    }
122
123 1
    public function getOffices(Request\GetOfficesRequest $request): Response\GetOfficesResponse
124
    {
125 1
        $object = new FindOfficeRequest(
126 1
            siteId: $request->cityId
0 ignored issues
show
Bug introduced by
It seems like $request->cityId can also be of type null; however, parameter $siteId 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

126
            /** @scrutinizer ignore-type */ siteId: $request->cityId
Loading history...
127 1
        );
128
129 1
        $json = $this->client->findOffice($object);
130 1
        $data = $this->jsonDecode($json);
131
132 1
        $transformer = new ArrayTransformer();
133 1
        $transformer
134 1
            ->map('id', 'id')
135 1
            ->map('name', 'name')
136 1
            ->map('nameEn', 'nameEn')
137 1
        ;
138
139 1
        $result['offices'] = $transformer->toArrays($data['offices']);
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...
140
141 1
        return new Response\GetOfficesResponse($result);
142
    }
143
144
    public function track(array $data)
145
    {
146
        // TODO: Implement track() method.
147
    }
148
149 2
    private function jsonDecode(string $json): array
150
    {
151 2
        return json_decode($json, true);
152
    }
153
}
154