Completed
Push — master ( 9b0b83...e388d4 )
by Pierre
03:51
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
10
final class Stat extends AbstractApi implements IApi
11
{
12
13
    const _SCRIPTS = 'scripts';
14
15
    /**
16
     * instanciate
17
     *
18
     * @param Container $container
19
     */
20 3
    public function __construct(Container $container)
21
    {
22 3
        parent::__construct($container);
23
    }
24
25
    /**
26
     * opcache
27
     *
28
     * @Role anonymous
29
     * @return Stat
30
     */
31 1
    final public function opcache(): Stat
32
    {
33 1
        $this->response
34 1
            ->setCode(Response::HTTP_SERVICE_UNAVAILABLE)
35 1
            ->setContent([
36 1
                Response::_ERROR => true,
37 1
                Response::_ERROR_MSG => 'Opcache disable'
38
            ]);
39 1
        $status = opcache_get_status();
40 1
        if (!empty($status)) {
41
            $path = dirname(dirname($this->request->getFilename()));
42
            $scripts = array_filter(
43
                $status[self::_SCRIPTS],
44
                function ($val) use ($path) {
45
                    return false !== strpos($val['full_path'], $path);
46
                }
47
            );
48
            $status[self::_SCRIPTS] = array_values($scripts);
49
            $bytes = array_reduce(
50
                $status[self::_SCRIPTS],
51
                function ($stack, $val) {
52
                    return $stack + (int) $val['memory_consumption'];
53
                }
54
            );
55
            $scriptCount = count($scripts);
56
            unset($scripts);
57
            $this->response
58
                ->setCode(Response::HTTP_OK)
59
                ->setContent(
60
                    [
61
                        'error' => false,
62
                        'datas' => [
63
                            'php_version' => phpversion(),
64
                            'nb_files' => $scriptCount,
65
                            'memory_used' => $bytes,
66
                            'status' => $status
67
                        ]
68
                    ]
69
                );
70
            unset($scriptCount, $scripts, $bytes);
71
        }
72 1
        unset($status);
73 1
        return $this;
74
    }
75
76
    /**
77
     * filecache
78
     *
79
     * @Role anonymous
80
     * @return Stat
81
     */
82 1
    final public function filecache(): Stat
83
    {
84 1
        $path = realpath($this->getCachePath());
85 1
        $files = glob($path . '/*');
86 1
        $this->response
87 1
            ->setCode(Response::HTTP_OK)
88 1
            ->setContent(
89
                [
90 1
                    'error' => false,
91
                    'datas' => [
92 1
                        'cache_path' => $path,
93 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

93
                        'cache_hits' => count(/** @scrutinizer ignore-type */ $files),
Loading history...
94 1
                        'cache_files' => $files,
95
                    ]
96
                ]
97
            );
98 1
        unset($path, $files);
99 1
        return $this;
100
    }
101
}
102