ToolsController::information()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 24
rs 9.6666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype\Plugin\Admin\Controllers;
6
7
use FilesystemIterator;
8
use Flextype\Component\Number\Number;
9
use Flextype\Component\Filesystem\Filesystem;
10
use Psr\Http\Message\ResponseInterface as Response;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Http\Message\ServerRequestInterface as Request;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use RecursiveDirectoryIterator;
13
use RecursiveIteratorIterator;
14
use function array_merge;
15
use function file_exists;
16
use function Flextype\Component\I18n\__;
0 ignored issues
show
introduced by
The function Flextype\Component\I18n\__ was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
17
use function getenv;
18
use function is_array;
19
use function php_sapi_name;
20
use function php_uname;
21
use function realpath;
22
23
class ToolsController
24
{
25
    /**
26
     * __construct
27
     */
28
     public function __construct()
29
     {
30
31
     }
32
33
    /**
34
     * Index page
35
     *
36
     * @param Request  $request  PSR7 request
37
     * @param Response $response PSR7 response
38
     */
39
    public function index(Request $request, Response $response) : Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

39
    public function index(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        return $response->withRedirect(flextype('router')->pathFor('admin.tools.information'));
0 ignored issues
show
Bug introduced by
The function flextype was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        return $response->withRedirect(/** @scrutinizer ignore-call */ flextype('router')->pathFor('admin.tools.information'));
Loading history...
42
    }
43
44
    /**
45
     * Information page
46
     *
47
     * @param Request  $request  PSR7 request
48
     * @param Response $response PSR7 response
49
     */
50
    public function information(Request $request, Response $response) : Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

50
    public function information(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
    {
52
        return flextype('twig')->render(
0 ignored issues
show
Bug introduced by
The function flextype was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
53
            $response,
54
            'plugins/admin/templates/system/tools/information.html',
55
            [
56
                'menu_item' => 'tools',
57
                'php_uname' => php_uname(),
58
                'webserver' => $_SERVER['SERVER_SOFTWARE'] ?? @getenv('SERVER_SOFTWARE'),
59
                'php_sapi_name' => php_sapi_name(),
60
                'links' =>  [
61
                    'information' => [
62
                        'link' => flextype('router')->pathFor('admin.tools.index'),
63
                        'title' => __('admin_information'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
                        'title' => /** @scrutinizer ignore-call */ __('admin_information'),
Loading history...
64
                        'active' => true
65
                    ],
66
                    'cache' => [
67
                        'link' => flextype('router')->pathFor('admin.tools.cache'),
68
                        'title' => __('admin_cache'),
69
70
                    ],
71
                    'registry' => [
72
                        'link' => flextype('router')->pathFor('admin.tools.registry'),
73
                        'title' => __('admin_registry'),
74
75
                    ],
76
                ],
77
            ]
78
        );
79
    }
80
81
    /**
82
     * Cache page
83
     *
84
     * @param Request  $request  PSR7 request
85
     * @param Response $response PSR7 response
86
     */
87
    public function cache(Request $request, Response $response) : Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
    public function cache(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
        return flextype('twig')->render(
0 ignored issues
show
Bug introduced by
The function flextype was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
90
            $response,
91
            'plugins/admin/templates/system/tools/cache.html',
92
            [
93
                'menu_item' => 'tools',
94
                'data_size' => Number::byteFormat($this->getDirectorySize(PATH['tmp'] . '/data')),
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\Admin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
95
                'glide_size' => Number::byteFormat($this->getDirectorySize(PATH['tmp'] . '/glide')),
96
                'twig_size' => Number::byteFormat($this->getDirectorySize(PATH['tmp'] . '/twig')),
97
                'preflight_size' => Number::byteFormat($this->getDirectorySize(PATH['tmp'] . '/preflight')),
98
                'links' =>  [
99
                    'information' => [
100
                        'link' => flextype('router')->pathFor('admin.tools.index'),
101
                        'title' => __('admin_information'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
                        'title' => /** @scrutinizer ignore-call */ __('admin_information'),
Loading history...
102
103
                    ],
104
                    'cache' => [
105
                        'link' => flextype('router')->pathFor('admin.tools.cache'),
106
                        'title' => __('admin_cache'),
107
                        'active' => true
108
                    ],
109
                    'registry' => [
110
                        'link' => flextype('router')->pathFor('admin.tools.registry'),
111
                        'title' => __('admin_registry'),
112
113
                    ],
114
                ],
115
                'buttons' => [
116
                    'tools_clear_cache' => [
117
                        'type' => 'action',
118
                        'id' => 'clear-cache-all',
119
                        'link' => flextype('router')->pathFor('admin.tools.clearCacheAllProcess'),
120
                        'title' => __('admin_clear_cache_all'),
121
                    ],
122
                ],
123
            ]
124
        );
125
    }
126
127
    /**
128
     * Information page
129
     *
130
     * @param Request  $request  PSR7 request
131
     * @param Response $response PSR7 response
132
     */
133
    public function registry(Request $request, Response $response) : Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

133
    public function registry(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
134
    {
135
        return flextype('twig')->render(
0 ignored issues
show
Bug introduced by
The function flextype was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
        return /** @scrutinizer ignore-call */ flextype('twig')->render(
Loading history...
136
            $response,
137
            'plugins/admin/templates/system/tools/registry.html',
138
            [
139
                'menu_item' => 'tools',
140
                'registry_dump' => $this->dotArray(flextype('registry')->all()),
141
                'links' =>  [
142
                    'information' => [
143
                        'link' => flextype('router')->pathFor('admin.tools.index'),
144
                        'title' => __('admin_information'),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
                        'title' => /** @scrutinizer ignore-call */ __('admin_information'),
Loading history...
145
146
                    ],
147
                    'cache' => [
148
                        'link' => flextype('router')->pathFor('admin.tools.cache'),
149
                        'title' => __('admin_cache'),
150
151
                    ],
152
                    'registry' => [
153
                        'link' => flextype('router')->pathFor('admin.tools.registry'),
154
                        'title' => __('admin_registry'),
155
                        'active' => true
156
                    ],
157
                ],
158
            ]
159
        );
160
    }
161
162
    /**
163
     * Clear cache process
164
     *
165
     * @param Request  $request  PSR7 request
166
     * @param Response $response PSR7 response
167
     */
168
    public function clearCacheProcess(Request $request, Response $response) : Response
169
    {
170
        $id = $request->getParsedBody()['cache-id'];
171
172
        if ($id == 'data') {
173
            Filesystem::deleteDir(PATH['tmp'] . '/data');
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\Admin\Controllers\PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
174
        }
175
176
        if ($id == 'twig') {
177
            Filesystem::deleteDir(PATH['tmp'] . '/twig');
178
        }
179
180
        if ($id == 'glide') {
181
            Filesystem::deleteDir(PATH['tmp'] . '/glide');
182
        }
183
184
        if ($id == 'preflight') {
185
            Filesystem::deleteDir(PATH['tmp'] . '/preflight');
186
        }
187
188
        flextype('flash')->addMessage('success', __('admin_message_cache_files_deleted'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

188
        flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_cache_files_deleted'));
Loading history...
Bug introduced by
The function flextype was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

188
        /** @scrutinizer ignore-call */ 
189
        flextype('flash')->addMessage('success', __('admin_message_cache_files_deleted'));
Loading history...
189
190
        return $response->withRedirect(flextype('router')->pathFor('admin.tools.cache'));
191
    }
192
193
    /**
194
     * Clear all cache process
195
     *
196
     * @param Request  $request  PSR7 request
197
     * @param Response $response PSR7 response
198
     */
199
    public function clearCacheAllProcess(Request $request, Response $response) : Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

199
    public function clearCacheAllProcess(/** @scrutinizer ignore-unused */ Request $request, Response $response) : Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
200
    {
201
        Filesystem::deleteDir(ROOT_DIR . '/var');
0 ignored issues
show
Bug introduced by
The constant Flextype\Plugin\Admin\Controllers\ROOT_DIR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
202
203
        flextype('flash')->addMessage('success', __('admin_message_cache_files_deleted'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

203
        flextype('flash')->addMessage('success', /** @scrutinizer ignore-call */ __('admin_message_cache_files_deleted'));
Loading history...
Bug introduced by
The function flextype was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

203
        /** @scrutinizer ignore-call */ 
204
        flextype('flash')->addMessage('success', __('admin_message_cache_files_deleted'));
Loading history...
204
205
        return $response->withRedirect(flextype('router')->pathFor('admin.tools.cache'));
206
    }
207
208
    /**
209
     * _dotArray
210
     */
211
    private function dotArray($array, $prepend = '') : array
212
    {
213
        $results = [];
214
215
        foreach ($array as $key => $value) {
216
            if (is_array($value) && ! empty($value)) {
217
                $results = array_merge($results, $this->dotArray($value, $prepend . $key . '.'));
218
            } else {
219
                $results[$prepend . $key] = $value;
220
            }
221
        }
222
223
        return $results;
224
    }
225
226
    /**
227
     * _getDirectorySize
228
     */
229
    private function getDirectorySize($path)
230
    {
231
        $bytestotal = 0;
232
        $path       = realpath($path);
233
        if ($path!==false && $path!=='' && file_exists($path)) {
234
            foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)) as $object) {
235
                $bytestotal += $object->getSize();
236
            }
237
        }
238
239
        return $bytestotal;
240
    }
241
}
242