Completed
Push — master ( ca8cb8...c2fbff )
by Stephen
15s queued 11s
created

PostcodeService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
eloc 17
c 2
b 0
f 1
dl 0
loc 113
ccs 23
cts 23
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 3 1
A getPostcode() 0 3 1
A getRandomPostcode() 0 3 1
A __construct() 0 5 1
A query() 0 5 1
A getTerminatedPostcode() 0 3 1
A getResponse() 0 10 1
A autocomplete() 0 3 1
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 the address details from a random postcode
61
     *
62
     * @return object
63
     */
64 3
    public function getRandomPostcode()
65
    {
66 3
        return $this->getResponse("random/postcodes");
67
    }
68
69
    /**
70
     * Query the API for a given string
71
     *
72
     * @param  string  $query
73
     *
74
     * @return array|null
75
     */
76 3
    public function query(string $query): ?array
77
    {
78 3
        $queryString = http_build_query(['q' => $query]);
79
80 3
        return $this->getResponse("postcodes?$queryString");
81
    }
82
83
    /**
84
     * Lookup a terminated postcode. Returns the postcode, year and month of termination.
85
     *
86
     * @param string $postcode
87
     *
88
     * @return object
89
     */
90 3
    public function getTerminatedPostcode($postcode)
91
    {
92 3
        return $this->getResponse("terminated_postcodes/$postcode");
93
    }
94
95
    /**
96
     * Autocomplete a postcode partial.
97
     *
98
     * @param string $partialPostcode
99
     *
100
     * @return array|null
101
     */
102 6
    public function autocomplete(string $partialPostcode): ?array
103
    {
104 6
        return $this->getResponse("postcodes/$partialPostcode/autocomplete");
105
    }
106
107
    /**
108
     * Get the response and return the result object
109
     *
110
     * @param string $uri
111
     */
112 27
    protected function getResponse(string $uri = null)
113
    {
114 27
        $url = $this->url . $uri;
115
116 27
        $request = $this->http->request(
117 27
            'GET',
118 18
            $url
119
        );
120
121 27
        return json_decode($request->getBody()->getContents())->result;
122
    }
123
}
124