Files
emails/api/common/Service/Install.php
2022-11-25 20:52:52 -03:00

73 lines
2.2 KiB
PHP

<?php
namespace ProVM\Common\Service;
use Psr\Log\LoggerInterface;
use PDO;
class Install
{
public function __construct(LoggerInterface $logger, PDO $pdo)
{
$this->setLogger($logger);
$this->setConnection($pdo);
}
protected LoggerInterface $logger;
protected PDO $connection;
public function getLogger(): LoggerInterface
{
return $this->logger;
}
public function getConnection(): PDO
{
return $this->connection;
}
public function setLogger(LoggerInterface $logger): Install
{
$this->logger = $logger;
return $this;
}
public function setConnection(PDO $pdo): Install
{
$this->connection = $pdo;
return $this;
}
public function run(): void
{
$tables = [
'messages' => [
'`mailbox_id` int UNSIGNED NOT NULL',
'`position` int UNSIGNED NOT NULL',
'`uid` int UNSIGNED NOT NULL PRIMARY KEY',
'`subject` varchar(255) NOT NULL',
'`from` varchar(100) NOT NULL',
'`date_time` datetime NOT NULL',
'`has_attachments` int(1) DEFAULT 0',
'`valid_attachments` int(1) DEFAULT 0',
'`downloaded_attachments` int(1) DEFAULT 0'
],
'attachments' => [
'`message_uid` int UNSIGNED NOT NULL',
'`filename` varchar(255) NOT NULL PRIMARY KEY',
'`encrypted` int(1) DEFAULT 0',
'`decrypted` int(1) DEFAULT 0',
'CONSTRAINT `message_uid_fk` FOREIGN KEY (`message_uid`) REFERENCES `messages` (`uid`)'
],
'mailboxes' => [
'`'
]
];
foreach ($tables as $table => $definitions) {
$this->getConnection()->query($this->buildCreateTable($table, $definitions));
}
}
protected function buildCreateTable(string $table_name, array $columns): string
{
$query = ["CREATE TABLE IF NOT EXISTS `{$table_name}` ("];
$query []= implode(',' . PHP_EOL, $columns);
$query []= ')';
return implode(PHP_EOL, $query);
}
}