From 28f1d7c6666383993a2910c90e3c31133dd9a9aa Mon Sep 17 00:00:00 2001 From: thekabal Date: Sun, 11 Mar 2018 20:26:33 -0600 Subject: [PATCH] Implement SectorsGateway --- classes/KabalTrade.php | 2 +- classes/Sectors/SectorsGateway.php | 44 ++++++++++++++++++++++++++++++ main.php | 7 ++--- 3 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 classes/Sectors/SectorsGateway.php diff --git a/classes/KabalTrade.php b/classes/KabalTrade.php index 9656843bb..2f9153602 100644 --- a/classes/KabalTrade.php +++ b/classes/KabalTrade.php @@ -32,7 +32,7 @@ public static function trade(\PDO $pdo_db, array $playerinfo, Reg $tkireg): void $shipgoods = null; // Obtain sector information - $sql = "SELECT * FROM ::prefix::universe WHERE sector_id=:sector_id"; + $sql = "SELECT * FROM ::prefix::universe WHERE sector_id=:sector_id LIMIT 1"; $stmt = $pdo_db->prepare($sql); $stmt->bindParam(':sector_id', $playerinfo['sector'], \PDO::PARAM_INT); $stmt->execute(); diff --git a/classes/Sectors/SectorsGateway.php b/classes/Sectors/SectorsGateway.php new file mode 100644 index 000000000..b3498f67c --- /dev/null +++ b/classes/Sectors/SectorsGateway.php @@ -0,0 +1,44 @@ +. +// +// File: classes/Sectors/SectorsGateway.php + +namespace Tki\Sectors; // Domain Entity organization pattern, Sectors objects + +class SectorsGateway // Gateway for SQL calls related to Sectors +{ + /** @var \PDO **/ + protected $pdo_db; // This will hold a protected version of the pdo_db variable + + public function __construct(\PDO $pdo_db) // Create the this->pdo_db object + { + $this->pdo_db = $pdo_db; + } + + public function selectSectorInfo(int $sector_id) + { + $sql = "SELECT * FROM ::prefix::universe WHERE sector_id=:sector_id LIMIT 1"; + $stmt = $this->pdo_db->prepare($sql); + $stmt->bindParam(':sector_id', $sector_id, \PDO::PARAM_INT); + $stmt->execute(); + \Tki\Db::logDbErrors($this->pdo_db, $sql, __LINE__, __FILE__); // Log any errors, if there are any + + // A little magic here. If it couldn't select a sector, the following call will return false - which is what we want for "no sector found". + $sectorinfo = $stmt->fetch(\PDO::FETCH_ASSOC); + return $sectorinfo; // FUTURE: Eventually we want this to return a sector object instead, for now, sectorinfo array or false for no user found. + } +} diff --git a/main.php b/main.php index 71896f664..98f22f1cc 100644 --- a/main.php +++ b/main.php @@ -54,11 +54,8 @@ // Pull sector info from database -$sql = "SELECT * FROM ::prefix::universe WHERE sector_id=:sector_id"; -$stmt = $pdo_db->prepare($sql); -$stmt->bindParam(':sector_id', $playerinfo['sector'], PDO::PARAM_INT); -$stmt->execute(); -$sectorinfo = $stmt->fetch(PDO::FETCH_ASSOC); +$sectors_gateway = new \Tki\Sectors\SectorsGateway($pdo_db); // Build a sector gateway object to handle the SQL calls +$sectorinfo = $sectors_gateway->selectsectorInfo($playerinfo['sector']); if ($playerinfo['on_planet'] == "Y") {