Completed
Pull Request — master (#3)
by ARCANEDEV
04:55
created

AbstractWebService::parsePosition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Class     AbstractWebService
9
 *
10
 * @package  Arcanedev\GeoLocation\Google
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
abstract class AbstractWebService
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /** @var  \GuzzleHttp\ClientInterface */
21
    protected $client;
22
23
    /** @var string|null */
24
    protected $key = null;
25
26
    /* -----------------------------------------------------------------
27
     |  Constructor
28
     | -----------------------------------------------------------------
29
     */
30
31
    /**
32
     * GoogleDistanceMatrix constructor.
33
     *
34
     * @param  \GuzzleHttp\ClientInterface  $client
35
     */
36 30
    public function __construct(ClientInterface $client)
37
    {
38 30
        $this->setHttpClient($client);
39 30
    }
40
41
    /* -----------------------------------------------------------------
42
     |  Getters & Setters
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Set the HTTP Client.
48
     *
49
     * @param  \GuzzleHttp\ClientInterface  $client
50
     *
51
     * @return self
52
     */
53 30
    public function setHttpClient(ClientInterface $client)
54
    {
55 30
        $this->client = $client;
56
57 30
        return $this;
58
    }
59
60
    /**
61
     * Set the API Key.
62
     *
63
     * @param  string  $key
64
     *
65
     * @return self
66
     */
67 30
    public function setKey($key)
68
    {
69 30
        $this->key = $key;
70
71 30
        return $this;
72
    }
73
74
    /* -----------------------------------------------------------------
75
     |  Common Methods
76
     | -----------------------------------------------------------------
77
     */
78
79
    /**
80
     * Make a GET request.
81
     *
82
     * @param  string  $url
83
     * @param  array   $options
84
     *
85
     * @return mixed
86
     */
87 18
    protected function get($url, array $options)
88
    {
89 18
        $response = $this->client->request('GET', $url, $options);
90
91 18
        return $this->prepareResponse($response);
92
    }
93
94
    /**
95
     * Prepare the URL query.
96
     *
97
     * @param  array  $params
98
     *
99
     * @return string
100
     */
101 18
    protected function prepareQuery(array $params)
102
    {
103 18
        $params = array_merge($params, $this->getDefaultQueryParams());
104
105 18
        return http_build_query(array_filter($params));
106
    }
107
108
    /**
109
     * Get the default query params.
110
     *
111
     * @return array
112
     */
113
    abstract protected function getDefaultQueryParams();
114
115
    /**
116
     * Prepare the response.
117
     *
118
     * @param  \Psr\Http\Message\ResponseInterface  $response
119
     *
120
     * @return mixed
121
     */
122
    abstract protected function prepareResponse(ResponseInterface $response);
123
124
    /**
125
     * Parse the position object for the query.
126
     *
127
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Position  $position
128
     *
129
     * @return string
130
     */
131 15
    protected function parsePosition(Position $position)
132
    {
133 15
        return $position->lat()->value().','.$position->long()->value();
134
    }
135
}
136