/** * {@inheritdoc} */ public function generate(VariableInterface $variable) { if (!$variable->hasOptions()) { throw new \InvalidArgumentException('Cannot generate variants for an object without options.'); } $optionSet = array(); $optionMap = array(); foreach ($variable->getOptions() as $k => $option) { foreach ($option->getValues() as $value) { $optionSet[$k][] = $value->getId(); $optionMap[$value->getId()] = $value; } } $permutations = $this->getPermutations($optionSet); foreach ($permutations as $permutation) { $variant = $this->variantRepository->createNew(); $variant->setObject($variable); $variant->setDefaults($variable->getMasterVariant()); if (is_array($permutation)) { foreach ($permutation as $id) { $variant->addOption($optionMap[$id]); } } else { $variant->addOption($optionMap[$permutation]); } $variable->addVariant($variant); $this->process($variable, $variant); } }
/** * @param PaymentSubjectInterface $payment * * @return AdjustmentInterface */ private function prepareAdjustmentForOrder(PaymentSubjectInterface $payment) { $adjustment = $this->adjustmentRepository->createNew(); $adjustment->setLabel(AdjustmentInterface::PAYMENT_ADJUSTMENT); $adjustment->setAmount($this->feeCalculator->calculate($payment)); $adjustment->setDescription($payment->getMethod()->getName()); return $adjustment; }
/** * {@inheritdoc} */ public function createPayment(OrderInterface $order) { $payment = $this->paymentRepository->createNew(); $payment->setCurrency($order->getCurrency()); $payment->setAmount($order->getTotal()); $order->addPayment($payment); return $payment; }
/** * {@inheritdoc} */ public function createPayment(OrderInterface $order) { $this->updateExistingPaymentsStates($order); /** @var $payment PaymentInterface */ $payment = $this->paymentRepository->createNew(); $payment->setCurrency($order->getCurrency()); $payment->setAmount($order->getTotal()); $order->addPayment($payment); return $payment; }
/** * @param ArchetypeInterface $archetype * @param AttributeSubjectInterface $subject */ private function createAndAssignAttributes(ArchetypeInterface $archetype, AttributeSubjectInterface $subject) { foreach ($archetype->getAttributes() as $attribute) { if (null === $subject->getAttributeByName($attribute->getName())) { /** @var AttributeValueInterface $attributeValue */ $attributeValue = $this->attributeValueRepository->createNew(); $attributeValue->setAttribute($attribute); $subject->addAttribute($attributeValue); } } }
/** * {@inheritdoc} */ public function generate(PromotionInterface $promotion, Instruction $instruction) { for ($i = 0, $amount = $instruction->getAmount(); $i < $amount; $i++) { $coupon = $this->repository->createNew(); $coupon->setPromotion($promotion); $coupon->setCode($this->generateUniqueCode()); $coupon->setUsageLimit($instruction->getUsageLimit()); $this->manager->persist($coupon); } $this->manager->flush(); }
/** * {@inheritdoc} */ public function build(PrototypeInterface $prototype, ProductInterface $product) { foreach ($prototype->getAttributes() as $attribute) { $attributeValue = $this->attributeValueRepository->createNew(); $attributeValue->setAttribute($attribute); $product->addAttribute($attributeValue); } foreach ($prototype->getOptions() as $option) { $product->addOption($option); } }
/** * {@inheritdoc} */ public function execute(PromotionSubjectInterface $subject, array $configuration, PromotionInterface $promotion) { if (!$subject instanceof OrderInterface && !$subject instanceof OrderItemInterface) { throw new UnexpectedTypeException($subject, 'Sylius\\Component\\Core\\Model\\OrderInterface or Sylius\\Component\\Core\\Model\\OrderItemInterface'); } $adjustment = $this->repository->createNew(); $adjustment->setAmount(-$configuration['amount']); $adjustment->setLabel(OrderInterface::PROMOTION_ADJUSTMENT); $adjustment->setDescription($promotion->getDescription()); $subject->addAdjustment($adjustment); }
/** * {@inheritdoc} */ public function createShipment(OrderInterface $order) { $shipment = $order->getShipments()->first(); if (!$shipment) { $shipment = $this->shipmentRepository->createNew(); $order->addShipment($shipment); } foreach ($order->getInventoryUnits() as $inventoryUnit) { if (null === $inventoryUnit->getShipment()) { $shipment->addItem($inventoryUnit); } } }
/** * {@inheritdoc} */ public function applyShippingCharges(OrderInterface $order) { // Remove all shipping adjustments, we recalculate everything from scratch. $order->removeAdjustments(AdjustmentInterface::SHIPPING_ADJUSTMENT); foreach ($order->getShipments() as $shipment) { $shippingCharge = $this->calculator->calculate($shipment); $adjustment = $this->adjustmentRepository->createNew(); $adjustment->setLabel(AdjustmentInterface::SHIPPING_ADJUSTMENT); $adjustment->setAmount($shippingCharge); $adjustment->setDescription($shipment->getMethod()->getName()); $order->addAdjustment($adjustment); } $order->calculateTotal(); }
/** * {@inheritdoc} */ public function create(StockableInterface $stockable, $quantity, $state = InventoryUnitInterface::STATE_SOLD) { if ($quantity < 1) { throw new \InvalidArgumentException('Quantity of units must be greater than 1.'); } $units = new ArrayCollection(); for ($i = 0; $i < $quantity; $i++) { $inventoryUnit = $this->repository->createNew(); $inventoryUnit->setStockable($stockable); $inventoryUnit->setInventoryState($state); $units->add($inventoryUnit); } return $units; }
/** * {@inheritdoc} */ public function addAttribute($name, $value, $presentation = null) { $attribute = $this->attributeRepository->findOneBy(array('name' => $name)); if (null === $attribute) { $attribute = $this->attributeRepository->createNew(); $attribute->setName($name); $attribute->setPresentation($presentation ?: $name); $this->productManager->persist($attribute); $this->productManager->flush($attribute); } $attributeValue = $this->attributeValueRepository->createNew(); $attributeValue->setAttribute($attribute); $attributeValue->setValue($value); $this->product->addAttribute($attributeValue); return $this; }
/** * Create promotion item * * @param array $configuration * * @return OrderItemInterface */ protected function createItem(array $configuration) { $variant = $this->variantRepository->find($configuration['variant']); $promotionItem = $this->itemRepository->createNew(); $promotionItem->setVariant($variant); $promotionItem->setQuantity((int) $configuration['quantity']); $promotionItem->setUnitPrice((int) $configuration['price']); return $promotionItem; }
/** * @param CurrencyInterface[] $managedCurrencies * @param string $code * @param float $rate * * @return null|CurrencyInterface */ protected function updateOrCreate(array $managedCurrencies, $code, $rate) { if (!empty($managedCurrencies) && in_array($code, $managedCurrencies)) { foreach ($managedCurrencies as $currency) { if ($code === $currency->getCode()) { $currency->setExchangeRate($rate); $this->manager->persist($currency); return; } } } else { /** @var $currency CurrencyInterface */ $currency = $this->repository->createNew(); $currency->setCode($code); $currency->setExchangeRate($rate); $this->manager->persist($currency); } }
/** * {@inheritdoc} */ public function getCart() { $this->initializeCart(); if (null !== $this->cart) { return $this->cart; } $this->cart = $this->repository->createNew(); $this->eventDispatcher->dispatch(SyliusCartEvents::CART_INITIALIZE, new CartEvent($this->cart)); return $this->cart; }
/** * Attach OAuth sign-in provider account to existing user * * @param FOSUserInterface $user * @param UserResponseInterface $response * * @return FOSUserInterface */ protected function updateUserByOAuthUserResponse(FOSUserInterface $user, UserResponseInterface $response) { $oauth = $this->oauthRepository->createNew(); $oauth->setIdentifier($response->getUsername()); $oauth->setProvider($response->getResourceOwner()->getName()); $oauth->setAccessToken($response->getAccessToken()); /* @var $user SyliusUserInterface */ $user->addOAuthAccount($oauth); $this->userManager->updateUser($user); return $user; }
private function addAdjustments(array $taxes, OrderInterface $order) { foreach ($taxes as $description => $tax) { $adjustment = $this->adjustmentRepository->createNew(); $adjustment->setLabel(AdjustmentInterface::TAX_ADJUSTMENT); $adjustment->setAmount($tax['amount']); $adjustment->setDescription($description); $adjustment->setNeutral($tax['included']); $order->addAdjustment($adjustment); } }
/** * @param string $code * * @return EmailInterface */ protected function getEmailFromConfiguration($code) { if (!isset($this->configuration[$code])) { throw new \InvalidArgumentException(sprintf('Email with code "%s" does not exist!', $code)); } $email = $this->emailRepository->createNew(); $configuration = $this->configuration[$code]; $email->setCode($code); $email->setSubject($configuration['subject']); $email->setTemplate($configuration['template']); if (isset($configuration['enabled']) && false === $configuration['enabled']) { $email->setEnabled(false); } if (isset($configuration['sender']['name'])) { $email->setSenderName($configuration['sender']['name']); } if (isset($configuration['sender']['address'])) { $email->setSenderAddress($configuration['sender']['address']); } return $email; }
function it_reverts_product(RepositoryInterface $variantRepository, RepositoryInterface $itemRepository, OrderInterface $order, OrderItemInterface $item, ProductVariantInterface $variant, PromotionInterface $promotion) { $configuration = array('variant' => 500, 'quantity' => 3, 'price' => 2); $variantRepository->find($configuration['variant'])->willReturn($variant); $itemRepository->createNew()->willReturn($item); $item->setUnitPrice($configuration['price'])->shouldBeCalled()->willReturn($item); $item->setVariant($variant)->shouldBeCalled()->willReturn($item); $item->setQuantity($configuration['quantity'])->shouldBeCalled()->willReturn($item); $item->equals($item)->willReturn(true); $order->getItems()->willReturn(array($item)); $order->removeItem($item)->shouldBeCalled(); $this->revert($order, $configuration, $promotion); }
/** * {@inheritdoc} * @throws ValidatorException */ public function saveSettings($namespace, Settings $settings) { $schema = $this->schemaRegistry->getSchema($namespace); $settingsBuilder = new SettingsBuilder(); $schema->buildSettings($settingsBuilder); $parameters = $settingsBuilder->resolve($settings->getParameters()); foreach ($settingsBuilder->getTransformers() as $parameter => $transformer) { if (array_key_exists($parameter, $parameters)) { $parameters[$parameter] = $transformer->transform($parameters[$parameter]); } } if (isset($this->resolvedSettings[$namespace])) { $transformedParameters = $this->transformParameters($settingsBuilder, $parameters); $this->resolvedSettings[$namespace]->setParameters($transformedParameters); } $persistedParameters = $this->parameterRepository->findBy(array('namespace' => $namespace)); $persistedParametersMap = array(); foreach ($persistedParameters as $parameter) { $persistedParametersMap[$parameter->getName()] = $parameter; } $this->eventDispatcher->dispatch(SettingsEvent::PRE_SAVE, new SettingsEvent($namespace, $settings, $parameters)); foreach ($parameters as $name => $value) { if (isset($persistedParametersMap[$name])) { $persistedParametersMap[$name]->setValue($value); } else { $parameter = $this->parameterRepository->createNew(); $parameter->setNamespace($namespace); $parameter->setName($name); $parameter->setValue($value); /* @var $errors ConstraintViolationListInterface */ $errors = $this->validator->validate($parameter); if (0 < $errors->count()) { throw new ValidatorException($errors->get(0)->getMessage()); } $this->parameterManager->persist($parameter); } } $this->parameterManager->flush(); $this->eventDispatcher->dispatch(SettingsEvent::POST_SAVE, new SettingsEvent($namespace, $settings, $parameters)); $this->cache->save($namespace, $parameters); }
function it_generates_variants_for_every_possible_permutation_of_an_objects_options_and_option_values(VariableInterface $productVariable, RepositoryInterface $variantRepository, SetBuilderInterface $setBuilder, OptionInterface $colorOption, OptionInterface $sizeOption, OptionValue $blackColor, OptionValue $whiteColor, OptionValue $redColor, OptionValue $smallSize, OptionValue $mediumSize, OptionValue $largeSize, VariantInterface $masterVariant, VariantInterface $permutationVariant) { $productVariable->hasOptions()->willReturn(true); $productVariable->getOptions()->willReturn(array($colorOption, $sizeOption)); $colorOption->getValues()->willReturn(array($blackColor, $whiteColor, $redColor)); $sizeOption->getValues()->willReturn(array($smallSize, $mediumSize, $largeSize)); $blackColor->getId()->willReturn('black1'); $whiteColor->getId()->willReturn('white2'); $redColor->getId()->willReturn('red3'); $smallSize->getId()->willReturn('small4'); $mediumSize->getId()->willReturn('medium5'); $largeSize->getId()->willReturn('large6'); $setBuilder->build(array(array('black1', 'white2', 'red3'), array('small4', 'medium5', 'large6')))->willReturn(array(array('black1', 'small4'), array('black1', 'medium5'), array('black1', 'large6'), array('white2', 'small4'), array('white2', 'medium5'), array('white2', 'large6'), array('red3', 'small4'), array('red3', 'medium5'), array('red3', 'large6'))); $productVariable->getMasterVariant()->willReturn($masterVariant); $variantRepository->createNew()->willReturn($permutationVariant); $permutationVariant->setObject($productVariable)->shouldBeCalled(); $permutationVariant->setDefaults($masterVariant)->shouldBeCalled(); $permutationVariant->addOption(Argument::type('Sylius\\Component\\Variation\\Model\\OptionValue'))->shouldBeCalled(); $productVariable->addVariant($permutationVariant)->shouldBeCalled(); $this->generate($productVariable); }
/** * Returns cart item form view. * * @param array $options * * @return FormView */ public function getItemFormView(array $options = array()) { $form = $this->formFactory->create('sylius_cart_item', $this->cartItemRepository->createNew(), $options); return $form->createView(); }