|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file has been created by developers from BitBag. |
| 5 | + * Feel free to contact us once you face any issues or want to start |
| 6 | + * You can find more information about us on https://bitbag.io and write us |
| 7 | + * an email on hello@bitbag.io. |
| 8 | + */ |
| 9 | + |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace BitBag\SyliusProductBundlePlugin\Fixture\Factory; |
| 13 | + |
| 14 | +use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleInterface; |
| 15 | +use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; |
| 16 | +use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface; |
| 17 | +use Sylius\Component\Core\Model\ProductVariantInterface; |
| 18 | +use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
| 19 | +use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
| 20 | +use Sylius\Component\Resource\Factory\FactoryInterface; |
| 21 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 22 | + |
| 23 | +final class ProductBundleFixtureFactory implements ExampleFactoryInterface |
| 24 | +{ |
| 25 | + private readonly OptionsResolver $optionsResolver; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private FactoryInterface $productBundleFactory, |
| 29 | + private FactoryInterface $productBundleItemFactory, |
| 30 | + private ProductRepositoryInterface $productRepository, |
| 31 | + private ProductVariantRepositoryInterface $productVariantRepository, |
| 32 | + ) { |
| 33 | + $this->optionsResolver = new OptionsResolver(); |
| 34 | + $this->configureOptions($this->optionsResolver); |
| 35 | + } |
| 36 | + |
| 37 | + private function configureOptions(OptionsResolver $resolver): void |
| 38 | + { |
| 39 | + $resolver |
| 40 | + ->setDefault('bundle', '') |
| 41 | + ->setAllowedTypes('bundle', 'string') |
| 42 | + ->setDefault('items', []) |
| 43 | + ->setAllowedTypes('items', 'array') |
| 44 | + ->setDefault('is_packed', '') |
| 45 | + ->setAllowedTypes('is_packed', 'bool') |
| 46 | + ; |
| 47 | + } |
| 48 | + |
| 49 | + public function create(array $options = []): ProductBundleInterface |
| 50 | + { |
| 51 | + $options = $this->optionsResolver->resolve($options); |
| 52 | + |
| 53 | + $bundleProduct = $this->productRepository->findOneByCode($options['bundle']); |
| 54 | + /** @var ProductBundleInterface $productBundle */ |
| 55 | + $productBundle = $this->productBundleFactory->createNew(); |
| 56 | + $productBundle->setProduct($bundleProduct); |
| 57 | + $productBundle->setIsPackedProduct($options['is_packed']); |
| 58 | + |
| 59 | + foreach ($options['items'] ?? [] as $item) { |
| 60 | + /** @var ProductVariantInterface $productVariant */ |
| 61 | + $productVariant = $this->productVariantRepository->findOneBy(['code' => $item]); |
| 62 | + /** @var ProductBundleItemInterface $bundleItem */ |
| 63 | + $bundleItem = $this->productBundleItemFactory->createNew(); |
| 64 | + $bundleItem->setProductVariant($productVariant); |
| 65 | + $bundleItem->setQuantity(1); |
| 66 | + $bundleItem->setProductBundle($productBundle); |
| 67 | + $productBundle->addProductBundleItem($bundleItem); |
| 68 | + } |
| 69 | + |
| 70 | + return $productBundle; |
| 71 | + } |
| 72 | +} |
0 commit comments