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

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