diff --git a/src/LQDN/Command/CounterpartChangeStateCommand.php b/src/LQDN/Command/CounterpartChangeStateCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..a88171607c11c93ed419224f0a57dcafbf4048c2 --- /dev/null +++ b/src/LQDN/Command/CounterpartChangeStateCommand.php @@ -0,0 +1,25 @@ +<?php + +namespace LQDN\Command; + +class CounterpartChangeStateCommand +{ + private $counterpartId; + private $state; + + public function __construct($counterpartId, $state) + { + $this->counterpartId = $counterpartId; + $this->state = $state; + } + + public function getCounterpartId() + { + return $this->counterpartId; + } + + public function getState() + { + return $this->state; + } +} diff --git a/src/LQDN/Command/CounterpartCreateCommand.php b/src/LQDN/Command/CounterpartCreateCommand.php index 08e31ccce276f4f42439268ed673712f61ee8714..2cfa70193b18bdc7a00fa75821ed0cce425fa073 100644 --- a/src/LQDN/Command/CounterpartCreateCommand.php +++ b/src/LQDN/Command/CounterpartCreateCommand.php @@ -2,7 +2,7 @@ namespace LQDN\Command; -class CounterPartCreateCommand +class CounterpartCreateCommand { private $counterpartId; private $adresseId; @@ -12,7 +12,7 @@ class CounterPartCreateCommand private $status; private $date; - public function __construct($adresseId, $userId, $quoi, $taille, $status, $date) + public function __construct($addressId, $userId, $quoi, $taille, $status, $date) { $this->userId = $userId; $this->addressId = $addressId; @@ -29,7 +29,7 @@ class CounterPartCreateCommand public function getAddressId() { - return $this->addressid; + return $this->addressId; } public function getQuoi() diff --git a/src/LQDN/Command/CounterpartDeleteCommand.php b/src/LQDN/Command/CounterpartDeleteCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..570a4b7236758cc17f41f30ac450f55252f8b098 --- /dev/null +++ b/src/LQDN/Command/CounterpartDeleteCommand.php @@ -0,0 +1,18 @@ +<?php + +namespace LQDN\Command; + +class CounterpartDeleteCommand +{ + private $counterpartId; + + public function __construct($counterpartId) + { + $this->counterpartId = $counterpartId; + } + + public function getCounterpartId() + { + return $this->counterpartId; + } +} diff --git a/src/LQDN/Finder/CounterpartFinder.php b/src/LQDN/Finder/CounterpartFinder.php index 19d3a814d5db718cc7350b0f81b3fda500682911..b566c55af7d5b1b250b2846a33d8388d0effdcf7 100644 --- a/src/LQDN/Finder/CounterpartFinder.php +++ b/src/LQDN/Finder/CounterpartFinder.php @@ -56,4 +56,91 @@ EOQ; return $counterparts; } + + /** + * Return all counterparts in a specific status + * + * @param mixed $status + * + * @return array + */ + public function findByStatus($status) + { + $status = (int) $status; + $counterparts = []; + $stmt = $this->connection->executeQuery('SELECT * FROM contreparties WHERE status = :status', ['status' => $status]); + while ($counterpart = $stmt->fetch()) { + $counterpart['pdf_id'] = ''; + $counterpart['pdf_nom'] = ''; + $counterpart['pdf_url'] = ''; + + // Of course, piplomes are messy + // @TODO: This part is probably not working. + if ('piplome' === $counterpart['quoi']) { + $query = <<<EOQ +SELECT d.id as id, a.nom as pseudo, d.pdf as pdf +FROM dons d +JOIN adresses a ON a.id = d.adresse_id +JOIN contreparties c ON c.id = d.taille +WHERE d.taille = :size +EOQ; + + $pdf = $this->connection->fetchAssoc($query, [ + 'size' => $counterpart['taille'] + ]); + if (false !== $pdf) { + $counterpart['pdf_id'] = $pdf['id']; + $counterpart['pdf_nom'] = $pdf['pseudo']; + $counterpart['pdf_url'] = $pdf['pdf']; + } + } + + $counterparts[$counterpart['id']] = $counterpart; + } + + return $counterparts; + } + + /** Return all counterparts by quoi + * + * @param mixed $quoi + * + * @return array + */ + public function findByQuoi($quoi) + { + $quoi = (string) $quoi; + $counterparts = []; + $stmt = $this->connection->executeQuery('SELECT * FROM contreparties WHERE quoi = :quoi', ['quoi' => $quoi]); + while ($counterpart = $stmt->fetch()) { + $counterpart['pdf_id'] = ''; + $counterpart['pdf_nom'] = ''; + $counterpart['pdf_url'] = ''; + + // Of course, piplomes are messy + // @TODO: This part is probably not working. + if ('piplome' === $counterpart['quoi']) { + $query = <<<EOQ +SELECT d.id as id, a.nom as pseudo, d.pdf as pdf +FROM dons d +JOIN adresses a ON a.id = d.adresse_id +JOIN contreparties c ON c.id = d.taille +WHERE d.taille = :size +EOQ; + + $pdf = $this->connection->fetchAssoc($query, [ + 'size' => $counterpart['taille'] + ]); + if (false !== $pdf) { + $counterpart['pdf_id'] = $pdf['id']; + $counterpart['pdf_nom'] = $pdf['pseudo']; + $counterpart['pdf_url'] = $pdf['pdf']; + } + } + + $counterparts[$counterpart['id']] = $counterpart; + } + + return $counterparts; + } } diff --git a/src/LQDN/Handler/AddressHandler.php b/src/LQDN/Handler/AddressHandler.php index c4dda1d2f2c3787e94fc075b51507a4d8f7b65fc..ddc37cc05208af3885539ca8d7843435733413ca 100644 --- a/src/LQDN/Handler/AddressHandler.php +++ b/src/LQDN/Handler/AddressHandler.php @@ -67,7 +67,6 @@ EOF; * Check if an address already exists. * * @param int $userId - * @param string $alias * * @return bool */ diff --git a/src/LQDN/Handler/CounterpartHandler.php b/src/LQDN/Handler/CounterpartHandler.php index d14a353153a7118f3c9f23c1cb87714201eb1644..6fdbce712ce17b263f295e9b46fbf3f1ac75f3cf 100644 --- a/src/LQDN/Handler/CounterpartHandler.php +++ b/src/LQDN/Handler/CounterpartHandler.php @@ -5,7 +5,7 @@ namespace LQDN\Handler; use Doctrine\DBAL\Connection; use LQDN\Command\CounterpartCreateCommand; use LQDN\Command\CounterpartDeleteCommand; -use LQDN\Command\CounterpartUpdateCommand; +use LQDN\Command\CounterpartChangeStateCommand; use LQDN\Exception\CounterpartAlreadyExistsException; class CounterpartHandler @@ -25,9 +25,10 @@ class CounterpartHandler public function handleCounterpartDeleteCommand(CounterpartDeleteCommand $command) { $counterpartId = $command->getCounterpartId(); + // Let's check if the counterpart exist $this->connection->executeUpdate( - "DELETE FROM counterparts WHERE id = :id", - ['id' => $counterpartId()] + "DELETE FROM contreparties WHERE id = :id", + ['id' => $counterpartId] ); } @@ -39,18 +40,75 @@ class CounterpartHandler public function handleCounterpartCreateCommand(CounterpartCreateCommand $command) { $userId = $command->getUserId(); - $adresseId = $command->getAdressId(); + $adresseId = $command->getAddressId(); $query =<<<EOF -REPLACE INTO counterparts(datec, user_id, addresse_id, quoi, taille, status) -VALUES (:datec, :user_id, :addresse_id, :quoi, :taille, :status) +INSERT INTO contreparties(datec, user_id, adresse_id, quoi, taille, status) +VALUES (:datec, :user_id, :adresse_id, :quoi, :taille, :status) EOF; $stmt = $this->connection->prepare($query); + $stmt->bindValue('datec', $command->getDateCreation()); $stmt->bindValue('user_id', $command->getUserId()); $stmt->bindValue('adresse_id', $command->getAddressId()); $stmt->bindValue('quoi', $command->getQuoi()); $stmt->bindValue('taille', $command->getTaille()); - $stmt->bindValue('datec', $command->getDate()); $stmt->bindValue('status', $command->getStatus()); + $stmt->execute(); + } + + /** + * Chnge the state of a counterpart + * + * @param CounterpartChangeStateCommand $command + */ + public function handleCounterpartChangeStateCommand(CounterpartChangeStateCommand $command) + { + $counterpartId = $command->getCounterpartId(); + $status = $command->getState(); + + $this->connection->executeUpdate( + "UPDATE contreparties SET status = :status WHERE id=:id", + [ + 'status' => $status, + 'id' => $counterpartId, + ] + ); + } + + /** + * Test if a counterpart exists + * + * @param int $counterpartId + * + * @return bool + */ + private function counterpartExists($counterpartId) + { + return (bool) $this->connection->fetchColumn( + "SELECT 1 FROM contreparties WHERE id = :counterpart_id", + [ + 'counterpart_id' => $counterpartId, + ], + 0 + ); + } + + /** + * Test if the counterpart can be deleted + * + * @param int $countepartId + * + * @return bool + */ + private function counterpartUsed($counterpartId) + { + $status = (int) $this->connection->fetchColumn( + "SELECT status FROM contreparties WHERE id = :id", + [ + 'id' => $counterpartId, + ], + 0 + ); + return ($status == 2); // status 2 is delivered counterparts } } diff --git a/tests/functional/Finder/CounterpartFinderTest.php b/tests/functional/Finder/CounterpartFinderTest.php index ac46f81b2f18f431c00dd1fd71265594a5e4733f..702ff2e9454f7376f0ef3bceeeedad7f2c66c539 100644 --- a/tests/functional/Finder/CounterpartFinderTest.php +++ b/tests/functional/Finder/CounterpartFinderTest.php @@ -29,4 +29,50 @@ class CounterpartFinderTest extends FunctionalTest ]; $this->assertEquals($expectedCounterpart, $firstCounterpart); } + + public function testFindByQuoi() + { + $counterparts = $this->container['counterpart_finder']->findByQuoi('piplome'); + $this->assertCount(1, $counterparts); + $firstCounterpart = reset($counterparts); + + // Check the first counterpart + $expectedCounterpart = [ + 'id' => '2', + 'user_id' => '2', + 'datec' => '2016-06-22 12:34:00', + 'quoi' => 'piplome', + 'taille' => '2', + 'status' => '1', + 'adresse_id' => null, + 'pdf_id' => '1', + 'pdf_nom' => 'Main', + 'pdf_url' => 'pdf', + 'commentaire' => '', + ]; + $this->assertEquals($expectedCounterpart, $firstCounterpart); + + } + + public function testFindById() + { + $counterparts = $this->container['counterpart_finder']->findByStatus(1); + $this->assertCount(2, $counterparts); + + $firstCounterpart = reset($counterparts); + $expectedCounterpart = [ + 'id' => '1', + 'datec' => '2016-06-22 12:34:00', + 'user_id' => '1', + 'quoi' => 'pishirt', // [piplome|pibag|pishirt|hoodie] + 'taille' => '8', + 'status' => '1', + 'adresse_id' => '1', + 'pdf_id' => '', + 'pdf_nom' => '', + 'pdf_url' => '', + 'commentaire' => '', + ]; + $this->assertEquals($expectedCounterpart, $firstCounterpart); + } } diff --git a/tests/functional/Handler/CounterpartHandlerTest.php b/tests/functional/Handler/CounterpartHandlerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6df472fbd54c01d651fca8493c2f1f571f8254fd --- /dev/null +++ b/tests/functional/Handler/CounterpartHandlerTest.php @@ -0,0 +1,45 @@ +<?php + +namespace LQDN\Tests\Functional\Handler; + +use LQDN\Command\CounterpartCreateCommand; +use LQDN\Command\CounterpartDeleteCommand; +use LQDN\Command\CounterpartChangeStateCommand; +use LQDN\Handler\CounterpartHandler; +use LQDN\Tests\Functional\FunctionalTest; + +class CounterpartHandlerTest extends FunctionalTest +{ + public function testCounterpartDelete() + { + $this->assertTrue($this->counterpartExists(1)); + + $this->container['command_handler']->handle(new CounterpartDeleteCommand(1)); + } + + public function testCounterpartCreate() + { + $this->assertFalse($this->counterpartExists(3)); + + $this->container['command_handler']->handle(new CounterpartCreateCommand(1, 1, 'pishirt', 4, 1, date("Y-m-d H:i:s"))); + } + + public function testCounterpartChangeState() + { + $this->assertTrue($this->counterpartExists(1)); + + $this->container['command_handler']->handle(new CounterpartChangeStateCommand(1, 2)); + } + + /** + * Check if a counterpart exists in BDD + * + * @param int $id + * + * @return bool + */ + private function counterpartExists($id) + { + return (bool) $this->container['db']->fetchColumn("SELECT 1 FROM contreparties WHERE id = $id"); + } +}