Completed
Push — master ( d388c3...ceb9a8 )
by ARCANEDEV
05:06
created

AbstractService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 125
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setHttpClient() 0 6 1
A setKey() 0 6 1
A get() 0 6 1
A prepareQuery() 0 8 2
getDefaultQueryParams() 0 1 ?
prepareResponse() 0 1 ?
A parsePosition() 0 4 1
1
<?php namespace Arcanedev\GeoLocation\Google;
2
3
use Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position;
4
use GuzzleHttp\ClientInterface;
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Class     AbstractService
9
 *
10
 * @package  Arcanedev\GeoLocation\Google
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
abstract class AbstractService
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 45
    public function __construct(ClientInterface $client)
37
    {
38 45
        $this->setHttpClient($client);
39 45
    }
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 45
    public function setHttpClient(ClientInterface $client)
54
    {
55 45
        $this->client = $client;
56
57 45
        return $this;
58
    }
59
60
    /**
61
     * Set the API Key.
62
     *
63
     * @param  string  $key
64
     *
65
     * @return self
66
     */
67 45
    public function setKey($key)
68
    {
69 45
        $this->key = $key;
70
71 45
        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 30
    protected function get($url, array $options)
88
    {
89 30
        $response = $this->client->request('GET', $url, $options);
90
91 30
        return $this->prepareResponse($response);
92
    }
93
94
    /**
95
     * Prepare the URL query.
96
     *
97
     * @param  array  $params
98
     *
99
     * @return string
100
     */
101 30
    protected function prepareQuery(array $params)
102
    {
103 30
        $query = http_build_query(array_filter(
104 30
            array_merge($params, $this->getDefaultQueryParams())
105 10
        ));
106
107 30
        return empty($query) ? '' : '?'.$query;
108
    }
109
110
    /**
111
     * Get the default query params.
112
     *
113
     * @return array
114
     */
115
    abstract protected function getDefaultQueryParams();
116
117
    /**
118
     * Prepare the response.
119
     *
120
     * @param  \Psr\Http\Message\ResponseInterface  $response
121
     *
122
     * @return \Arcanedev\GeoLocation\Google\AbstractResponse
123
     */
124
    abstract protected function prepareResponse(ResponseInterface $response);
125
126
    /**
127
     * Parse the position object for the query.
128
     *
129
     * @param  \Arcanedev\GeoLocation\Contracts\Entities\Coordinates\Position  $position
130
     *
131
     * @return string
132
     */
133 15
    protected function parsePosition(Position $position)
134
    {
135 15
        return $position->lat()->value().','.$position->lng()->value();
136
    }
137
}
138