Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.95% covered (success)
80.95%
17 / 21
80.00% covered (success)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractSetup
80.95% covered (success)
80.95%
17 / 21
80.00% covered (success)
80.00%
4 / 5
13.00
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 checkMinimumPhpVersion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 checkMinimumUpdateVersion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 checkMaintenanceMode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 checkPreUpgrade
76.47% covered (warning)
76.47%
13 / 17
0.00% covered (danger)
0.00%
0 / 1
8.83
1<?php
2
3/**
4 * The abstract setup class for installation and updating phpMyFAQ.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL wasn't distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2023-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2023-04-04
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Setup;
21
22use phpMyFAQ\Configuration;
23use phpMyFAQ\Core\Exception;
24use phpMyFAQ\System;
25
26abstract class AbstractSetup
27{
28    public function __construct(
29        protected System $system,
30    ) {
31    }
32
33    /**
34     * Checks the minimum required PHP version, defined in System class.
35     * Returns true if it's okay.
36     */
37    public function checkMinimumPhpVersion(): bool
38    {
39        return version_compare(version1: PHP_VERSION, version2: System::VERSION_MINIMUM_PHP) > 0;
40    }
41
42    /**
43     * We only support updates from 3.2.0 and later.
44     */
45    public function checkMinimumUpdateVersion(string $version): bool
46    {
47        return version_compare(version1: $version, version2: '3.1.0', operator: '>');
48    }
49
50    /**
51     * Updates only possible if the maintenance mode is enabled.
52     */
53    public function checkMaintenanceMode(): bool
54    {
55        return (bool) Configuration::getConfigurationInstance()->get(item: 'main.maintenanceMode');
56    }
57
58    /**
59     * Checks for the minimum PHP requirement and if the database credentials file is readable.
60     * @throws Exception
61     */
62    public function checkPreUpgrade(string $databaseType): void
63    {
64        $database = null;
65        if (!$this->checkMinimumPhpVersion()) {
66            throw new Exception(sprintf('Sorry, but you need PHP %s or later!', System::VERSION_MINIMUM_PHP));
67        }
68
69        if (
70            !is_readable((string) PMF_ROOT_DIR . '/content/core/config/database.php')
71            && !is_readable((string) PMF_ROOT_DIR . '/config/database.php')
72        ) {
73            throw new Exception(
74                'Sorry, but the database configuration file is not readable. Please check the permissions.',
75            );
76        }
77
78        if ('' !== $databaseType) {
79            $databaseFound = false;
80            foreach (array_keys($this->system->getSupportedDatabases()) as $database) {
81                if ($database !== $databaseType) {
82                    continue;
83                }
84
85                $databaseFound = true;
86                break;
87            }
88
89            if (!$databaseFound) {
90                throw new Exception(sprintf('Sorry, but the database %s is not supported!', ucfirst($databaseType)));
91            }
92        }
93    }
94}