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