Lines 80.95% 17 / 21
Methods 80.00% 4 / 5
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 checkMinimumPhpVersion 100.00% 1 / 1 100.00% 1 / 1 1
 checkMinimumUpdateVersion 100.00% 1 / 1 100.00% 1 / 1 1
 checkMaintenanceMode 100.00% 1 / 1 100.00% 1 / 1 1
 checkPreUpgrade 76.47% 13 / 17 0.00% 0 / 1 8.83
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}