Completed
Pull Request — master (#3)
by ARCANEDEV
05:27
created

AbstractWebService::prepareQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php namespace Arcanedev\GeoLocation\Google;
2
3
use Arcanedev\GeoLocation\Contracts\Entities\Position;
4
use GuzzleHttp\ClientInterface;
5
6
/**
7
 * Class     AbstractWebService
8
 *
9
 * @package  Arcanedev\GeoLocation\Google
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
abstract class AbstractWebService
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /** @var  \GuzzleHttp\ClientInterface */
20
    protected $client;
21
22
    /** @var string|null */
23
    protected $key = null;
24
25
    /* -----------------------------------------------------------------
26
     |  Constructor
27
     | -----------------------------------------------------------------
28
     */
29
30
    /**
31
     * GoogleDistanceMatrix constructor.
32
     *
33
     * @param  \GuzzleHttp\ClientInterface  $client
34
     */
35 30
    public function __construct(ClientInterface $client)
36
    {
37 30
        $this->setHttpClient($client);
38 30
    }
39
40
    /* -----------------------------------------------------------------
41
     |  Getters & Setters
42
     | -----------------------------------------------------------------
43
     */
44
45
    /**
46
     * Set the HTTP Client.
47
     *
48
     * @param  \GuzzleHttp\ClientInterface  $client
49
     *
50
     * @return self
51
     */
52 30
    public function setHttpClient(ClientInterface $client)
53
    {
54 30
        $this->client = $client;
55
56 30
        return $this;
57
    }
58
59
    /**
60
     * Set the API Key.
61
     *
62
     * @param  string  $key
63
     *
64
     * @return self
65
     */
66 30
    public function setKey($key)
67
    {
68 30
        $this->key = $key;
69
70 30
        return $this;
71
    }
72
73
    /* -----------------------------------------------------------------
74
     |  Other Methods
75
     | -----------------------------------------------------------------
76
     */
77
78
    /**
79
     * Prepare the URL query.
80
     *
81
     * @param  array  $params
82
     *
83
     * @return string
84
     */
85 18
    protected function prepareQuery(array $params)
86
    {
87 18
        $params = array_merge($params, $this->getDefaultQueryParams());
88
89 18
        return http_build_query(array_filter($params));
90
    }
91
92
    /**
93
     * Get the default query params.
94
     *
95
     * @return array
96
     */
97
    abstract protected function getDefaultQueryParams();
98
99
    /**
100
     * Parse the position object for the query.
101
     *
102
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Position  $position
103
     *
104
     * @return string
105
     */
106 15
    protected function parsePosition(Position $position)
107
    {
108 15
        return $position->lat()->value().','.$position->long()->value();
109
    }
110
}
111