Completed
Push — master ( fdb933...3b2cfe )
by Ramesh
07:20 queued 04:23
created

Service::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace SmartyStreets;
4
5
use SmartyStreets\Location\Location;
6
use SmartyStreets\Location\Lookup;
7
use GuzzleHttp\Client;
8
9
class Service
10
{
11
    protected $authId;
12
13
    protected $authToken;
14
15
    private $simulation = false;
16
17
    public function __construct($authId, $authToken, $simulation = false)
18
    {
19
        $this->authId = $authId;
20
        $this->authToken = $authToken;
21
        $this->setSimulation($simulation);
22
    }
23
24
    public function setSimulation($value)
25
    {
26
        $this->simulation = $value;
27
    }
28
29
    public function getSimulation()
30
    {
31
        return $this->simulation;
32
    }
33
34
    public function getInspectedLocation($location)
35
    {
36
        if ($this->getSimulation()) {
37
            return new Location($this->getSampleLocation());
38
        }
39
40
        return new Location($this->getRequest(new Lookup($location)));
41
    }
42
43
    public function getRequest(Lookup $location)
44
    {
45
        $client = new Client([
46
            'base_uri' => 'https://international-street.api.smartystreets.com/',
47
        ]);
48
        $response = $client->request('GET', 'verify', [
49
            'query' => [
50
                'auth-id'     => $this->authId,
51
                'auth-token'  => $this->authToken,
52
                'address1'    => $location->getStreet().' '.$location->getStreetNumber(),
53
                'locality'    => $location->getPlace(),
54
                'postal_code' => $location->getPostcode(),
55
                'country'     => $location->getCountry(),
56
                'geocode'     => $location->getGeocode(),
57
            ],
58
        ]);
59
60
        $body = json_decode($response->getBody(), true);
61
62
        return reset($body);
63
    }
64
65
    protected function getSampleLocation()
66
    {
67
        return [
68
            'address1'   => 'Birgistrasse 10',
69
            'address2'   => '8304 Wallisellen',
70
            'components' => [
71
72
                'administrative_area'     => 'ZH',
73
                'sub_administrative_area' => 'Bülach',
74
                'country_iso_3'           => 'CHE',
75
                'locality'                => 'Wallisellen',
76
                'postal_code'             => '8304',
77
                'postal_code_short'       => '8304',
78
                'premise'                 => '10',
79
                'premise_number'          => '10',
80
                'thoroughfare'            => 'Birgistrasse',
81
82
            ],
83
            'metadata' => [
84
85
                'latitude'              => 47.41603,
86
                'longitude'             => 8.57788,
87
                'geocode_precision'     => 'Premise',
88
                'max_geocode_precision' => 'DeliveryPoint',
89
                'address_format'        => 'thoroughfare premise|postal_code locality',
90
            ],
91
            'analysis' => [
92
                'verification_status'   => 'Verified',
93
                'address_precision'     => 'Premise',
94
                'max_address_precision' => 'Premise',
95
            ],
96
        ];
97
    }
98
}
99