Completed
Pull Request — dev (#321)
by Tristan
06:34
created

JSONFetcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
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
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
13
class JSONFetcher implements JSONGetter, JSONPoster
14
{
15
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
16
     * @var \GuzzleHttp\Client
17
     */
18
    private $client;
0 ignored issues
show
Coding Style introduced by
Private member variable "client" must be prefixed with an underscore
Loading history...
19
    /**
20
     * JSONFetcher constructor.
21
     *
22
     * @param \GuzzleHttp\Client $client
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
23
     */
24
    public function __construct(Client $client)
25
    {
26
        $this->client = $client;
27
    }
28
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $url should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $params should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $options should have a doc-comment as per coding-style.
Loading history...
29
     * {@inheritdoc}
30
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
31
    public function get(string $url, array $params = [], array $options = []): ?array
32
    {
33
        $reqOpts = array_merge([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
34
            'query' => $params,
35
            'headers' => [
36
                'Accept' => 'application/json',
37
            ],
38
        ], $options);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
39
        return $this->request("GET", $url, $reqOpts);
40
    }
41
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
42
     * @param string $method
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
43
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
44
     * @param array  $options
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
45
     *
46
     * @return array
47
     */
48
    protected function request(string $method, string $url, array $options): ?array
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
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $url should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $options should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $params should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $body should have a doc-comment as per coding-style.
Loading history...
56
     * {@inheritdoc}
57
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
58
    public function post(string $url, array $params = [], $body = null, array $options = []): array
59
    {
60
        $reqOpts = array_merge([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
61
            'query' => $params,
62
            'headers' => [
63
                'Accept' => 'application/json',
64
                'Content-Type' => 'application/x-www-form-urlencoded',
65
            ],
66
            'body' => $body,
67
        ], $options);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
68
        return $this->request("POST", $url, $reqOpts);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->request('POST', $url, $reqOpts) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
69
    }
70
}
71