Completed
Push — master ( 579110...b52e58 )
by Pierre
02:10
created

Stat::opcache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 25
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 35
rs 9.52
1
<?php
2
3
namespace App\Controllers\Api\V1;
4
5
use App\Interfaces\Controllers\IApi;
6
use App\Reuse\Controllers\AbstractApi;
7
use App\Http\Response;
8
use App\Container;
9
10
final class Stat extends AbstractApi implements IApi
11
{
12
13
    /**
14
     * instanciate
15
     *
16
     * @param Container $container
17
     */
18
    public function __construct(Container $container)
19
    {
20
        parent::__construct($container);
21
    }
22
23
    /**
24
     * opcache
25
     *
26
     * @Role anonymous
27
     * @return Stat
28
     */
29
    final public function opcache(): Stat
30
    {
31
        $status = opcache_get_status();
32
        $this->response->setCode(Response::HTTP_OK)->setContent([
33
            Response::_ERROR => true,
34
            Response::_ERROR_MSG => 'Opcache disable'
35
        ]);
36
        if ($status) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $status of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
37
            $path = dirname(dirname($this->request->getFilename()));
38
            $scripts = array_filter($status['scripts'], function ($val) use ($path) {
39
                return strpos($val['full_path'], $path) !== false;
40
            });
41
            $status['scripts'] = array_values($scripts);
42
            $bytes = array_reduce($status['scripts'], function ($stack, $val) {
43
                return $stack + $val['memory_consumption'];
44
            });
45
            $scriptCount = count($scripts);
46
            unset($scripts);
47
            $this->response
48
                ->setCode(Response::HTTP_OK)
49
                ->setContent(
50
                    [
51
                        'error' => false,
52
                        'datas' => [
53
                            'php_version' => phpversion(),
54
                            'nb_files' => $scriptCount,
55
                            'memory_used' => $bytes,
56
                            'status' => $status
57
                        ]
58
                    ]
59
                );
60
            unset($scriptCount, $scripts, $bytes);
61
        }
62
        unset($status);
63
        return $this;
64
    }
65
66
    /**
67
     * filecache
68
     *
69
     * @Role anonymous
70
     * @return Stat
71
     */
72
    final public function filecache(): Stat
73
    {
74
        $path = realpath($this->getCachePath());
75
        $files = glob($path . '/*');
76
        $this->response
77
            ->setCode(Response::HTTP_OK)
78
            ->setContent(
79
                [
80
                    'error' => false,
81
                    'datas' => [
82
                        'cache_path' => $path,
83
                        '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

83
                        'cache_hits' => count(/** @scrutinizer ignore-type */ $files),
Loading history...
84
                        'cache_files' => $files,
85
                    ]
86
                ]
87
            );
88
        unset($path, $files);
89
        return $this;
90
    }
91
}
92