Completed
Push — master ( 579110...b52e58 )
by Pierre
02:10
created

Test   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 27
c 1
b 1
f 1
dl 0
loc 74
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A pokemonApiRelay() 0 23 4
A jwtaction() 0 10 1
A pokerelay() 0 5 1
A __construct() 0 3 1
1
<?php
2
3
namespace App\Controllers\Api\V1;
4
5
use App\Interfaces\Controllers\IApi;
6
use App\Reuse\Controllers\AbstractApi;
7
use App\Http\Response;
8
use App\Http\Request;
9
use App\Container;
10
11
final class Test extends AbstractApi implements IApi
12
{
13
14
    use \App\Reuse\Controllers\Api\TRelay;
15
16
    /**
17
     * instanciate
18
     *
19
     * @param Container $container
20
     */
21
    public function __construct(Container $container)
22
    {
23
        parent::__construct($container);
24
    }
25
26
    /**
27
     * jwtaction
28
     *
29
     * @return Test
30
     */
31
    final public function jwtaction(): Test
32
    {
33
        $this->response->setCode(Response::HTTP_OK)->setContent([
34
            Response::_ERROR => false,
35
            Response::_ERROR_MSG => 'Jwt auth succeeded',
36
            'datas' => [
37
                'user' => $this->request->getSession('auth', 'user')
38
            ]
39
        ]);
40
        return $this;
41
    }
42
43
    /**
44
     * pokerelay
45
     *
46
     * @see https://pokeapi.co/
47
     * @return Test
48
     */
49
    final public function pokerelay(): Test
50
    {
51
        $pokeApiUrl = 'https://pokeapi.co/api/v2/pokemon/ditto/';
52
        $this->pokemonApiRelay($pokeApiUrl);
53
        return $this;
54
    }
55
56
    /**
57
     * run external api with cache and set response
58
     *
59
     * @param string $url
60
     * @return Test
61
     */
62
    protected function pokemonApiRelay(string $url):Test
63
    {
64
        if ($this->cacheExpired()) {
65
            $this->apiRelayRequest(
66
                Request::METHOD_GET,
67
                $url,
68
                [
69
                    'Accept: application/json',
70
                    //'Authorization: Bearer ' . $this->token
71
                ]
72
            );
73
            if ($this->apiRelayHttpCode === Response::HTTP_OK) {
74
                $this->setCache($this->apiRelayResponse);
0 ignored issues
show
Bug introduced by
It seems like $this->apiRelayResponse can also be of type boolean; however, parameter $content of App\Reuse\Controllers\AbstractApi::setCache() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

74
                $this->setCache(/** @scrutinizer ignore-type */ $this->apiRelayResponse);
Loading history...
75
            }
76
        } else {
77
            $this->apiRelayResponse = $this->getCache();
78
            $this->apiRelayHttpCode = Response::HTTP_OK;
79
        }
80
        $statusCode = ($this->apiRelayHttpCode == Response::HTTP_OK)
81
            ? $this->apiRelayHttpCode
82
            : Response::HTTP_NOT_FOUND;
83
        $this->response->setCode($statusCode)->setContent($this->apiRelayResponse);
84
        return $this;
85
    }
86
}
87