Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
69.39% covered (warning)
69.39%
102 / 147
59.18% covered (warning)
59.18%
58 / 98
3.31% covered (danger)
3.31%
9 / 272
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Lib
69.39% covered (warning)
69.39%
102 / 147
59.18% covered (warning)
59.18%
58 / 98
3.31% covered (danger)
3.31%
9 / 272
0.00% covered (danger)
0.00%
0 / 9
1958.83
0.00% covered (danger)
0.00%
0 / 1
 __construct
81.82% covered (warning)
81.82%
9 / 11
50.00% covered (danger)
50.00%
2 / 4
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
4.12
 loadEnvVar
72.73% covered (warning)
72.73%
8 / 11
71.43% covered (warning)
71.43%
5 / 7
11.11% covered (danger)
11.11%
1 / 9
0.00% covered (danger)
0.00%
0 / 1
22.56
 delTree
66.67% covered (warning)
66.67%
6 / 9
58.33% covered (warning)
58.33%
7 / 12
9.09% covered (danger)
9.09%
1 / 11
0.00% covered (danger)
0.00%
0 / 1
33.05
 testVersion
62.50% covered (warning)
62.50%
5 / 8
50.00% covered (danger)
50.00%
3 / 6
33.33% covered (danger)
33.33%
1 / 3
0.00% covered (danger)
0.00%
0 / 1
5.67
 testExtensions
58.33% covered (warning)
58.33%
7 / 12
58.33% covered (warning)
58.33%
7 / 12
6.25% covered (danger)
6.25%
1 / 16
0.00% covered (danger)
0.00%
0 / 1
25.60
 envReport
76.32% covered (warning)
76.32%
29 / 38
65.00% covered (warning)
65.00%
13 / 20
1.04% covered (danger)
1.04%
1 / 96
0.00% covered (danger)
0.00%
0 / 1
70.02
 resetCache
69.23% covered (warning)
69.23%
9 / 13
60.00% covered (warning)
60.00%
6 / 10
25.00% covered (danger)
25.00%
1 / 4
0.00% covered (danger)
0.00%
0 / 1
10.75
 testDirectory
56.25% covered (warning)
56.25%
18 / 32
56.52% covered (warning)
56.52%
13 / 23
0.78% covered (danger)
0.78%
1 / 129
0.00% covered (danger)
0.00%
0 / 1
107.69
 testDB
84.62% covered (warning)
84.62%
11 / 13
50.00% covered (danger)
50.00%
2 / 4
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
 execute
