AddressControl::detail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
nc 1
nop 1
dl 0
loc 19
rs 9.7998
c 1
b 0
f 0
1
<?php
2
3
namespace DansMaCulotte\LaPoste;
4
5
use DansMaCulotte\LaPoste\Resources\Address;
6
7
/**
8
 * Implementation of ControlAdresse Web Service
9
 * https://developer.laposte.fr/products/controladresse/latest
10
 */
11
class AddressControl extends LaPoste
12
{
13
    /** @var string */
14
    const SERVICE_URI = '/controladresse/v1/adresses';
15
16
    /**
17
     * Construct Method
18
     *
19
     * @param string $apiKey La Poste Developer API Key
20
     */
21
    public function __construct(string $apiKey)
22
    {
23
        parent::__construct($apiKey);
24
    }
25
26
    /**
27
     * Find an address matching with the selector
28
     *
29
     * @param string $address An address located in France.
30
     *
31
     * @return array
32
     * @throws \GuzzleHttp\Exception\GuzzleException
33
     */
34
    public function find(string $address)
35
    {
36
        $response = $this->httpClient->request(
37
            'GET',
38
            self::SERVICE_URI,
39
            [
40
                'query' => [
41
                    'q' => $address
42
                ]
43
            ]
44
        );
45
46
        $body = json_decode((string) $response->getBody(), true);
47
48
        return $body;
49
    }
50
51
    /**
52
     * Get details on a specific address
53
     *
54
     * @param string $code A code of an address
55
     *
56
     * @return Address
57
     * @throws \GuzzleHttp\Exception\GuzzleException
58
     */
59
    public function detail(string $code)
60
    {
61
        $response = $this->httpClient->request(
62
            'GET',
63
            self::SERVICE_URI. '/' . $code
64
        );
65
66
        $body = json_decode((string) $response->getBody(), true);
67
68
        return new Address(
69
            $body['destinataire'],
70
            $body['pointRemise'],
71
            $body['numeroVoie'],
72
            $body['libelleVoie'],
73
            $body['lieuDit'],
74
            $body['codePostal'],
75
            $body['codeCedex'],
76
            $body['commune'],
77
            $body['blocAdresse']
78
        );
79
    }
80
}
81