Issues (32)

src/Model/API/ApiModel.php (4 issues)

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
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15
16
abstract class ApiModel
17
{
18
19
    /**
20
     * Generic function to GET /api/ OpenLRW routes
21
     *
22
     * @param String $route
23
     * @param String $prefix
24
     * @return mixed|object
25
     * @throws \Exception
26
     */
27
    public static function get(String $route)
28
    {
29
        try {
30
            return json_decode(Provider::$http->request(
31
                'GET',
32
                'api/' . $route,
33
                [
34
                    'headers' => [
35
                        'X-Requested-With' => 'XMLHttpRequest',
36
                        'Authorization' => 'Bearer ' . Provider::makeJWT()
0 ignored issues
show
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

36
                        'Authorization' => 'Bearer ' . /** @scrutinizer ignore-type */ Provider::makeJWT()
Loading history...
37
                    ]
38
                ])
39
                ->getBody()
40
                ->getContents()
41
            );
42
        } catch (GuzzleException $e) {
43
            if ($e->getCode() == 401) {
44
                try {
45
                    Provider::generateJwt();
46
                } catch (GuzzleException $e) {
47
                    die($e->getMessage());
0 ignored issues
show
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...
48
                }
49
                self::get($route);
50
            } else if ($e->getCode() == 404) {
51
                return null;
52
            } else {
53
                throw new \Exception($e);
0 ignored issues
show
$e of type GuzzleHttp\Exception\GuzzleException is incompatible with the type string expected by parameter $message of Exception::__construct(). ( Ignorable by Annotation )

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

53
                throw new \Exception(/** @scrutinizer ignore-type */ $e);
Loading history...
54
            }
55
            return null;
56
        }
57
58
    }
59
60
61
    /**
62
     * Generic function to POST OpenLRW routes
63
     *
64
     * @param String $route
65
     * @param array $args
66
     * @return mixed|object
67
     * @throws \GuzzleHttp\Exception\GuzzleException
68
     */
69
    public function post(String $route, array $args) // ToDo : add JWT
70
    {
71
       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...
72
           'headers' => [ 'X-Requested-With' => 'XMLHttpRequest' ],
73
           'json' => $args
74
       ])->getBody()->getContents());
75
    }
76
77
78
79
80
}