<?php
 namespace App\ImportEmailBookings; class EmailsStorage { private string $dbTable; public function __construct() { global $DBTEmailBookings; $this->dbTable = $DBTEmailBookings; } public function insert(string $account, string $idEmail, string $supplier, int $idImportedOrder) :void { $CFG = \Engine\Loader_Cfg::getInstance()->getGlobal(); if (!\DB::isInsideTransaction()) { throw new \Exception('You must be inside a transaction to insert a new booking email.'); } if (!key_exists($account, $CFG['loadEmailBookings']['accountsList'])) { throw new \RuntimeException('Account code not set or invalid.'); } if (!strlen($idEmail)) { throw new \RuntimeException('Email id not set.'); } if (!in_array($supplier, $CFG['loadEmailBookings']['suppliersList'])) { throw new \RuntimeException('Supplier is not set or valid.'); } if ($this->getDetails($account, $idEmail, true)) { throw new \RuntimeException('Email id is already imported.'); } $db = \DB::getInstance(); $sql = "
			INSERT INTO `{$this->dbTable}` (account, idEmail, supplier, idImportedOrder, importTime)
			VALUES (?, ?, ?, ?, ?)
		"; $prepVars = [$account, $idEmail, $supplier, $idImportedOrder, DATETIME]; if (!$db->Execute($sql, $prepVars)) { throw new \DBException('Could not insert email in the history DB table.'); } } public function getDetails(string $account, string $idEmail, bool $forUpdate) :array|false { $db = \DB::getInstance(); $sql = "SELECT * FROM `{$this->dbTable}` WHERE account=? AND idEmail=?"; if ($forUpdate) { $sql .= " FOR UPDATE"; } $emailDetails = $db->GetRow($sql, [$account, $idEmail]); if ($db->ErrorNo()) { throw new \DBException('Could not get email message details.'); } if (!$emailDetails) { return false; } else { return $emailDetails; } } } 