CheckRequirementsCommand::checkPHPExtensions()   F
last analyzed

Complexity

Conditions 20
Paths > 20000

Size

Total Lines 59
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 40
nc 36288
nop 2
dl 0
loc 59
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 *  Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 *  This program is free software: you can redistribute it and/or modify
8
 *  it under the terms of the GNU Affero General Public License as published
9
 *  by the Free Software Foundation, either version 3 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU Affero General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU Affero General Public License
18
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Command;
22
23
use Symfony\Component\Console\Command\Command;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console\Output\OutputInterface;
27
use Symfony\Component\Console\Style\SymfonyStyle;
28
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
29
30
class CheckRequirementsCommand extends Command
31
{
32
    protected static $defaultName = 'partdb:check-requirements';
33
34
    protected ContainerBagInterface $params;
35
36
    public function __construct(ContainerBagInterface $params)
37
    {
38
        $this->params = $params;
39
        parent::__construct();
40
    }
41
42
    protected function configure(): void
43
    {
44
        $this
45
            ->setDescription('Checks if the requirements Part-DB needs or recommends are fulfilled.')
46
            ->addOption('only_issues', 'i', InputOption::VALUE_NONE, 'Only show issues, not success messages.')
47
        ;
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output): int
51
    {
52
        $io = new SymfonyStyle($input, $output);
53
54
        $only_issues = (bool) $input->getOption('only_issues');
55
56
        $io->title('Checking PHP configuration...');
57
        $this->checkPHP($io, $only_issues);
58
59
        $io->title('Checking PHP extensions...');
60
        $this->checkPHPExtensions($io, $only_issues);
61
62
        $io->title('Checking Part-DB configuration');
63
        $this->checkPartDBConfig($io, $only_issues);
64
65
        return self::SUCCESS;
66
67
    }
68
69
    protected function checkPHP(SymfonyStyle $io, $only_issues = false): void
70
    {
71
        //Check PHP versions
72
        $io->isVerbose() && $io->comment('Checking PHP version...');
73
        if (PHP_VERSION_ID < 80100) {
74
            $io->warning('You are using PHP '. PHP_VERSION .'. This will work, but a newer version is recommended.');
75
        } else {
76
            !$only_issues && $io->success('PHP version is sufficient.');
77
        }
78
79
        //Check if opcache is enabled
80
        $io->isVerbose() && $io->comment('Checking Opcache...');
81
        $opcache_enabled = ini_get('opcache.enable') === '1';
82
        if (!$opcache_enabled) {
83
            $io->warning('Opcache is not enabled. This will work, but performance will be better with opcache enabled. Set opcache.enable=1 in your php.ini to enable it');
84
        } else {
85
            !$only_issues && $io->success('Opcache is enabled.');
86
        }
87
88
        //Check if opcache is configured correctly
89
        $io->isVerbose() && $io->comment('Checking Opcache configuration...');
90
        if ($opcache_enabled && (ini_get('opcache.memory_consumption') < 256 || ini_get('opcache.max_accelerated_files') < 20000)) {
91
            $io->warning('Opcache configuration can be improved. See https://symfony.com/doc/current/performance.html for more info.');
92
        } else {
93
            !$only_issues && $io->success('Opcache configuration is already performance optimized.');
94
        }
95
    }
96
97
    protected function checkPartDBConfig(SymfonyStyle $io, $only_issues = false): void
98
    {
99
        //Check if APP_ENV is set to prod
100
        $io->isVerbose() && $io->comment('Checking debug mode...');
101
        if($this->params->get('kernel.debug')) {
102
            $io->warning('You have activated debug mode, this is will leak informations in a production environment.');
103
        } else {
104
            !$only_issues && $io->success('Debug mode disabled.');
105
        }
106
107
    }
108
109
    protected function checkPHPExtensions(SymfonyStyle $io, $only_issues = false): void
110
    {
111
        //Get all installed PHP extensions
112
        $extensions = get_loaded_extensions();
113
        $io->isVerbose() && $io->comment('Your PHP installation has '. count($extensions) .' extensions installed: '. implode(', ', $extensions));
114
115
        $db_drivers_count = 0;
116
        if(!in_array('pdo_mysql', $extensions)) {
117
            $io->error('pdo_mysql is not installed. You will not be able to use MySQL databases.');
118
        } else {
119
            !$only_issues && $io->success('PHP extension pdo_mysql is installed.');
120
            $db_drivers_count++;
121
        }
122
123
        if(!in_array('pdo_sqlite', $extensions)) {
124
            $io->error('pdo_sqlite is not installed. You will not be able to use SQLite. databases');
125
        } else {
126
            !$only_issues && $io->success('PHP extension pdo_sqlite is installed.');
127
            $db_drivers_count++;
128
        }
129
130
        $io->isVerbose() && $io->comment('You have '. $db_drivers_count .' database drivers installed.');
131
        if ($db_drivers_count === 0) {
132
            $io->error('You have no database drivers installed. You have to install at least one database driver!');
133
        }
134
135
        if(!in_array('curl', $extensions)) {
136
            $io->warning('curl extension is not installed. Install curl extension for better performance');
137
        } else {
138
            !$only_issues && $io->success('PHP extension curl is installed.');
139
        }
140
141
        $gd_installed = in_array('gd', $extensions);
142
        if(!$gd_installed) {
143
            $io->error('GD is not installed. GD is required for image processing.');
144
        } else {
145
            !$only_issues && $io->success('PHP extension GD is installed.');
146
        }
147
148
        //Check if GD has jpeg support
149
        $io->isVerbose() && $io->comment('Checking if GD has jpeg support...');
150
        if ($gd_installed) {
151
            $gd_info = gd_info();
152
            if($gd_info['JPEG Support'] === false) {
153
                $io->warning('Your GD does not have jpeg support. You will not be able to generate thumbnails of jpeg images.');
154
            } else {
155
                !$only_issues && $io->success('GD has jpeg support.');
156
            }
157
158
            if($gd_info['PNG Support'] === false) {
159
                $io->warning('Your GD does not have png support. You will not be able to generate thumbnails of png images.');
160
            } else {
161
                !$only_issues && $io->success('GD has png support.');
162
            }
163
164
            if($gd_info['WebP Support'] === false) {
165
                $io->warning('Your GD does not have WebP support. You will not be able to generate thumbnails of WebP images.');
166
            } else {
167
                !$only_issues && $io->success('GD has WebP support.');
168
            }
169
        }
170
171
172
173
174
    }
175
176
}