Stat::filecache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 12
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 1
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controllers\Api\V1;
6
7
use Nymfonya\Component\Http\Response;
8
use Nymfonya\Component\Container;
9
use App\Component\Cache\Redis\Adapter;
10
use App\Reuse\Controllers\Cacheable;
11
12
final class Stat extends Cacheable
13
{
14
15
    const _SCRIPTS = 'scripts';
16
17
    /**
18
     * instanciate
19
     *
20
     * @param Container $container
21
     */
22 5
    public function __construct(Container $container)
23
    {
24 5
        parent::__construct($container);
25
    }
26
27
    /**
28
     * opcache
29
     *
30
     * @Role anonymous
31
     * @return Stat
32
     */
33 1
    final public function opcache(): Stat
34
    {
35 1
        $this->response
36 1
            ->setCode(Response::HTTP_SERVICE_UNAVAILABLE)
37 1
            ->setContent([
38 1
                Response::_ERROR => true,
39 1
                Response::_ERROR_MSG => 'Opcache disable'
40
            ]);
41 1
        $status = opcache_get_status();
42 1
        if (!empty($status)) {
43
            $path = dirname(dirname($this->request->getFilename()));
44
            $scripts = array_filter(
45
                $status[self::_SCRIPTS],
46
                function ($val) use ($path) {
47
                    return false !== strpos($val['full_path'], $path);
48
                }
49
            );
50
            $status[self::_SCRIPTS] = array_values($scripts);
51
            $bytes = array_reduce(
52
                $status[self::_SCRIPTS],
53
                function ($stack, $val) {
54
                    return $stack + (int) $val['memory_consumption'];
55
                }
56
            );
57
            $scriptCount = count($scripts);
58
            unset($scripts);
59
            $this->response
60
                ->setCode(Response::HTTP_OK)
61
                ->setContent(
62
                    [
63
                        'error' => false,
64
                        'datas' => [
65
                            'php_version' => phpversion(),
66
                            'nb_files' => $scriptCount,
67
                            'memory_used' => $bytes,
68
                            'status' => $status
69
                        ]
70
                    ]
71
                );
72
            unset($scriptCount, $scripts, $bytes);
73
        }
74 1
        unset($status);
75 1
        return $this;
76
    }
77
78
    /**
79
     * filecache
80
     *
81
     * @Role anonymous
82
     * @return Stat
83
     */
84 1
    final public function filecache(): Stat
85
    {
86 1
        $path = realpath($this->getFileCachePath());
87 1
        $files = glob($path . '/*');
88 1
        $this->response
89 1
            ->setCode(Response::HTTP_OK)
90 1
            ->setContent(
91
                [
92 1
                    'error' => false,
93
                    'datas' => [
94 1
                        'cache_path' => $path,
95 1
                        'cache_hits' => count($files),
0 ignored issues
show
Bug introduced by
It seems like $files can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
                        'cache_hits' => count(/** @scrutinizer ignore-type */ $files),
Loading history...
96 1
                        'cache_files' => $files,
97
                    ]
98
                ]
99
            );
100 1
        unset($path, $files);
101 1
        return $this;
102
    }
103
104
    /**
105
     * check redis service
106
     *
107
     * @return Test
108
     */
109 2
    final public function redis(): Stat
110
    {
111 2
        $redisService = $this->getContainer()->getService(Adapter::class);
112 2
        $client = $redisService->getClient();
113 2
        $error = $redisService->isError();
114 2
        $ping = '';
115 2
        $keys = [];
116 2
        if (false === $error) {
117 1
            $client->set('redis-entry-name', 'redis-entry-name-item');
118 1
            $client->lpush('redis-list', 'item0');
119 1
            $client->lpush('redis-list', 'item1');
120 1
            $client->lpush('redis-list', 'item2');
121 1
            $ping = $client->ping();
122 1
            $keys = $client->keys('*');
123
        }
124 2
        $resCodeError = $error
125 1
            ? Response::HTTP_INTERNAL_SERVER_ERROR
126 2
            : Response::HTTP_OK;
127 2
        $this->response->setCode($resCodeError)->setContent(
128
            [
129 2
                'error' => $error,
130 2
                'errorCode' => $redisService->getErrorCode(),
131 2
                'errorMessage' => $redisService->getErrorMessage(),
132
                'datas' => [
133 2
                    'keys' => $keys,
134 2
                    'ping' => $ping,
135
                ]
136
            ]
137
        );
138 2
        unset($redisService, $client);
139 2
        return $this;
140
    }
141
}
142