Completed
Push — master ( 88d4e7...879ddc )
by Pierre
17:36 queued 14:17
created

Stat   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 54.76%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 82
ccs 23
cts 42
cp 0.5476
rs 10
c 2
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A filecache() 0 18 1
A opcache() 0 37 2
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\Component\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 3
    public function __construct(Container $container)
19
    {
20 3
        parent::__construct($container);
21
    }
22
23
    /**
24
     * opcache
25
     *
26
     * @Role anonymous
27
     * @return Stat
28
     */
29 1
    final public function opcache(): Stat
30
    {
31 1
        $this->response
32 1
            ->setCode(Response::HTTP_SERVICE_UNAVAILABLE)
33 1
            ->setContent([
34 1
                Response::_ERROR => true,
35
                Response::_ERROR_MSG => 'Opcache disable'
36
            ]);
37 1
        $status = opcache_get_status();
38 1
        if (!empty($status)) {
39
            $path = dirname(dirname($this->request->getFilename()));
40
            $scripts = array_filter($status['scripts'], function ($val) use ($path) {
41
                return strpos($val['full_path'], $path) !== false;
42
            });
43
            $status['scripts'] = array_values($scripts);
44
            $bytes = array_reduce($status['scripts'], function ($stack, $val) {
45
                return $stack + $val['memory_consumption'];
46
            });
47
            $scriptCount = count($scripts);
48
            unset($scripts);
49
            $this->response
50
                ->setCode(Response::HTTP_OK)
51
                ->setContent(
52
                    [
53
                        'error' => false,
54
                        'datas' => [
55
                            'php_version' => phpversion(),
56
                            'nb_files' => $scriptCount,
57
                            'memory_used' => $bytes,
58
                            'status' => $status
59
                        ]
60
                    ]
61
                );
62
            unset($scriptCount, $scripts, $bytes);
63
        }
64 1
        unset($status);
65 1
        return $this;
66
    }
67
68
    /**
69
     * filecache
70
     *
71
     * @Role anonymous
72
     * @return Stat
73
     */
74 1
    final public function filecache(): Stat
75
    {
76 1
        $path = realpath($this->getCachePath());
77 1
        $files = glob($path . '/*');
78 1
        $this->response
79 1
            ->setCode(Response::HTTP_OK)
80 1
            ->setContent(
81
                [
82 1
                    'error' => false,
83
                    'datas' => [
84 1
                        'cache_path' => $path,
85 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

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