Completed
Push — master ( 81b3a2...8e331d )
by Pierre
03:08 queued 45s
created

Test::jwtaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 6
c 1
b 1
f 1
nc 1
nop 0
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 1
rs 10
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
use App\Tools\File\Uploader;
11
12
final class Test extends AbstractApi implements IApi
13
{
14
15
    use \App\Reuse\Controllers\Api\TRelay;
16
17
    /**
18
     * instanciate
19
     *
20
     * @param Container $container
21
     */
22 10
    public function __construct(Container $container)
23
    {
24 10
        parent::__construct($container);
25
    }
26
27
    /**
28
     * jwtaction
29
     *
30
     * @return Test
31
     */
32 1
    final public function jwtaction(): Test
33
    {
34 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
35 1
            Response::_ERROR => false,
36 1
            Response::_ERROR_MSG => 'Jwt auth succeeded',
37
            'datas' => [
38 1
                'user' => $this->request->getSession('auth', 'user')
39
            ]
40
        ]);
41 1
        return $this;
42
    }
43
44
    /**
45
     * upload with jwt bearer
46
     *
47
     * @return Test
48
     */
49 1
    final public function upload(): Test
50
    {
51 1
        $appPath = dirname(dirname($this->request->getFilename()));
52 1
        $upload = (new Uploader())
53 1
            ->setTargetPath($appPath . '/assets/upload/')
54 1
            ->process();
55 1
        $resCodeError = $upload->isError()
56 1
            ? Response::HTTP_INTERNAL_SERVER_ERROR
57 1
            : Response::HTTP_OK;
58 1
        $this->response->setCode($resCodeError)->setContent(
59 1
            $upload->getInfos()
60
        );
61 1
        unset($upload);
62 1
        return $this;
63
    }
64
65
    /**
66
     * pokerelay
67
     *
68
     * @see https://pokeapi.co/
69
     * @return Test
70
     */
71 1
    final public function pokerelay(): Test
72
    {
73 1
        $pokeApiUrl = 'https://pokeapi.co/api/v2/pokemon/ditto/';
74 1
        $this->pokemonApiRelay($pokeApiUrl);
75 1
        return $this;
76
    }
77
78
    /**
79
     * run external api with cache and set response
80
     *
81
     * @param string $url
82
     * @return Test
83
     */
84 2
    protected function pokemonApiRelay(string $url): Test
85
    {
86 2
        if ($this->cacheExpired()) {
87
            $apiHeaders = [
88 1
                'Accept: application/json',
89
                //'Authorization: Bearer ' . $this->token
90
            ];
91 1
            $this->apiRelayRequest(Request::METHOD_GET, $url, $apiHeaders);
92 1
            if ($this->apiRelayHttpCode === Response::HTTP_OK) {
93 1
                $this->setCache($this->apiRelayResponse);
94
            }
95
        } else {
96 1
            $this->apiRelayResponse = $this->getCache();
97 1
            $this->apiRelayHttpCode = Response::HTTP_OK;
98
        }
99 2
        $statusCode = ($this->apiRelayHttpCode == Response::HTTP_OK)
100 2
            ? $this->apiRelayHttpCode
101 2
            : Response::HTTP_NOT_FOUND;
102 2
        $this->response->setCode($statusCode)->setContent($this->apiRelayResponse);
103 2
        return $this;
104
    }
105
}
106