Completed
Push — master ( 002acb...069052 )
by Pierre
03:01
created

Test::pokerelay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
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\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 11
    public function __construct(Container $container)
23
    {
24 11
        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 1
    final public function redis(): Test
71
    {
72 1
        $redisService = $this->getContainer()->getService(
73 1
            \App\Component\Cache\Redis\Adapter::class
74
        );
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($upload);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $upload seems to be never defined.
Loading history...
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