Passed
Push — master ( e4e152...8fde47 )
by Georgi
02:55
created

SystemEnvironmentOverview   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 93
c 1
b 0
f 0
dl 0
loc 177
rs 10
wmc 20

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addGroupResults() 0 16 4
A testDatabasePermissions() 0 38 3
A testRequiredExtensions() 0 16 5
A renderView() 0 21 1
A unitToInt() 0 5 1
B testSystemCompatibility() 0 35 6
1
<?php
2
3
namespace Epesi\Core\System;
4
5
use atk4\ui\View;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\Schema;
8
use Illuminate\Database\Schema\Blueprint;
9
10
class SystemEnvironmentOverview extends View
11
{
12
	protected $systemRequirements = [
13
			'memory_limit' => '32M',
14
			'upload_max_filesize' => '8M',
15
			'post_max_size' => '16M'
16
	];
17
			
18
	protected $extensionRequirements = [
19
			'extension_loaded' => [
20
					'curl' => [
21
							'name' => 'cURL library',
22
							'severity' => 1
23
					]
24
			],
25
			'class_exists' => [
26
					'ZipArchive' => [
27
							'name' => 'ZIPArchive library',
28
							'severity' => 2
29
					]
30
			],
31
			'function_exists' => [
32
					'imagecreatefromjpeg' => [
33
							'name' => 'PHP GD extension - image processing',
34
							'severity' => 2
35
					]
36
			],
37
			'ini_get' => [
38
					'allow_url_fopen' => [
39
							'name' => 'Remote file_get_contents()',
40
							'severity' => 2
41
					]
42
			]
43
	];
44
	
45
	public function renderView()
46
	{
47
		$this->addClass('ui grid');
48
		
49
		$columns = $this->add('Columns');
50
		
51
		$column = $columns->addColumn()->setStyle('min-width', '350px');
1 ignored issue
show
Bug introduced by
The method addColumn() does not exist on atk4\ui\View. It seems like you code against a sub-type of atk4\ui\View such as atk4\ui\Columns or atk4\ui\Grid or atk4\ui\Table. ( Ignorable by Annotation )

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

51
		$column = $columns->/** @scrutinizer ignore-call */ addColumn()->setStyle('min-width', '350px');
Loading history...
52
		
53
		$grid = $column->add(['View', 'class' => ['ui grid']]);
54
		
55
		$this->addGroupResults(__('System'), $this->testSystemCompatibility(), $grid);
56
		
57
		$this->addGroupResults(__('Extensions'), $this->testRequiredExtensions(), $grid);
58
		
59
		$column = $columns->addColumn()->setStyle('min-width', '350px');
60
		
61
		$grid = $column->add(['View', 'class' => ['ui grid']]);
62
		
63
		$this->addGroupResults(__('Database'), $this->testDatabasePermissions(), $grid);
64
65
		parent::renderView();
66
	}
67
	
68
	public function addGroupResults($group, $testResults = [], $container = null)
69
	{
70
		if (! $testResults) return;
71
		
72
		$container = $container?: $this;
73
		
74
		$container->add(['Header', $group]);
75
		
76
		$colorMap = ['green', 'yellow', 'red'];
77
		
78
		foreach ($testResults as $test) {
79
			$color = $colorMap[$test['severity']];
80
			
81
			$row = $container->add(['View', 'class' => ['row']]);
82
			$row->add(['View', $test['name'], 'class' => ['nine wide column']]);
83
			$row->add(['View', 'class' => ['six wide right aligned column']])->add(['Label', $test['result'], 'class' => ["$color horizontal"]]);;
84
		}
85
	}
86
	
87
	protected function testDatabasePermissions()
88
	{
89
		ob_start();
90
		
91
		@Schema::dropIfExists('test');
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition for dropIfExists(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

91
		/** @scrutinizer ignore-unhandled */ @Schema::dropIfExists('test');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
92
93
		@Schema::create('test', function (Blueprint $table) {
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition for create(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

93
		/** @scrutinizer ignore-unhandled */ @Schema::create('test', function (Blueprint $table) {

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
94
			$table->increments('id');
95
		});
96
			
97
		$create = Schema::hasTable('test');
98
				
99
		@Schema::table('test', function (Blueprint $table) {
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition for table(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

99
		/** @scrutinizer ignore-unhandled */ @Schema::table('test', function (Blueprint $table) {

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
100
			$table->addColumn('TEXT', 'field_name');
101
		});
102
		
103
		$alter = Schema::hasColumn('test', 'field_name');
104
		
105
		$insert = @DB::insert('INSERT INTO test (id) VALUES (1)');
106
		$update = @DB::update('UPDATE test SET field_name=1 WHERE id=1');
107
		$delete = @DB::delete('DELETE FROM test');
108
		@Schema::dropIfExists('test');
109
		
110
		$drop = ! Schema::hasTable('test');
111
		
112
		ob_end_clean();
113
		
114
		$result = compact('create', 'alter', 'insert', 'update', 'delete', 'drop');
115
		
116
		array_walk($result, function(& $testResult, $testName) {
117
			$testResult = [
118
					'name' => __(':permission permission', ['permission' => strtoupper($testName)]),
119
					'result' => $testResult? __('OK'): __('Failed'),
120
					'severity' => $testResult? 0: 2
121
			];
122
		});		
123
		
124
		return $result;
125
	}
126
	
127
	protected function unitToInt($string)
128
	{
129
		return (int) preg_replace_callback('/(\-?\d+)(.?)/', function ($m) {
130
			return $m[1] * pow(1024, strpos('BKMG', $m[2]));
131
		}, strtoupper($string));
132
	}
133
	
134
	protected function testSystemCompatibility()
135
	{
136
// 		\Composer\Semver\Semver::satisfies(PHP_VERSION, $requiredPhpVersion);
137
		
138
		$ret = [];
139
		
140
		if ($requiredPhpVersion = $this->app->packageInfo()['require']['php']?? null) {
141
			$ret[] = [
142
					'name' => __('PHP version required :version', ['version' => $requiredPhpVersion]),
143
					'result' => PHP_VERSION,
144
					'severity' => version_compare(PHP_VERSION, $requiredPhpVersion, '>=')? 0: 2
145
			];
146
		}
147
		
148
		foreach ($this->systemRequirements as $iniKey => $requiredSize) {
149
			$actualSize = ini_get($iniKey);
150
			$actualSizeBytes = $this->unitToInt($actualSize);
151
			$requiredSizeBytes = $this->unitToInt($requiredSize);
152
			
153
			$severity = 0;
154
			if ($actualSizeBytes < $requiredSizeBytes) {
155
				$severity = 2;
156
			}
157
			elseif ($actualSizeBytes == $requiredSizeBytes) {
158
				$severity = 1;
159
			}
160
			
161
			$ret[] = [
162
					'name' => ucwords(str_ireplace('_', ' ', $iniKey)) . ' > ' . $requiredSize,
163
					'result' => $actualSize,
164
					'severity' => $severity
165
			];
166
		}
167
		
168
		return $ret;
169
	}
170
	
171
	protected function testRequiredExtensions()
172
	{
173
		$ret = [];
174
		
175
		foreach ($this->extensionRequirements as $callback => $extensions) {
176
			foreach ($extensions as $extension => $test) {
177
				$result = $callback($extension);
178
				
179
				$ret[] = array_merge($test, [
180
						'result' => $result? __('OK'): __('Missing'),
181
						'severity' => $result? 0: $test['severity']
182
				]);
183
			}
184
		}
185
		
186
		return $ret;
187
	}
188
	
189
}
190