Passed
Push — master ( b4b547...a14240 )
by Xavier
05:36
created

ApiModel::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * @author  Xavier Chopin <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace App\Model\API;
11
12
use App\Controller\AbstractController as Provider;
13
use GuzzleHttp\Exception\GuzzleException;
14
15
abstract class ApiModel
16
{
17
18
    /**
19
     * Generic function to GET OpenLRW routes
20
     *
21
     * @param String $route
22
     * @return mixed|object
23
     */
24
    public static function get(String $route)
25
    {
26
        try {
27
            return json_decode(Provider::$http->request(
28
                'GET',
29
                "api/$route",
30
                [
31
                    'headers' => [
32
                        'X-Requested-With' => 'XMLHttpRequest',
33
                        'Authorization' => 'Bearer ' . Provider::makeJWT()
0 ignored issues
show
Bug introduced by
Are you sure App\Controller\AbstractController::makeJWT() of type mixed|Psr\Http\Message\ResponseInterface 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

33
                        'Authorization' => 'Bearer ' . /** @scrutinizer ignore-type */ Provider::makeJWT()
Loading history...
34
                    ]
35
                ])
36
                ->getBody()
37
                ->getContents()
38
            );
39
        } catch (GuzzleException $e) {
40
            if ($e->getCode() == 401) {
41
                try {
42
                    Provider::generateJwt();
43
                } catch (GuzzleException $e) {
44
                    die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
45
                }
46
                self::get($route);
47
            }
48
            return null;
49
        }
50
51
    }
52
53
    /**
54
     * Generic function to POST OpenLRW routes
55
     *
56
     * @param String $route
57
     * @param array $args
58
     * @return mixed|object
59
     * @throws \GuzzleHttp\Exception\GuzzleException
60
     */
61
    public function post(String $route, array $args) // ToDo : add JWT
62
    {
63
       return json_decode( self::$http->request('POST', $route, [
0 ignored issues
show
Bug Best Practice introduced by
The property http does not exist on App\Model\API\ApiModel. Did you maybe forget to declare it?
Loading history...
64
           'headers' => [ 'X-Requested-With' => 'XMLHttpRequest' ],
65
           'json' => $args
66
       ])->getBody()->getContents());
67
    }
68
69
}