Passed
Push — master ( 4c9e55...edca11 )
by Evert
03:38
created

VeloClient::fetchStations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 17
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Earnould\LaravelVeloApi;
3
4
use Closure;
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Cache;
8
use Earnould\LaravelVeloApi\Exceptions\VeloException;
9
10
class VeloClient
11
{
12
    public function __construct(Client $client = null)
13
    {
14
        $this->guzzleClient = $client ?: new Client();
0 ignored issues
show
Bug Best Practice introduced by
The property guzzleClient does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
15
        $this->client_id = config('laravel-velo-api.client_id');
0 ignored issues
show
Bug Best Practice introduced by
The property client_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16
        $this->client_secret = config('laravel-velo-api.client_secret');
0 ignored issues
show
Bug Best Practice introduced by
The property client_secret does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        $this->token_url = "https://antwerp.pub.api.smartbike.com/oauth/v2/token";
0 ignored issues
show
Bug Best Practice introduced by
The property token_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
        $this->api_url = "https://antwerp.pub.api.smartbike.com/api/en/v3";
0 ignored issues
show
Bug Best Practice introduced by
The property api_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
        $this->apiResponseFormat = '.json';
0 ignored issues
show
Bug Best Practice introduced by
The property apiResponseFormat does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
    }
21
22
    
23
    /**
24
     * Request Velo API Access Token response
25
     *
26
     * @return void
27
     */
28
    private function requestAccessToken() : string
29
    {   
30
        $response = $this->guzzleClient->post($this->token_url, [
31
            'form_params'   =>  [
32
                'client_id'     => $this->client_id,
33
                'client_secret' => $this->client_secret,
34
                'grant_type'    => 'client_credentials'
35
            ],
36
        ]);
37
38
        $access_token = json_decode($response->getBody()->getContents())->access_token;
39
        
40
        if(!empty($access_token)) {
41
            return $access_token;
42
        }
43
        
44
        throw new VeloException("An access token is required to perform requests tothe Velo API", 403);
45
    }
46
47
    /**
48
     * Get cached token or request a new one.
49
     *
50
     * @return void
51
     */
52
    public function getAccessToken()
53
    {
54
        return Cache::remember('access_token', 55, Closure::fromCallable([$this, 'requestAccessToken']));
55
    }
56
57
    /**
58
     * Prepare the uri for a request
59
     *
60
     * @param [type] $resource
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
61
     * @return string
62
     */
63
    function prepareUri($resource) : string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
    {
65
        $uri = $this->api_url . '/' . $resource . $this->apiResponseFormat;
66
67
        return $uri;
68
    }
69
70
    /**
71
     * Fetch all Velo stations
72
     *
73
     * @return Collection
74
     */
75
    function fetchStations() : Collection
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
76
    {
77
        $response = $this->guzzleClient->get($this->prepareUri('stations'), [
78
            'headers' => [
79
                'Authorization' => 'Bearer ' . $this->getAccessToken(),
0 ignored issues
show
Bug introduced by
Are you sure $this->getAccessToken() of type void can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
                'Authorization' => 'Bearer ' . /** @scrutinizer ignore-type */ $this->getAccessToken(),
Loading history...
Bug introduced by
Are you sure the usage of $this->getAccessToken() targeting Earnould\LaravelVeloApi\...lient::getAccessToken() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
80
            ]
81
        ]);
82
        
83
        try
84
        {
85
            $stations = json_decode($response->getBody()->getContents())->stations;
86
        }
87
        catch (\ErrorException $e) {
88
            throw new VeloException($e->getMessage(), $e->getCode());
89
        }
90
91
        return collect($stations);
92
    }
93
94
    /**
95
     * Fetch all Velo stations statuses
96
     *
97
     * @return Collection
98
     */
99
    function fetchStationsStatuses() : Collection
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
100
    {
101
        $response = $this->guzzleClient->get($this->prepareUri('stations/status'), [
102
            'headers' => [
103
                'Authorization' => 'Bearer ' . $this->getAccessToken(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getAccessToken() targeting Earnould\LaravelVeloApi\...lient::getAccessToken() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure $this->getAccessToken() of type void can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
                'Authorization' => 'Bearer ' . /** @scrutinizer ignore-type */ $this->getAccessToken(),
Loading history...
104
            ]
105
        ]);
106
            
107
        try
108
        {
109
            $stationsStatuses = json_decode($response->getBody()->getContents())->stationsStatus;
110
        }
111
        catch (\ErrorException $e) {
112
            throw new VeloException($e->getMessage(), $e->getCode());
113
        }
114
115
        return collect($stationsStatuses);
116
    }
117
}