-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OP-291: Extract abstraction to prevent code repetition
- Loading branch information
Showing
11 changed files
with
116 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Command/Wishlist/AddSelectedProductsToCartInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* You can find more information about us on https://bitbag.io and write us | ||
* an email on hello@bitbag.io. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusWishlistPlugin\Command\Wishlist; | ||
|
||
use Doctrine\Common\Collections\Collection; | ||
|
||
interface AddSelectedProductsToCartInterface extends WishlistSyncCommandInterface | ||
{ | ||
public function getWishlistProducts(): Collection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* You can find more information about us on https://bitbag.io and write us | ||
* an email on hello@bitbag.io. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusWishlistPlugin\Controller\Action; | ||
|
||
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistSyncCommandInterface; | ||
use BitBag\SyliusWishlistPlugin\Exception\InsufficientProductStockException; | ||
use BitBag\SyliusWishlistPlugin\Exception\InvalidProductQuantityException; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||
|
||
abstract class BaseAddWishlistProductsAction extends BaseWishlistProductsAction | ||
{ | ||
abstract protected function getCommand(FormInterface $form): WishlistSyncCommandInterface; | ||
|
||
protected function handleCommand(FormInterface $form): void | ||
{ | ||
$command = $this->getCommand($form); | ||
|
||
try { | ||
$this->messageBus->dispatch($command); | ||
if (false === $this->getFlashBag()->has('success')) { | ||
$this->getFlashBag()->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_to_cart')); | ||
} | ||
} catch (HandlerFailedException $exception) { | ||
$this->getFlashBag()->add('error', $this->getExceptionMessage($exception)); | ||
} | ||
} | ||
|
||
private function getExceptionMessage(HandlerFailedException $exception): string | ||
{ | ||
$previous = $exception->getPrevious(); | ||
if ($previous instanceof InsufficientProductStockException) { | ||
return $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.insufficient_stock', ['%productName%' => $previous->getProductName()]); | ||
} | ||
if ($previous instanceof InvalidProductQuantityException) { | ||
return $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.increase_quantity'); | ||
} | ||
|
||
return $exception->getMessage(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters