Files
sysPass/app/modules/api/Init.php
Rubén D d3c6448295 test(IT): Setup integration tests
Signed-off-by: Rubén D <nuxsmin@syspass.org>
2024-07-27 11:10:50 +02:00

132 lines
3.7 KiB
PHP

<?php
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Modules\Api;
use Klein\Klein;
use SP\Core\Application;
use SP\Core\Context\ContextException;
use SP\Core\HttpModuleBase;
use SP\Core\Language;
use SP\Core\ProvidersHelper;
use SP\Domain\Common\Providers\Http;
use SP\Domain\Core\Exceptions\InitializationException;
use SP\Domain\Core\Exceptions\SPException;
use SP\Domain\Core\LanguageInterface;
use SP\Domain\Http\Ports\RequestService;
use SP\Infrastructure\Database\DatabaseUtil;
use function SP\logger;
/**
* Class Init
*/
final class Init extends HttpModuleBase
{
private Language $language;
private DatabaseUtil $databaseUtil;
public function __construct(
Application $application,
ProvidersHelper $providersHelper,
RequestService $request,
Klein $router,
LanguageInterface $language,
DatabaseUtil $databaseUtil
) {
parent::__construct(
$application,
$providersHelper,
$request,
$router
);
$this->language = $language;
$this->databaseUtil = $databaseUtil;
}
/**
* @param string $controller
* @throws ContextException
* @throws InitializationException
* @throws SPException
*/
public function initialize(string $controller): void
{
logger(__FUNCTION__);
// Initialize context
$this->context->initialize();
// Load language
$this->language->setLanguage();
// Checks if it needs to switch the request over HTTPS
Http::checkHttps($this->configData, $this->request);
// Checks if sysPass is installed
$this->checkInstalled();
// Checks if maintenance mode is turned on
if ($this->checkMaintenanceMode()) {
throw new InitializationException('Maintenance mode');
}
// Checks if upgrade is needed
if ($this->checkUpgradeNeeded()) {
throw new InitializationException('Upgrade needed');
}
// Checks if the database is set up
if (!$this->databaseUtil->checkDatabaseConnection()) {
throw new InitializationException('Database connection error');
}
if (!$this->databaseUtil->checkDatabaseTables($this->configData->getDbName())) {
throw new InitializationException('Database checking error');
}
// Initialize event handlers
$this->initEventHandlers();
}
/**
* Comprueba que la aplicación esté instalada
* Esta función comprueba si la aplicación está instalada. Si no lo está, redirige al instalador.
*
* @throws InitializationException
*/
private function checkInstalled(): void
{
if (!$this->configData->isInstalled()) {
throw new InitializationException('Not installed');
}
}
public function getName(): string
{
return 'api';
}
}