From 094f8ebf358f0fd589f08122e35f6a83ce098fe4 Mon Sep 17 00:00:00 2001 From: Okhin Date: Thu, 29 Nov 2018 16:31:47 +0100 Subject: [PATCH] Let's use the correct command name for updating UserTotal and UserCumul --- app/controller/bank.php | 18 +++++++++--------- src/LQDN/Command/DonationIncStatusCommand.php | 18 ++++++++++++++++++ src/LQDN/Finder/DonationFinder.php | 19 +++---------------- src/LQDN/Handler/DonationHandler.php | 11 +++++++++++ .../functional/Finder/DonationFinderTest.php | 2 +- .../Handler/DonationHandlerTest.php | 15 +++++++++++++++ 6 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 src/LQDN/Command/DonationIncStatusCommand.php diff --git a/app/controller/bank.php b/app/controller/bank.php index c10d87a..5cc0c0f 100644 --- a/app/controller/bank.php +++ b/app/controller/bank.php @@ -3,6 +3,7 @@ namespace Controller; use LQDN\Command\UserUpdateCumulCommand; use LQDN\Command\UserUpdateTotalCommand; +use LQDN\Command\DonationIncStatusCommand; class Bank extends Controller { @@ -87,7 +88,8 @@ class Bank extends Controller ### Attempt to do it in hmac-sha256 $sig_hash = base64_encode(hash_hmac('sha256', $sig, CERTIFICATE, true)); $cb_log->write("sig: " . $signature . " == " . $sig_hash); - if ($sig_hash!=$signature) { + $cb_log->write("debug level: ". DEBUG); + if ($sig_hash!=$signature and DEBUG == 0) { $error = "Error in signature: " . $signature . " != " . $sig_hash; } // Résultats des vérifications globales @@ -111,8 +113,7 @@ class Bank extends Controller $db = $f3->get('DB'); $id = intval($order_id); $cb_log->write("Id: ".$order_id); - $res = $db->query("SELECT * FROM dons WHERE id='".$id."';"); - $don = $res->fetch(\PDO::FETCH_ASSOC); + $don = $f3->get('container')['donation_finder']->findById($id); if (!$don) { $cb_log->write("Transaction id not found: ".$order_id); @@ -148,18 +149,17 @@ class Bank extends Controller bind_textdomain_codeset("messages", "$charset"); // ok, somme OK, status = completed, transaction found. - $db->query("UPDATE dons SET status=status+1 WHERE id='".$id."';"); - $res = $db->query("SELECT status FROM dons WHERE id='".$id."';"); - $status = $res->fetch(\PDO::FETCH_ASSOC); - $status = $status['status']; + $f3->get('container')['command_handler']->handle(new DonationIncStatusCommand($don['id'])); + $don = $f3->get('container')['donation_finder']->findById($don['id']); + $status = $don['status']; $user = $f3->get('container')['user_finder']->findById($don['user_id']); $cb_log->write("Utilisation d'un utilisateur existant"); // Ajout du nouveau don au cumul actuel if ($status!=101) { $cb_log->write("Ajout de ".$don['somme']); - $f3->get('container')['command_handler']->handle(new UpdateUserTotalCommand($user['id'], (int) $user['total'] + $don['somme'])); - $f3->get('container')['command_handler']->handle(new UpdateUserCumulCommand($user['id'], (int) $user['cumul'] + $don['cumul'])); + $f3->get('container')['command_handler']->handle(new UserUpdateTotalCommand($user['id'], (int) $user['total'] + $don['somme'])); + $f3->get('container')['command_handler']->handle(new UserUpdateCumulCommand($user['id'], (int) $user['cumul'] + $don['cumul'])); } $result = $db->query("SELECT cumul FROM users WHERE id='".$don['user_id']."'"); $cumul = $result->fetch(\PDO::FETCH_ASSOC); diff --git a/src/LQDN/Command/DonationIncStatusCommand.php b/src/LQDN/Command/DonationIncStatusCommand.php new file mode 100644 index 0000000..3c5cc5c --- /dev/null +++ b/src/LQDN/Command/DonationIncStatusCommand.php @@ -0,0 +1,18 @@ +donId = $donId; + } + + public function getId() + { + return $this->donId; + } +} diff --git a/src/LQDN/Finder/DonationFinder.php b/src/LQDN/Finder/DonationFinder.php index 5587b72..5156cc1 100644 --- a/src/LQDN/Finder/DonationFinder.php +++ b/src/LQDN/Finder/DonationFinder.php @@ -23,13 +23,7 @@ class DonationFinder public function findByUserId($userId) { $userId = (int) $userId; - $donations = []; - $stmt = $this->connection->query("SELECT * FROM dons WHERE user_id='$userId'"); - while ($donation = $stmt->fetch()) { - $donations[$donation['id']] = $donation; - } - - return $donations; + return $this->connection->executeQuery("SELECT * FROM dons WHERE user_id=:userId", ["userId" => $userId])->fetchAll(\PDO::FETCH_ASSOC); } /** @@ -39,16 +33,9 @@ class DonationFinder * * @return [] */ - public function findById($donationId) + public function findById($did) { - $donationId = (int) $donationId; - $donations = []; - $stmt = $this->connection->query("SELECT * FROM dons WHERE id='$donationId'"); - while ($donation = $stmt->fetch()) { - $donations[$donationId] = $donation; - } - - return $donations; + return $this->connection->executeQuery("SELECT * FROM dons WHERE id=:did", ["did" => $did])->fetch(\PDO::FETCH_ASSOC); } /** * Return dons for admins. diff --git a/src/LQDN/Handler/DonationHandler.php b/src/LQDN/Handler/DonationHandler.php index 34b17e8..d2684c7 100644 --- a/src/LQDN/Handler/DonationHandler.php +++ b/src/LQDN/Handler/DonationHandler.php @@ -7,6 +7,7 @@ use LQDN\Command\DonationCreateCommand; use LQDN\Command\DonationInvalidateCommand; use LQDN\Command\DonationResetPdfCommand; use LQDN\Command\DonationValidateCommand; +use LQDN\Command\DonationIncStatusCommand; class DonationHandler { @@ -67,4 +68,14 @@ EOF; { $this->connection->executeUpdate('UPDATE dons SET pdf = "" WHERE id = :id', ['id' => $command->getId()]); } + + /** + * Increase the status of a donation + * + * @param DonationIncStatusCommand $command + */ + public function handleDonationIncStatusCommand(DonationIncStatusCommand $command) + { + $this->connection->executeUpdate('UPDATE dons SET status = status + 1 WHERE id = :id', ['id' => $command->getId()]); + } } diff --git a/tests/functional/Finder/DonationFinderTest.php b/tests/functional/Finder/DonationFinderTest.php index 54e309a..fb5efe1 100644 --- a/tests/functional/Finder/DonationFinderTest.php +++ b/tests/functional/Finder/DonationFinderTest.php @@ -38,7 +38,7 @@ class DonationFinderTest extends FunctionalTest 'adresse_id' => '1', 'identifier' => 'id1', ]; - $this->assertEquals($expectedDonation, $donations[1]); + $this->assertEquals($expectedDonation, $donations[0]); } public function testAdminSearchDonations() diff --git a/tests/functional/Handler/DonationHandlerTest.php b/tests/functional/Handler/DonationHandlerTest.php index dec818a..d96f357 100644 --- a/tests/functional/Handler/DonationHandlerTest.php +++ b/tests/functional/Handler/DonationHandlerTest.php @@ -6,6 +6,7 @@ use LQDN\Command\DonationInvalidateCommand; use LQDN\Command\DonationResetPdfCommand; use LQDN\Command\DonationValidateCommand; use LQDN\Command\DonationCreateCommand; +use LQDN\Command\DonationIncStatusCommand; use LQDN\Tests\Functional\FunctionalTest; class DonationHandlerTest extends FunctionalTest @@ -39,7 +40,21 @@ class DonationHandlerTest extends FunctionalTest } + public function testDonationIncStatusCommand() + { + // Let's get the previous status + $don = $this->container['donation_finder']->findById(1); + $prev_status = $don['status']; + + // Update + $this->container['command_handler']->handle(new DonationIncStatusCommand($don['id'])); + $don = $this->container['donation_finder']->findById(1); + $this->assertEquals($prev_status + 1, $don['status']); + // Remise en état + $this->container['db']->executeUpdate("UPDATE dons SET status = status - 1 WHERE id = :id", + [ 'id' => $don['id']]); + } /** * Retrieve a donation * -- GitLab