n/a
0 / 0
n/a
0 / 0
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3namespace App\Service\ApplicationInfo;
4
5use App\ExceptionApplication;
6use App\Service\ApplicationInfo\Output\InterfaceOutput;
7use Doctrine\DBAL\DriverManager;
8use Doctrine\DBAL\Tools\DsnParser;
9use Throwable;
10
11abstract class Lib
12{
13    public InterfaceOutput $output;
14    public string $rootDirectoy;
15    public string $logDirectory;
16    public string $tmpDirectory;
17    public string $tmpFileTest;
18    /** @var string[] */
19    public array $env;
20
21    public function __construct(InterfaceOutput $output)
22    {
23        $this->output = $output;
24        try {
25            $this->rootDirectoy = realpath(dirname(__DIR__) . '/../..') ?: '';
26            $this->loadEnvVar();
27            $this->logDirectory = $this->env['APP_LOG_DIR'] ?? $this->rootDirectoy . '/var/log';
28            $this->tmpDirectory = $this->env['APP_CACHE_DIR'] ?? $this->rootDirectoy . '/var/cache';
29            $this->tmpFileTest = $this->tmpDirectory . '/tempFileTest.tmp';
30            $this->output->top();
31            $this->execute();
32        } catch (Throwable $e) {
33            $this->output->erreur($e->getMessage());
34        }
35        $this->output->bottom();
36    }
37
38    protected function loadEnvVar(): void
39    {
40        $env = [];
41        if (file_exists($this->rootDirectoy . '/.env.local')) {
42            /** @var string[]|false $parse */
43            $parse = parse_ini_file($this->rootDirectoy . '/.env.local');
44            if (false !== $parse) {
45                $env += $parse;
46            }
47        }
48        if (file_exists($this->rootDirectoy . '/.env')) {
49            /** @var string[]|false $parse */
50            $parse = parse_ini_file($this->rootDirectoy . '/.env');
51            if (false !== $parse) {
52                $env += $parse;
53            }
54        }
55
56        /** @var string[] $_ENV */
57        $env += $_ENV;
58        $this->env = $env;
59    }
60
61    protected function delTree(string $dir, bool $root = true): void
62    {
63        if (is_dir($dir)) {
64            $dirList = scandir($dir);
65            if ($dirList) {
66                $files = array_diff($dirList, ['.', '..']);
67                foreach ($files as $file) {
68                    (is_dir("$dir/$file")) ?
69                        $this->delTree("$dir/$file", true) : unlink("$dir/$file");
70                }
71            }
72            if ($root) {
73                @rmdir($dir);
74            }
75        }
76    }
77
78    public function testVersion(): void
79    {
80        $this->output->title('Test de la version');
81        try {
82            $propertiesContent = parse_ini_file($this->rootDirectoy . '/release.properties');
83            if (!isset($propertiesContent['version'])) {
84                throw new ExceptionApplication('version manquante dans le fichier release.properties');
85            }
86            /** @var string $version */
87            $version = $propertiesContent['version'];
88
89            $this->output->line('Version : ' . $this->output->ok($this->output->filter($version)));
90        } catch (Throwable $e) {
91            $this->output->erreur($this->output->filter($e->getMessage()));
92        }
93    }
94
95    /**
96     * @param string[] $requiredextensions
97     */
98    public function testExtensions(array $requiredextensions): void
99    {
100        $this->output->title('Test les extensions du PHP');
101
102        $loadedextensions = get_loaded_extensions();
103
104        $diff = [];
105        foreach ($requiredextensions as $extension) {
106            if (!in_array($extension, $loadedextensions)) {
107                $diff[] = $extension;
108            }
109        }
110        if ([] !== $diff) {
111            sort($requiredextensions);
112            $this->output->line($this->output->ko('Il manque une ou plusieurs extensions:'));
113            foreach ($diff as $module) {
114                $this->output->line($this->output->ko($this->output->filter($module)));
115            }
116        } else {
117            $this->output->line($this->output->ok("Toutes les extensions à l'application du php sont installées"));
118        }
119    }
120
121    public function envReport(): void
122    {
123        $this->output->title('Informations');
124        $phpcurrentver = substr(phpversion(), 0, 3);
125
126        if ($phpcurrentver < '8.3') {
127            $info = $this->output->ko($this->output->filter($phpcurrentver));
128        } else {
129            $info = $this->output->ok($this->output->filter($phpcurrentver));
130        }
131        $this->output->line('Version du PHP : ' . $info);
132
133        $this->output->line('Valeur par defaut du umask : ' . $this->output->filter(decoct(umask())));
134        umask(0);   // NOSONAR
135        $this->output->line('Valeur du umask si mis à 0 : ' . $this->output->filter(decoct(umask())));
136
137        $memSize = ini_get('memory_limit');
138        $this->output->line('Taille maximale de la mémoire par défaut : ' . $this->output->filter($memSize));
139
140        ini_set('memory_limit', '512M');
141        $memNewSize = ini_get('memory_limit');
142        $this->output->line('Modification de la taille maximale de la mémoire à 512M');
143        if ('512M' == $memNewSize) {
144            $info = $this->output->ok($this->output->filter($memNewSize));
145        } else {
146            $info = $this->output->ko($this->output->filter($memNewSize));
147        }
148        $this->output->line('Nouvelle taille maximale de la mémoire : ' . $info);
149        $this->output->line('Modification de la taille maximale de la mémoire à 1024M');
150        if ('1024M' == $memNewSize) {
151            $info = $this->output->ok($this->output->filter($memNewSize));
152        } else {
153            $info = $this->output->ko($this->output->filter($memNewSize));
154        }
155        $this->output->line('Nouvelle taille maximale de la mémoire : ' . $info);
156
157        $executionTime = ini_get('max_execution_time');
158        $this->output->line('Valeur par défaut de max_execution_time : ' . $this->output->filter($executionTime));
159        if (0 == $executionTime) {
160            $this->output->line($this->output->ko('!!! infini timeout !!! ( peut etre en mode xdebug)'));
161        } else {
162            $this->output->line('Modification max_execution_time à 300 sec : ');
163            $result = set_time_limit(300);
164            if (!$result) {
165                $this->output->line($this->output->ko("Il n'est pas possible de changer max_execution_time à 300 sec"));
166            }
167            $this->output->line('Nouvelle valeur de max_execution_time : ' . $this->output->filter($executionTime));
168        }
169
170        if ('' !== date_default_timezone_get() && '0' !== date_default_timezone_get()) {
171            $timezone = $this->output->filter(date_default_timezone_get());
172        } else {
173            $timezone = $this->output->ko($this->output->filter('non définit'));
174        }
175
176        $this->output->line('Time zone : ' . $timezone);
177        $this->output->line('Date locale ' . date('d.m.Y H:i:s') . ' décalage horaire : ' . date('P  e'));
178        $this->output->line('Date GMT : ' . gmdate('d.m.Y H:i:s'));
179    }
180
181    public function resetCache(): void
182    {
183        $this->output->title('Reset des caches');
184
185        $this->output->line('Reset de opcache');
186        if (function_exists('opcache_reset')) {
187            // reset du cache php
188            $status = opcache_reset();
189            if ($status) {
190                $this->output->line($this->output->ok('Opcache effacé'));
191            } else {
192                $this->output->erreur('Reset de opcache : ' . $this->output->ko('KO'));
193            }
194        } else {
195            $this->output->erreur("Le cache opcache n'est pas installé");
196        }
197        $this->output->line('Effacement du répertoire temporaire');
198        try {
199            $this->delTree(rtrim($this->tmpDirectory, '/'), false);
200            $this->output->line($this->output->ok('Répertoire temporaire effacé'));
201        } catch (Throwable $e) {
202            $this->output->erreur($this->output->filter($e->getMessage()));
203        }
204    }
205
206    public function testDirectory(): void
207    {
208        $this->output->title('Test écriture du répertoire temporaire de cache');
209        $this->output->line('Test la présence du répertoire temporaire de cache');
210        try {
211            if (!realpath($this->tmpDirectory)) {
212                $this->output->erreur("La configuration du répertoire temporaire de cache n'est pas définie");
213            }
214            if (!is_dir($this->tmpDirectory)) {
215                $this->output->line('Création du du répertoire temporaire');
216                mkdir($this->tmpDirectory, 0777, true);
217            }
218            if (is_file($this->tmpFileTest)) {
219                unlink($this->tmpFileTest);
220            }
221            if (file_put_contents($this->tmpFileTest, 'TESTCONTENT')) {
222                $this->output->line('Ecriture de fichier : ' . $this->output->ok('OK'));
223                $filecontent = file_get_contents($this->tmpFileTest);
224                if ('TESTCONTENT' == $filecontent) {
225                    $this->output->line('Lecture de fichier : ' . $this->output->ok('OK'));
226                    unlink($this->tmpFileTest);
227                    if (!is_file($this->tmpFileTest)) {
228                        $this->output->line('Suppression de fichier : ' . $this->output->ok('OK'));
229                        $this->output->line('Répertoire temporaire de cache : ' . $this->output->ok('OK'));
230                    } else {
231                        $this->output->erreur(
232                            'Impossible de supprimer un fichier dans le répertoire temporaire'
233                        );
234                    }
235                } else {
236                    $this->output->erreur('Impossible de lire un fichier dans le répertoire temporaire');
237                }
238            } else {
239                $this->output->erreur("Impossible d'écrire un fichier dans le répertoire temporaire");
240            }
241
242            $this->output->line('Test la présence du répertoire des fichiers de log');
243            if (!realpath($this->logDirectory)) {
244                $this->output->erreur("La configuration du répertoire des fichiers de log n'est pas définie");
245            }
246
247            if (!is_dir($this->logDirectory)) {
248                $this->output->line('Création du du répertoire des fichiers de log');
249                mkdir($this->logDirectory, 0777, true);
250            }
251            $this->output->line('Répertoire des fichiers de log : ' . $this->output->ok('OK'));
252        } catch (Throwable $e) {
253            $this->output->erreur($this->output->filter($e->getMessage()));
254        }
255    }
256
257    public function testDB(string $dsn, string $sql, string $dbname = ''): void
258    {
259        $this->output->title('Test de la DB ' . $this->output->filter($dbname));
260        try {
261            $this->output->line('Test de la connexion avec la DB');
262            $dsn = str_replace('%kernel.project_dir%', $this->rootDirectoy, $dsn);
263            $dsnParser = new DsnParser();
264            $connectionParams = $dsnParser->parse($dsn);
265            $conn = DriverManager::getConnection($connectionParams);
266            $this->output->line('Connexion : ' . $this->output->ok('OK'));
267            $stmt = $conn->executeQuery($sql);
268            $this->output->line('Requete SQL ' . $this->output->ok('OK'));
269            $stmt->fetchAssociative();
270            $this->output->line('Lecture table ' . $this->output->ok('OK'));
271        } catch (Throwable $e) {
272            $this->output->erreur($this->output->filter($e->getMessage()));
273        }
274    }
275
276    abstract public function execute(): void;
277}