vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/EventListener/ChannelDeletionListener.php line 33

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.     private ChannelRepositoryInterface $channelRepository;
  19.     public function __construct(ChannelRepositoryInterface $repository)
  20.     {
  21.         $this->channelRepository $repository;
  22.     }
  23.     /**
  24.      * Prevent channel deletion if no more channels enabled.
  25.      */
  26.     public function onChannelPreDelete(ResourceControllerEvent $event): void
  27.     {
  28.         $channel $event->getSubject();
  29.         if (!$channel instanceof ChannelInterface) {
  30.             throw new UnexpectedTypeException(
  31.                 $channel,
  32.                 ChannelInterface::class
  33.             );
  34.         }
  35.         $results $this->channelRepository->findBy(['enabled' => true]);
  36.         if (!$results || (count($results) === && current($results) === $channel)) {
  37.             $event->stop('sylius.channel.delete_error');
  38.         }
  39.     }
  40. }