From d82638445d363db60c29b8a2a8f1a0bfc29a2ef2 Mon Sep 17 00:00:00 2001 From: LucileDT Date: Mon, 13 Jul 2020 13:38:54 +0200 Subject: [PATCH] Ref #215: check if bank information is needed when setting one to a payment --- src/Entity/Payment.php | 12 ++++++++ src/Entity/PaymentType.php | 22 ++++++++++++++ src/Migrations/Version20200713102639.php | 37 ++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/Migrations/Version20200713102639.php diff --git a/src/Entity/Payment.php b/src/Entity/Payment.php index d9252113..5960d444 100644 --- a/src/Entity/Payment.php +++ b/src/Entity/Payment.php @@ -204,8 +204,20 @@ function getBank() return $this->bank; } + /** + * Set Bank for this payment + * + * @param \App\Entity\Bank $bank Bank to set for this payment + * @return \self + * @throws \Exception If bank information is not needed in the + * corresponding payment type, throws an exception + */ function setBank(Bank $bank): self { + if (!$this->type->isBankneeded()) + { + throw new \Exception('Bank information is not needed in this payment.'); + } $this->bank = $bank; return $this; diff --git a/src/Entity/PaymentType.php b/src/Entity/PaymentType.php index 664f7578..2aa84e67 100644 --- a/src/Entity/PaymentType.php +++ b/src/Entity/PaymentType.php @@ -36,6 +36,16 @@ class PaymentType */ private $payments; + /** + * Boolean to check in the Payment of this type if Bank information + * is needed. + * + * @var boolean True is Bank is needed in payment, false otherwise + * + * @ORM\Column(type="boolean", length=255) + */ + private $isBankneeded; + public function __construct() { $this->payments = new ArrayCollection(); @@ -100,4 +110,16 @@ public function removePayment(Payment $payment): self return $this; } + + function isBankneeded(): bool + { + return $this->isBankneeded; + } + + function setIsBankneeded(bool $isBankneeded): self + { + $this->isBankneeded = $isBankneeded; + + return $this; + } } diff --git a/src/Migrations/Version20200713102639.php b/src/Migrations/Version20200713102639.php new file mode 100644 index 00000000..c420daee --- /dev/null +++ b/src/Migrations/Version20200713102639.php @@ -0,0 +1,37 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('ALTER TABLE payment_type ADD is_bankneeded TINYINT(1) NOT NULL'); + + // Bank information is only needed in payment of type Check + $this->addSql('UPDATE payment_type SET is_bankneeded = 0 WHERE id = 1'); + $this->addSql('UPDATE payment_type SET is_bankneeded = 0 WHERE id = 2'); + $this->addSql('UPDATE payment_type SET is_bankneeded = 0 WHERE id = 3'); + $this->addSql('UPDATE payment_type SET is_bankneeded = 1 WHERE id = 4'); + $this->addSql('UPDATE payment_type SET is_bankneeded = 0 WHERE id = 5'); + } + + public function down(Schema $schema) : void + { + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('ALTER TABLE payment_type DROP is_bankneeded'); + } +}