Passed
Push — master ( 56e406...e8d866 )
by Pierre
02:35 queued 11s
created

Test::upload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 13
ccs 0
cts 11
cp 0
crap 6
rs 9.9332
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\Tools\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
    public function __construct(Container $container)
23
    {
24
        parent::__construct($container);
25
    }
26
27
    /**
28
     * jwtaction
29
     *
30
     * @return Test
31
     */
32
    final public function jwtaction(): Test
33
    {
34
        $this->response->setCode(Response::HTTP_OK)->setContent([
35
            Response::_ERROR => false,
36
            Response::_ERROR_MSG => 'Jwt auth succeeded',
37
            'datas' => [
38
                'user' => $this->request->getSession('auth', 'user')
39
            ]
40
        ]);
41
        return $this;
42
    }
43
44
    /**
45
     * upload
46
     *
47
     * @return Test
48
     */
49
    final public function upload(): Test
50
    {
51
        $upload = new Uploader($this->getContainer());
52
        $appPath = dirname(dirname($this->request->getFilename()));
53
        $upload->setTargetPath($appPath . '/assets/upload/')->process();
54
        $resCodeError = $upload->getError()
55
            ? Response::HTTP_INTERNAL_SERVER_ERROR
56
            : Response::HTTP_OK;
57
        $this->response->setCode($resCodeError)->setContent(
58
            $upload->getInfos()
59
        );
60
        unset($upload);
61
        return $this;
62
    }
63
64
    /**
65
     * pokerelay
66
     *
67
     * @see https://pokeapi.co/
68
     * @return Test
69
     */
70
    final public function pokerelay(): Test
71
    {
72
        $pokeApiUrl = 'https://pokeapi.co/api/v2/pokemon/ditto/';
73
        $this->pokemonApiRelay($pokeApiUrl);
74
        return $this;
75
    }
76
77
    /**
78
     * run external api with cache and set response
79
     *
80
     * @param string $url
81
     * @return Test
82
     */
83
    protected function pokemonApiRelay(string $url): Test
84
    {
85
        if ($this->cacheExpired()) {
86
            $this->apiRelayRequest(
87
                Request::METHOD_GET,
88
                $url,
89
                [
90
                    'Accept: application/json',
91
                    //'Authorization: Bearer ' . $this->token
92
                ]
93
            );
94
            if ($this->apiRelayHttpCode === Response::HTTP_OK) {
95
                $this->setCache($this->apiRelayResponse);
96
            }
97
        } else {
98
            $this->apiRelayResponse = $this->getCache();
99
            $this->apiRelayHttpCode = Response::HTTP_OK;
100
        }
101
        $statusCode = ($this->apiRelayHttpCode == Response::HTTP_OK)
102
            ? $this->apiRelayHttpCode
103
            : Response::HTTP_NOT_FOUND;
104
        $this->response->setCode($statusCode)->setContent($this->apiRelayResponse);
105
        return $this;
106
    }
107
}
108