Completed
Push — master ( e63d71...bc30c3 )
by Stephen
22s queued 12s
created

PostcodeService::getTerminatedPostcode()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JustSteveKing\LaravelPostcodes\Service;
6
7
use GuzzleHttp\Client;
8
9
class PostcodeService
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $url;
15
16
    /**
17
     * @var Client
18
     */
19
    protected $http;
20
21
    /**
22
     * Postcode Service constructor.
23
     *
24
     * @param Client $client
25
     *
26
     * @return void
27
     */
28 36
    public function __construct(Client $client)
29
    {
30 36
        $this->url = config('services.postcodes.url');
31
32 36
        $this->http = $client;
33 36
    }
34
35
    /**
36
     * Validate a postcode against the API
37
     *
38
     * @param string $postcode
39
     *
40
     * @return bool
41
     */
42 9
    public function validate(string $postcode): bool
43
    {
44 9
        return $this->getResponse("postcodes/$postcode/validate");
45
    }
46
47
    /**
48
     * Get the address details from a postcode
49
     *
50
     * @param string $postcode
51
     *
52
     * @return object
53
     */
54 3
    public function getPostcode(string $postcode): object
55
    {
56 3
        return $this->getResponse("postcodes/$postcode");
57
    }
58
59
    /**
60
     * Get information based on outward code including geo data
61
     *
62
     * @param string $outwardcode
63
     *
64 3
     * @return object
65
     */
66 3
    public function getOutwardCode(string $outwardcode): object
67
    {
68
        return $this->getResponse("outcodes/$outwardcode");
69
    }
70
71
    /**
72
     * Get the address details from a random postcode
73
     *
74
     * @return object
75
     */
76 3
    public function getRandomPostcode()
77
    {
78 3
        return $this->getResponse("random/postcodes");
79
    }
80 3
81
    /**
82
     * Query the API for a given string
83
     *
84
     * @param  string  $query
85
     *
86
     * @return array|null
87
     */
88
    public function query(string $query): ?array
89
    {
90 3
        $queryString = http_build_query(['q' => $query]);
91
92 3
        return $this->getResponse("postcodes?$queryString");
93
    }
94
95
    /**
96
     * Get data for the postcodes nearest to the passed postcode
97
     *
98
     * @param string $postcode
99
     *
100
     * return array|null
101
     */
102 6
    public function nearest(string $postcode): ?array
103
    {
104 6
        return $this->getResponse("postcodes/$postcode/nearest");
105
    }
106
107
    /**
108
     * Lookup a terminated postcode. Returns the postcode, year and month of termination.
109
     *
110
     * @param string $postcode
111
     *
112 27
     * @return object
113
     */
114 27
    public function getTerminatedPostcode($postcode)
115
    {
116 27
        return $this->getResponse("terminated_postcodes/$postcode");
117 27
    }
118 18
119
    /**
120
     * Autocomplete a postcode partial.
121 27
     *
122
     * @param string $partialPostcode
123
     *
124
     * @return array|null
125
     */
126
    public function autocomplete(string $partialPostcode): ?array
127
    {
128
        return $this->getResponse("postcodes/$partialPostcode/autocomplete");
129
    }
130
131
    /**
132
     * Get nearest outward codes for a given longitude & latitude
133
     *
134
     * @param float $latitude
135
     * @param float $longitude
136
     *
137
     * @return array|null
138
     */
139
    public function nearestOutwardCodesForGivenLngAndLat(float $longitude, float $latitude): ?array
140
    {
141
        return $this->getResponse(sprintf(
142
            'outcodes?lon=%s&lat=%s',
143
            $longitude,
144
            $latitude
145
        ));
146
    }
147
148
    /**
149
     * Get the response and return the result object
150
     *
151
     * @param string $uri
152
     */
153
    protected function getResponse(string $uri = null)
154
    {
155
        $url = $this->url . $uri;
156
157
        $request = $this->http->request(
158
            'GET',
159
            $url
160
        );
161
162
        return json_decode($request->getBody()->getContents())->result;
163
    }
164
}
165