Issues (9)

src/Services/Routes.php (1 issue)

1
<?php
2
3
namespace kalanis\google_maps\Services;
4
5
6
use kalanis\google_maps\Remote\Body;
7
use kalanis\google_maps\ServiceException;
8
use Psr\Http\Message\RequestInterface;
9
10
11
/**
12
 * Routes service
13
 *
14
 * @see https://developers.google.com/maps/documentation/routes/
15
 * @see https://developers.google.com/maps/documentation/routes/compute_route_directions
16
 */
17
class Routes extends AbstractService
18
{
19
    /**
20
     * Route lookup
21
     *
22
     * @param array<mixed>|null $origin
23
     * @param array<mixed>|null $destination
24
     * @param array<mixed> $body Full body
25
     * @throws ServiceException
26
     * @return RequestInterface
27
     */
28 2
    public function computeRoutes(array|null $origin, array|null $destination, array $body = []): RequestInterface
29
    {
30 2
        $requestBody = $body;
31 2
        $requestBody['origin'] = $origin ?? $requestBody['origin'] ?? [];
32 2
        $requestBody['destination'] = $destination ?? $requestBody['destination'] ?? [];
33
34
        // Language Code
35 2
        if (!empty($this->lang->getLanguage())) {
36 1
            $requestBody['languageCode'] = $this->lang->getLanguage();
37
        }
38
39
        // Google API request body format
40 2
        $encoded = @json_encode($requestBody);
41 2
        if (false === $encoded) {
42
            // @codeCoverageIgnoreStart
43
            // to get this error you must have something really fishy in $bodyParams
44
            throw new ServiceException(json_last_error_msg());
45
        }
46
        // @codeCoverageIgnoreEnd
47
48 2
        return $this->getWithDefaults('https://routes.googleapis.com/directions/v2:computeRoutes', [])
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getWithDef...\Remote\Body($encoded)) returns the type Psr\Http\Message\MessageInterface which includes types incompatible with the type-hinted return Psr\Http\Message\RequestInterface.
Loading history...
49 2
            ->withMethod('POST')
50 2
            ->withHeader('X-Goog-FieldMask', 'routes.duration,routes.distanceMeters,routes.legs,geocodingResults')
51 2
            ->withHeader('X-Goog-Api-Key', $this->auth->getKey())
52 2
            ->withBody(new Body($encoded));
53
    }
54
}
55