Services::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace kalanis\google_maps;
4
5
6
use kalanis\google_maps\Remote\Response;
7
use kalanis\google_maps\Services\ServiceFactory;
8
use Psr\Http\Client\ClientExceptionInterface;
9
use Psr\Http\Client\ClientInterface;
10
use Psr\Http\Message\RequestInterface;
11
use ReflectionException;
12
13
14
/**
15
 * Main service class
16
 * @method array|string directions(string $origin, string $destination, array $params = [])
17
 * @method array|string distanceMatrix(string $origin, string $destination, array $params = [])
18
 * @method array|string elevation(string $locations, array $params = [])
19
 * @method array|string geocode(string $address, array $params = [])
20
 * @method array|string reverseGeocode(string $lat, string $lng, array $params = [])
21
 * @method array|string computeRoutes(array $origin, array $destination, array $body = [], array $headers = [], array $params = [])
22
 * @method array|string geolocate(array $bodyParams = [])
23
 * @method array|string timezone(string $location, string|null $timestamp = null, array $params = [])
24
 * @method array|string nearby(string $keyword, float[] $latlng, float|null $radius = null, string|null $type = null, array $params = [])
25
 * @method array|string findPlace(string $input, string $inputType, string[] $fields = [], float[]|null $bias = null, array $params = [])
26
 * @method array|string findText(string $query, float $radius, float[] $location = [], int|null $maxPrice = null, int|null $minPrice = null, bool $openNow = false, string|null $region = null, string|null $type = null, array $params = [])
27
 * @method array|string placeDetails(string $placeId, string[] $fields = [], string $region = null, bool $translateReviews = true, string $sortReviews = null, array $params = [])
28
 * @method array|string snapToRoads(array|string|null $path, array $params = [])
29
 */
30
class Services
31
{
32 7
    public function __construct(
33
        protected readonly ServiceFactory  $factory,
34
        protected readonly ClientInterface $client,
35
        protected readonly Response        $response,
36
    )
37
    {
38 7
    }
39
40
    /**
41
     * Client methods refer to each service
42
     *
43
     * All service methods from Client calling would leave out the first argument (Client itself).
44
     *
45
     * @param string $method Client's method name
46
     * @param array<int, string|int|float> $arguments Method arguments
47
     * @throws ServiceException
48
     * @throws ReflectionException
49
     * @throws ClientExceptionInterface
50
     * @return mixed Processed service method return
51
     */
52 7
    public function __call(string $method, array $arguments)
53
    {
54
        // walkthrough:
55
        // 1 - get service
56
        // 2 - call method to create Request object; pass params there
57
        // 3 - asks client for http data with things from correct service
58
        // 4 - client response with something
59
        // 5 - parse response and returns it to the user
60
61
        // Get the service from Factory
62 7
        $service = $this->factory->getService($method);
63 7
        $request = call_user_func_array([$service, $method], $arguments);
64 7
        if (!$request instanceof RequestInterface) {
65 1
            throw new ServiceException(sprintf('Call *%s::%s* cannot be used - returns *%s*!', get_class($service), $method, gettype($request)));
66
        }
67 6
        return $this->response->process(
68 6
            $this->client->sendRequest(
69 6
                $request
70 6
            ),
71 6
            $service->wantInnerResult()
72 6
        );
73
    }
74
}
75