Completed
Push — master ( ae06d6...7c119a )
by Pierre
14:28 queued 11:26
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.3544

Importance

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

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