Completed
Push — master ( 5c6447...c86f07 )
by Pierre
02:59
created

Stat::opcache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.2433

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 31
c 2
b 0
f 1
nc 2
nop 0
dl 0
loc 43
ccs 10
cts 31
cp 0.3226
crap 3.2433
rs 9.424
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\Interfaces\Controllers\IApi;
10
use App\Reuse\Controllers\AbstractApi;
11
use App\Component\Cache\Redis\Adapter;
12
13
final class Stat extends AbstractApi implements IApi
14
{
15
16
    const _SCRIPTS = 'scripts';
17
18
    /**
19
     * instanciate
20
     *
21
     * @param Container $container
22
     */
23 5
    public function __construct(Container $container)
24
    {
25 5
        parent::__construct($container);
26
    }
27
28
    /**
29
     * opcache
30
     *
31
     * @Role anonymous
32
     * @return Stat
33
     */
34 1
    final public function opcache(): Stat
35
    {
36 1
        $this->response
37 1
            ->setCode(Response::HTTP_SERVICE_UNAVAILABLE)
38 1
            ->setContent([
39 1
                Response::_ERROR => true,
40 1
                Response::_ERROR_MSG => 'Opcache disable'
41
            ]);
42 1
        $status = opcache_get_status();
43 1
        if (!empty($status)) {
44
            $path = dirname(dirname($this->request->getFilename()));
45
            $scripts = array_filter(
46
                $status[self::_SCRIPTS],
47
                function ($val) use ($path) {
48
                    return false !== strpos($val['full_path'], $path);
49
                }
50
            );
51
            $status[self::_SCRIPTS] = array_values($scripts);
52
            $bytes = array_reduce(
53
                $status[self::_SCRIPTS],
54
                function ($stack, $val) {
55
                    return $stack + (int) $val['memory_consumption'];
56
                }
57
            );
58
            $scriptCount = count($scripts);
59
            unset($scripts);
60
            $this->response
61
                ->setCode(Response::HTTP_OK)
62
                ->setContent(
63
                    [
64
                        'error' => false,
65
                        'datas' => [
66
                            'php_version' => phpversion(),
67
                            'nb_files' => $scriptCount,
68
                            'memory_used' => $bytes,
69
                            'status' => $status
70
                        ]
71
                    ]
72
                );
73
            unset($scriptCount, $scripts, $bytes);
74
        }
75 1
        unset($status);
76 1
        return $this;
77
    }
78
79
    /**
80
     * filecache
81
     *
82
     * @Role anonymous
83
     * @return Stat
84
     */
85 1
    final public function filecache(): Stat
86
    {
87 1
        $path = realpath($this->getCachePath());
88 1
        $files = glob($path . '/*');
89 1
        $this->response
90 1
            ->setCode(Response::HTTP_OK)
91 1
            ->setContent(
92
                [
93 1
                    'error' => false,
94
                    'datas' => [
95 1
                        'cache_path' => $path,
96 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

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