function it_recognizes_a_subject_as_eligible_if_nth_order_is_equal_with_configured(CustomerInterface $customer, OrderInterface $subject, OrderRepositoryInterface $ordersRepository) { $subject->getCustomer()->willReturn($customer); $customer->getId()->willReturn(1); $ordersRepository->countByCustomer($customer)->willReturn(9); $this->isEligible($subject, ['nth' => 10])->shouldReturn(true); }
/** * {@inheritdoc} */ public function isEligible(PromotionSubjectInterface $subject, array $configuration) { if (!$subject instanceof OrderInterface) { throw new UnsupportedTypeException($subject, OrderInterface::class); } if (!isset($configuration['nth']) || !is_int($configuration['nth'])) { return false; } $customer = $subject->getCustomer(); if (null === $customer) { return false; } //eligible if it is first order of guest and the promotion is on first order if (null === $customer->getId()) { return 1 === $configuration['nth']; } return $this->orderRepository->countByCustomer($customer) === $configuration['nth'] - 1; }