Passed
Push — bugfix/relatives_not_saving ( 6485fa...f1e1c5 )
by Tristan
13:39
created

JSONFetcher   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 20
dl 0
loc 56
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A post() 0 11 1
A __construct() 0 3 1
A request() 0 6 1
A get() 0 9 1
1
<?php
2
3
namespace App\Services\Auth;
4
5
use GuzzleHttp\Client;
6
use App\Services\Auth\Contracts\JSONGetter;
7
use App\Services\Auth\Contracts\JSONPoster;
8
9
/**
10
 * Adapted from the OpenIDConnect Laravel package at
11
 * https://github.com/furdarius/oidconnect-laravel
12
 */
13
class JSONFetcher implements JSONGetter, JSONPoster
14
{
15
    /**
16
     * @var \GuzzleHttp\Client
17
     */
18
    private $client;
19
    /**
20
     * JSONFetcher constructor.
21
     *
22
     * @param \GuzzleHttp\Client $client
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
23
     */
24
    public function __construct(Client $client)
25
    {
26
        $this->client = $client;
27
    }
28
    /**
3 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$url" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$params" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$options" missing
Loading history...
29
     * {@inheritdoc}
30
     */
1 ignored issue
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
31
    public function get(string $url, array $params = [], array $options = [])
32
    {
33
        $reqOpts = array_merge([
34
            'query' => $params,
35
            'headers' => [
36
                'Accept' => 'application/json',
37
            ],
38
        ], $options);
39
        return $this->request("GET", $url, $reqOpts);
40
    }
41
    /**
42
     * @param string $method
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
43
     * @param string $url
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
44
     * @param array  $options
1 ignored issue
show
introduced by
@param annotation of method \App\Services\Auth\JSONFetcher::request() does not specify type hint for items of its traversable parameter $options.
Loading history...
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
45
     *
46
     * @return array
0 ignored issues
show
introduced by
@return annotation of method \App\Services\Auth\JSONFetcher::request() does not specify type hint for items of its traversable return value.
Loading history...
47
     */
48
    protected function request(string $method, string $url, array $options)
0 ignored issues
show
introduced by
Method \App\Services\Auth\JSONFetcher::request() does not have return type hint for its return value but it should be possible to add it based on @return annotation "array".
Loading history...
49
    {
50
        $response = $this->client->request($method, $url, $options);
51
        // TODO: Handle request errors (ex.: authorization error with 403 status code)
52
        $data = $response->getBody()->getContents();
53
        return json_decode($data, true);
54
    }
55
    /**
4 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$url" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$params" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$body" missing
Loading history...
Coding Style Documentation introduced by
Doc comment for parameter "$options" missing
Loading history...
56
     * {@inheritdoc}
57
     */
1 ignored issue
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
58
    public function post(string $url, array $params = [], $body = null, array $options = [])
59
    {
60
        $reqOpts = array_merge([
61
            'query' => $params,
62
            'headers' => [
63
                'Accept' => 'application/json',
64
                'Content-Type' => 'application/x-www-form-urlencoded',
65
            ],
66
            'body' => $body,
67
        ], $options);
68
        return $this->request("POST", $url, $reqOpts);
69
    }
70
}
71