Test   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
eloc 38
c 4
b 1
f 2
dl 0
loc 92
ccs 37
cts 37
cp 1
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A upload() 0 15 2
A pokemonApiRelay() 0 20 4
A jwtaction() 0 10 1
A pokerelay() 0 5 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controllers\Api\V1;
6
7
use Nymfonya\Component\Container;
8
use Nymfonya\Component\Http\Response;
9
use Nymfonya\Component\Http\Request;
10
use App\Component\File\Uploader;
11
use App\Reuse\Controllers\Cacheable;
12
13
final class Test extends Cacheable
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
        $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
     * pokerelay
68
     *
69
     * @see https://pokeapi.co/
70
     * @return Test
71
     */
72 1
    final public function pokerelay(): Test
73
    {
74 1
        $pokeApiUrl = 'https://pokeapi.co/api/v2/pokemon/ditto/';
75 1
        $this->pokemonApiRelay($pokeApiUrl);
76 1
        return $this;
77
    }
78
79
    /**
80
     * run external api with cache and set response
81
     *
82
     * @param string $url
83
     * @return Test
84
     */
85 2
    protected function pokemonApiRelay(string $url): Test
86
    {
87 2
        if ($this->cacheFileExpired()) {
88
            $apiHeaders = [
89 1
                'Accept: application/json',
90
                //'Authorization: Bearer ' . $this->token
91
            ];
92 1
            $this->apiRelayRequest(Request::METHOD_GET, $url, $apiHeaders);
93 1
            if ($this->apiRelayHttpCode === Response::HTTP_OK) {
94 1
                $this->setFileCache($this->apiRelayResponse);
95
            }
96
        } else {
97 1
            $this->apiRelayResponse = $this->getFileCache();
98 1
            $this->apiRelayHttpCode = Response::HTTP_OK;
99
        }
100 2
        $statusCode = ($this->apiRelayHttpCode == Response::HTTP_OK)
101 2
            ? $this->apiRelayHttpCode
102 2
            : Response::HTTP_NOT_FOUND;
103 2
        $this->response->setCode($statusCode)->setContent($this->apiRelayResponse);
104 2
        return $this;
105
    }
106
}
107