Files
ws_orderreference/src/Database/ReferenceInstaller.php
Isabelle a36587d312 fix(webservice): Namespaces and redundancies
redundant require statements, clean up install trait, standardize trait names, remove hard-coded php version require for composer.json
2026-03-02 13:22:25 +01:00

82 lines
2.2 KiB
PHP

<?php
/**
* 2026 De Websmid
*
* @author De Websmid <contact@De Websmid.com>
* @copyright 2026 De Websmid
* @license De Websmid
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0).
* It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0
*/
declare(strict_types=1);
namespace Websmid\WsOrderReference\Database;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception as DBALException;
class ReferenceInstaller
{
private Connection $connection;
private string $dbPrefix;
public function __construct(Connection $connection, string $dbPrefix)
{
$this->connection = $connection;
$this->dbPrefix = $dbPrefix;
}
public function createTables(): array
{
$errors = [];
$this->dropTables();
$sqlInstallFile = dirname(__DIR__) . '/Resources/data/install.sql';
$sqlContent = file_get_contents($sqlInstallFile);
$sqlContent = str_replace('PREFIX_', $this->dbPrefix, $sqlContent);
$sqlQueries = array_filter(array_map('trim', explode(';', $sqlContent)));
foreach ($sqlQueries as $query) {
if (empty($query)) {
continue;
}
try {
$this->connection->executeQuery($query);
} catch (DBALException $e) {
$errors[] = [
'key' => $e->getMessage(),
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
];
}
}
return $errors;
}
public function dropTables(): array
{
$errors = [];
$tableNames = [
'ws_orderreference',
];
foreach ($tableNames as $tableName) {
$sql = 'DROP TABLE IF EXISTS ' . $this->dbPrefix . $tableName;
try {
$this->connection->executeQuery($sql);
} catch (DBALException $e) {
$errors[] = [
'key' => $e->getMessage(),
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
];
}
}
return $errors;
}
}