Completed
Push — master ( 791c20...d90c8e )
by Pierre
03:16
created

Test::orm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
     * orm make a simple select on db table
107
     *
108
     * @return Test
109
     */
110
    final public function orm():Test
111
    {
112
        return $this;
113
    }
114
115
    /**
116
     * pokerelay
117
     *
118
     * @see https://pokeapi.co/
119
     * @return Test
120
     */
121 1
    final public function pokerelay(): Test
122
    {
123 1
        $pokeApiUrl = 'https://pokeapi.co/api/v2/pokemon/ditto/';
124 1
        $this->pokemonApiRelay($pokeApiUrl);
125 1
        return $this;
126
    }
127
128
    /**
129
     * run external api with cache and set response
130
     *
131
     * @param string $url
132
     * @return Test
133
     */
134 2
    protected function pokemonApiRelay(string $url): Test
135
    {
136 2
        if ($this->cacheExpired()) {
137
            $apiHeaders = [
138 1
                'Accept: application/json',
139
                //'Authorization: Bearer ' . $this->token
140
            ];
141 1
            $this->apiRelayRequest(Request::METHOD_GET, $url, $apiHeaders);
142 1
            if ($this->apiRelayHttpCode === Response::HTTP_OK) {
143 1
                $this->setCache($this->apiRelayResponse);
144
            }
145
        } else {
146 1
            $this->apiRelayResponse = $this->getCache();
147 1
            $this->apiRelayHttpCode = Response::HTTP_OK;
148
        }
149 2
        $statusCode = ($this->apiRelayHttpCode == Response::HTTP_OK)
150 2
            ? $this->apiRelayHttpCode
151 2
            : Response::HTTP_NOT_FOUND;
152 2
        $this->response->setCode($statusCode)->setContent($this->apiRelayResponse);
153 2
        return $this;
154
    }
155
}
156