Completed
Push — master ( ef1150...fac4d7 )
by Pierre
34:39 queued 30:55
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\Component\Http\Response;
8
use App\Component\Http\Request;
9
use App\Component\Container;
10
use App\Component\File\Uploader;
11
use App\Component\Cache\Redis\Adapter;
12
13
final class Test extends AbstractApi implements IApi
14
{
15
16
    use \App\Reuse\Controllers\Api\TRelay;
17
18
    /**
19
     * instanciate
20
     *
21
     * @param Container $container
22
     */
23 11
    public function __construct(Container $container)
24
    {
25 11
        parent::__construct($container);
26
    }
27
28
    /**
29
     * jwtaction
30
     *
31
     * @return Test
32
     */
33 1
    final public function jwtaction(): Test
34
    {
35 1
        $this->response->setCode(Response::HTTP_OK)->setContent([
36 1
            Response::_ERROR => false,
37 1
            Response::_ERROR_MSG => 'Jwt auth succeeded',
38
            'datas' => [
39 1
                'user' => $this->request->getSession('auth', 'user')
40
            ]
41
        ]);
42 1
        return $this;
43
    }
44
45
    /**
46
     * upload with jwt bearer
47
     *
48
     * @return Test
49
     */
50 1
    final public function upload(): Test
51
    {
52 1
        $appPath = dirname(dirname($this->request->getFilename()));
53 1
        $uploader = new Uploader();
54
        $uploader
55 1
            ->setTargetPath($appPath . '/assets/upload/')
56 1
            ->process();
57 1
        $resCodeError = $uploader->isError()
58 1
            ? Response::HTTP_INTERNAL_SERVER_ERROR
59 1
            : Response::HTTP_OK;
60 1
        $this->response->setCode($resCodeError)->setContent(
61 1
            $uploader->getInfos()
62
        );
63 1
        unset($uploader);
64 1
        return $this;
65
    }
66
67
    /**
68
     * check redis service
69
     *
70
     * @return Test
71
     */
72 1
    final public function redis(): Test
73
    {
74 1
        $redisService = $this->getContainer()->getService(Adapter::class);
75 1
        $client = $redisService->getClient();
76 1
        $error = $redisService->isError();
77 1
        $ping = '';
78 1
        $keys = [];
79 1
        if (false === $error) {
80 1
            $client->set('redis-entry-name', 'redis-entry-name-item');
81 1
            $client->lpush('redis-list', 'item0');
82 1
            $client->lpush('redis-list', 'item1');
83 1
            $client->lpush('redis-list', 'item2');
84 1
            $ping = $client->ping();
85 1
            $keys = $client->keys('*');
86
        }
87 1
        $resCodeError = $error
88
            ? Response::HTTP_INTERNAL_SERVER_ERROR
89 1
            : Response::HTTP_OK;
90 1
        $this->response->setCode($resCodeError)->setContent(
91
            [
92 1
                'error' => $error,
93 1
                'errorCode' => $redisService->getErrorCode(),
94 1
                'errorMessage' => $redisService->getErrorMessage(),
95
                'datas' => [
96 1
                    'keys' => $keys,
97 1
                    'ping' => $ping,
98
                ]
99
            ]
100
        );
101 1
        unset($redisService, $client);
102 1
        return $this;
103
    }
104
105
    /**
106
     * pokerelay
107
     *
108
     * @see https://pokeapi.co/
109
     * @return Test
110
     */
111 1
    final public function pokerelay(): Test
112
    {
113 1
        $pokeApiUrl = 'https://pokeapi.co/api/v2/pokemon/ditto/';
114 1
        $this->pokemonApiRelay($pokeApiUrl);
115 1
        return $this;
116
    }
117
118
    /**
119
     * run external api with cache and set response
120
     *
121
     * @param string $url
122
     * @return Test
123
     */
124 2
    protected function pokemonApiRelay(string $url): Test
125
    {
126 2
        if ($this->cacheExpired()) {
127
            $apiHeaders = [
128 1
                'Accept: application/json',
129
                //'Authorization: Bearer ' . $this->token
130
            ];
131 1
            $this->apiRelayRequest(Request::METHOD_GET, $url, $apiHeaders);
132 1
            if ($this->apiRelayHttpCode === Response::HTTP_OK) {
133 1
                $this->setCache($this->apiRelayResponse);
134
            }
135
        } else {
136 1
            $this->apiRelayResponse = $this->getCache();
137 1
            $this->apiRelayHttpCode = Response::HTTP_OK;
138
        }
139 2
        $statusCode = ($this->apiRelayHttpCode == Response::HTTP_OK)
140 2
            ? $this->apiRelayHttpCode
141 2
            : Response::HTTP_NOT_FOUND;
142 2
        $this->response->setCode($statusCode)->setContent($this->apiRelayResponse);
143 2
        return $this;
144
    }
145
}
146