DefaultController::getStatus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use Slim\Container;
8
use Slim\Http\Request;
9
use Slim\Http\Response;
10
11
class DefaultController extends BaseController
12
{
13
    const API_VERSION = '0.45.0';
14
15
    public function __construct(Container $container)
16
    {
17
        $this->container = $container;
18
    }
19
20
    public function getHelp(Request $request, Response $response, array $args): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
    {
22
        $url = getenv('APP_DOMAIN');
23
        $endpoints = [
24
            'tasks' => $url . '/api/v1/tasks',
25
            'users' => $url . '/api/v1/users',
26
            'notes' => $url . '/api/v1/notes',
27
            'docs' => $url . '/docs/index.html',
28
            'status' => $url . '/status',
29
            'this help' => $url . '',
30
        ];
31
        $message = [
32
            'endpoints' => $endpoints,
33
            'version' => self::API_VERSION,
34
            'timestamp' => time(),
35
        ];
36
37
        return $this->jsonResponse($response, 'success', $message, 200);
38
    }
39
40
    public function getStatus(Request $request, Response $response, array $args): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        $status = [
43
            'stats' => $this->getDbStats(),
44
            'MySQL' => 'OK',
45
            'Redis' => $this->checkRedisConnection(),
46
            'version' => self::API_VERSION,
47
            'timestamp' => time(),
48
        ];
49
50
        return $this->jsonResponse($response, 'success', $status, 200);
51
    }
52
53
    private function getDbStats(): array
54
    {
55
        $userService = $this->container->get('user_service');
56
        $taskService = $this->container->get('task_service');
57
        $noteService = $this->container->get('get_all_note_service');
58
59
        return [
60
            'users' => count($userService->getAll()),
61
            'tasks' => count($taskService->getAllTasks()),
62
            'notes' => count($noteService->getAll()),
63
        ];
64
    }
65
66
    private function checkRedisConnection(): string
67
    {
68
        $redis = 'Disabled';
69
        if (self::isRedisEnabled() === true) {
70
            $redisService = $this->container->get('redis_service');
71
            $redisKey = 'test:status';
72
            $key = $redisService->generateKey($redisKey);
73
            $redisService->set($key, []);
74
            $redis = 'OK';
75
        }
76
77
        return $redis;
78
    }
79
}
80