AbstractService::queryParams()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace kalanis\google_maps\Services;
4
5
6
use kalanis\google_maps\Remote\Headers\ApiAuth;
7
use kalanis\google_maps\Remote\Headers\Language;
8
use Psr\Http\Message\RequestInterface;
9
10
11
/**
12
 * Google Maps Abstract Service
13
 *
14
 * Each basic call returns params to GET part of request
15
 * To fill HTTP headers, you must fill method "getHeaders()"
16
 * To fill request body, you must set data in "$body" variable
17
 *
18
 * Pass ApiAuth class is necessary to set Google API keys
19
 */
20
abstract class AbstractService
21
{
22
    protected const API_HOST = 'https://maps.googleapis.com';
23
24
    /**
25
     * Constructor
26
     *
27
     * @param RequestInterface $request Request to fill
28
     * @param ApiAuth $auth Class with auth params
29
     * @param Language $lang Class to set the language
30
     */
31 52
    public function __construct(
32
        protected RequestInterface  $request,
33
        protected readonly ApiAuth  $auth,
34
        protected readonly Language $lang,
35
    )
36
    {
37 52
    }
38
39
    /**
40
     * If parse response body for 'results' node
41
     * Usually necessary to discard that behavior if you want to get 'status' node
42
     *
43
     * @return bool
44
     */
45 6
    public function wantInnerResult(): bool
46
    {
47 6
        return true;
48
    }
49
50
    /**
51
     * @param string $path
52
     * @param array<string, string|int|float> $params
53
     * @return RequestInterface
54
     */
55 38
    protected function getWithDefaults(string $path, array $params): RequestInterface
56
    {
57 38
        $current = parse_url($path);
58 38
        $url = $this->request->getUri()
59 38
            ->withScheme($current['scheme'] ?? '')
60 38
            ->withUserInfo('')
61 38
            ->withHost($current['host'] ?? '')
62 38
            ->withPort(null)
63 38
            ->withPath($current['path'] ?? '')
64 38
            ->withQuery('')
65 38
            ->withFragment('')
66 38
        ;
67 38
        if (!empty($params)) {
68 36
            $url = $url->withQuery(http_build_query($params, '', null, PHP_QUERY_RFC3986));
69
        }
70 38
        return $this->request
71 38
            ->withMethod('GET')
72 38
            ->withUri($url)
73 38
        ;
74
    }
75
76
    /**
77
     * @param array<string, string|int|float> $params
78
     * @return array<string, string|int|float>
79
     */
80 3
    protected function queryParams(array $params): array
81
    {
82 3
        return array_merge($this->auth->getAuthParams(), $params);
83
    }
84
85
    /**
86
     * @param array<string, string|int|float> $params
87
     * @return array<string, string|int|float>
88
     */
89 33
    protected function queryParamsLang(array $params): array
90
    {
91 33
        return array_merge($this->auth->getAuthParams(), $this->lang->getToQuery('GET'), $params);
92
    }
93
}
94