src/Sylius/Bundle/CoreBundle/EventListener/ChannelDeletionListener.php line 34

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\CoreBundle\EventListener;
  12. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  13. use Sylius\Component\Channel\Model\ChannelInterface;
  14. use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
  15. use Sylius\Component\Resource\Exception\UnexpectedTypeException;
  16. final class ChannelDeletionListener
  17. {
  18.     /** @var ChannelRepositoryInterface */
  19.     private $channelRepository;
  20.     public function __construct(ChannelRepositoryInterface $repository)
  21.     {
  22.         $this->channelRepository $repository;
  23.     }
  24.     /**
  25.      * Prevent channel deletion if no more channels enabled.
  26.      */
  27.     public function onChannelPreDelete(ResourceControllerEvent $event): void
  28.     {
  29.         $channel $event->getSubject();
  30.         if (!$channel instanceof ChannelInterface) {
  31.             throw new UnexpectedTypeException(
  32.                 $channel,
  33.                 ChannelInterface::class
  34.             );
  35.         }
  36.         $results $this->channelRepository->findBy(['enabled' => true]);
  37.         if (!$results || (count($results) === && current($results) === $channel)) {
  38.             $event->stop('sylius.channel.delete_error');
  39.         }
  40.     }
  41. }