Completed
Push — master ( 420da5...d93dfb )
by Pierre
43:19 queued 10:32
created

Test::redis()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 25
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 33
rs 9.52
ccs 16
cts 16
cp 1
crap 3
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\Component\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
     * check redis service
67
     *
68
     * @return Test
69
     */
70
    final public function redis(): Test
71 1
    {
72
        $redisService = $this->getContainer()->getService(
73 1
            \App\Component\Cache\Redis\Adapter::class
74 1
        );
75 1
        $client = $redisService->getClient();
76
        $error = $redisService->isError();
77
        $ping = '';
78
        $keys = [];
79
        if (false === $error) {
80
            $client->set('redis-entry-name', 'redis-entry-name-item');
81
            $client->lpush('redis-list', 'item0');
82
            $client->lpush('redis-list', 'item1');
83
            $client->lpush('redis-list', 'item2');
84 2
            $ping = $client->ping();
85
            $keys = $client->keys('*');
86 2
        }
87
        $resCodeError = $error
88 1
            ? Response::HTTP_INTERNAL_SERVER_ERROR
89
            : Response::HTTP_OK;
90
        $this->response->setCode($resCodeError)->setContent(
91 1
            [
92 1
                'error' => $error,
93 1
                'errorCode' => $redisService->getErrorCode(),
94
                'errorMessage' => $redisService->getErrorMessage(),
95
                'datas' => [
96 1
                    'keys' => $keys,
97 1
                    'ping' => $ping,
98
                ]
99 2
            ]
100 2
        );
101 2
        unset($upload);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $upload seems to be never defined.
Loading history...
102 2
        return $this;
103 2
    }
104
105
    /**
106
     * pokerelay
107
     *
108
     * @see https://pokeapi.co/
109
     * @return Test
110
     */
111
    final public function pokerelay(): Test
112
    {
113
        $pokeApiUrl = 'https://pokeapi.co/api/v2/pokemon/ditto/';
114
        $this->pokemonApiRelay($pokeApiUrl);
115
        return $this;
116
    }
117
118
    /**
119
     * run external api with cache and set response
120
     *
121
     * @param string $url
122
     * @return Test
123
     */
124
    protected function pokemonApiRelay(string $url): Test
125
    {
126
        if ($this->cacheExpired()) {
127
            $apiHeaders = [
128
                'Accept: application/json',
129
                //'Authorization: Bearer ' . $this->token
130
            ];
131
            $this->apiRelayRequest(Request::METHOD_GET, $url, $apiHeaders);
132
            if ($this->apiRelayHttpCode === Response::HTTP_OK) {
133
                $this->setCache($this->apiRelayResponse);
134
            }
135
        } else {
136
            $this->apiRelayResponse = $this->getCache();
137
            $this->apiRelayHttpCode = Response::HTTP_OK;
138
        }
139
        $statusCode = ($this->apiRelayHttpCode == Response::HTTP_OK)
140
            ? $this->apiRelayHttpCode
141
            : Response::HTTP_NOT_FOUND;
142
        $this->response->setCode($statusCode)->setContent($this->apiRelayResponse);
143
        return $this;
144
    }
145
}
146