vendor/store.shopware.com/netzpevents6/src/Components/HasEventsRule.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetzpEvents6\Components;
  3. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  4. use Shopware\Core\Checkout\Cart\Rule\CartRuleScope;
  5. use Shopware\Core\Checkout\Cart\Rule\LineItemScope;
  6. use Shopware\Core\Framework\Rule\Rule;
  7. use Shopware\Core\Framework\Rule\RuleScope;
  8. use Symfony\Component\Validator\Constraints\Type;
  9. class HasEventsRule extends Rule
  10. {
  11.     protected bool $hasEvents;
  12.     public function __construct()
  13.     {
  14.         parent::__construct();
  15.         $this->shouldHaveEvents false;
  16.     }
  17.     public function getName(): string
  18.     {
  19.         return 'netzpEventsRule';
  20.     }
  21.     public function match(RuleScope $scope): bool
  22.     {
  23.         if ($scope instanceof LineItemScope) {
  24.             return $this->lineItemHasEvents($scope->getLineItem());
  25.         }
  26.         if ( ! $scope instanceof CartRuleScope) {
  27.             return false;
  28.         }
  29.         $hasEvents false;
  30.         foreach($scope->getCart()->getLineItems() as $lineItem) {
  31.             if($this->lineItemHasEvents($lineItem)) {
  32.                 $hasEvents true;
  33.                 break;
  34.             }
  35.         }
  36.         if ($this->shouldHaveEvents) {
  37.             return $hasEvents;
  38.         }
  39.         return ! $hasEvents;
  40.     }
  41.     private function lineItemHasEvents(LineItem $lineItem)
  42.     {
  43.         if(array_key_exists('netzp_event'$lineItem->getPayload())) {
  44.             return true;
  45.         }
  46.         return false;
  47.     }
  48.     public function getConstraints(): array
  49.     {
  50.         return [
  51.             'shouldHaveEvents' => [new Type('bool')]
  52.         ];
  53.     }
  54. }