GetDepartmentFromPostalCodeImpl::execute()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.9332
1
<?php
2
3
4
namespace App\Src\UseCases\Domain\System;
5
6
7
use GuzzleHttp\Client;
8
9
class GetDepartmentFromPostalCodeImpl implements GetDepartmentFromPostalCode
10
{
11
    private $httpClient;
12
    private $api = 'https://api-adresse.data.gouv.fr/search/?q=';
13
14
    public function __construct()
15
    {
16
        $this->httpClient = new Client();
17
    }
18
19
20
    public function execute(string $postalCode)
21
    {
22
        $response = $this->httpClient->get($this->api.$postalCode);
23
        $content = json_decode($response->getBody()->getContents(), true);
24
25
        $features = $content['features'];
26
        if(isset($features) && !empty($features)){
27
            $feature = $features[0];
28
            $coordinates = $feature['geometry']['coordinates'];
29
            $departmentNumber = explode(',', $feature['properties']['context'])[0];
30
        }
31
32
        return [
33
            'coordinates' => $coordinates,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $coordinates does not seem to be defined for all execution paths leading up to this point.
Loading history...
34
            'department_number' => $departmentNumber
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $departmentNumber does not seem to be defined for all execution paths leading up to this point.
Loading history...
35
        ];
36
    }
37
}
38