-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* You can find more information about us on https://bitbag.io and write us | ||
* an email on [email protected]. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusProductBundlePlugin\Fixture\Factory; | ||
|
||
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleInterface; | ||
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; | ||
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
use Sylius\Component\Core\Repository\ProductRepositoryInterface; | ||
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; | ||
use Sylius\Component\Resource\Factory\FactoryInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
final class ProductBundleFixtureFactory implements ExampleFactoryInterface | ||
{ | ||
private readonly OptionsResolver $optionsResolver; | ||
|
||
public function __construct( | ||
private FactoryInterface $productBundleFactory, | ||
private FactoryInterface $productBundleItemFactory, | ||
private ProductRepositoryInterface $productRepository, | ||
private ProductVariantRepositoryInterface $productVariantRepository, | ||
) { | ||
$this->optionsResolver = new OptionsResolver(); | ||
$this->configureOptions($this->optionsResolver); | ||
} | ||
|
||
private function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver | ||
->setDefault('bundle', '') | ||
->setAllowedTypes('bundle', 'string') | ||
->setDefault('items', []) | ||
->setAllowedTypes('items', 'array') | ||
->setDefault('is_packed', '') | ||
->setAllowedTypes('is_packed', 'bool') | ||
; | ||
} | ||
|
||
public function create(array $options = []): ProductBundleInterface | ||
{ | ||
$options = $this->optionsResolver->resolve($options); | ||
|
||
$bundleProduct = $this->productRepository->findOneByCode($options['bundle']); | ||
/** @var ProductBundleInterface $productBundle */ | ||
$productBundle = $this->productBundleFactory->createNew(); | ||
$productBundle->setProduct($bundleProduct); | ||
$productBundle->setIsPackedProduct($options['is_packed']); | ||
|
||
foreach ($options['items'] ?? [] as $item) { | ||
/** @var ProductVariantInterface $productVariant */ | ||
$productVariant = $this->productVariantRepository->findOneBy(['code' => $item]); | ||
/** @var ProductBundleItemInterface $bundleItem */ | ||
$bundleItem = $this->productBundleItemFactory->createNew(); | ||
$bundleItem->setProductVariant($productVariant); | ||
$bundleItem->setQuantity(1); | ||
$bundleItem->setProductBundle($productBundle); | ||
$productBundle->addProductBundleItem($bundleItem); | ||
} | ||
|
||
return $productBundle; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* You can find more information about us on https://bitbag.io and write us | ||
* an email on [email protected]. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusProductBundlePlugin\Fixture; | ||
|
||
use Sylius\Bundle\CoreBundle\Fixture\AbstractResourceFixture; | ||
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface; | ||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
|
||
final class ProductBundleFixture extends AbstractResourceFixture implements FixtureInterface | ||
{ | ||
protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void | ||
{ | ||
$resourceNodeChildren = $resourceNode->children(); | ||
$resourceNodeChildren->scalarNode('bundle')->end(); | ||
$resourceNodeChildren->arrayNode('items')->scalarPrototype()->end(); | ||
$resourceNodeChildren->booleanNode('is_packed')->end(); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'product_bundle'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="bitbag_sylius_product_bundle.fixture.product_bundle" | ||
class="BitBag\SyliusProductBundlePlugin\Fixture\ProductBundleFixture"> | ||
<argument type="service" id="bitbag_sylius_product_bundle.manager.product_bundle"/> | ||
<argument type="service" id="bitbag_sylius_product_bundle.fixture.factory.product_bundle"/> | ||
<tag name="sylius_fixtures.fixture"/> | ||
</service> | ||
<service id="bitbag_sylius_product_bundle.fixture.factory.product_bundle" | ||
class="BitBag\SyliusProductBundlePlugin\Fixture\Factory\ProductBundleFixtureFactory"> | ||
<argument type="service" id="bitbag_sylius_product_bundle.factory.product_bundle"/> | ||
<argument type="service" id="bitbag_sylius_product_bundle.factory.product_bundle_item"/> | ||
<argument type="service" id="sylius.repository.product"/> | ||
<argument type="service" id="sylius.repository.product_variant"/> | ||
</service> | ||
</services> | ||
</container> |
46 changes: 45 additions & 1 deletion
46
tests/Application/config/packages/bitbag_sylius_product_bundle_plugin.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,46 @@ | ||
imports: | ||
- { resource: "@BitBagSyliusProductBundlePlugin/Resources/config/config.yml" } | ||
- { resource: "@BitBagSyliusProductBundlePlugin/Resources/config/config.yml" } | ||
|
||
sylius_fixtures: | ||
suites: | ||
default: | ||
fixtures: | ||
tshirt_bundle_products: | ||
name: product | ||
options: | ||
custom: | ||
- name: 'Packed T-Shirt bundle' | ||
tax_category: 'clothing' | ||
channels: | ||
- 'FASHION_WEB' | ||
main_taxon: 'mens_t_shirts' | ||
taxons: | ||
- 't_shirts' | ||
- 'mens_t_shirts' | ||
|
||
- name: 'Not packed T-Shirt bundle' | ||
tax_category: 'clothing' | ||
channels: | ||
- 'FASHION_WEB' | ||
main_taxon: 'womens_t_shirts' | ||
taxons: | ||
- 't_shirts' | ||
- 'womens_t_shirts' | ||
|
||
tshirt_bundles: | ||
name: product_bundle | ||
options: | ||
custom: | ||
- bundle: 'Packed_T_Shirt_bundle' | ||
items: | ||
- 'Sport_basic_white_T_Shirt-variant-0' | ||
- 'Raglan_grey_&_black_Tee-variant-0' | ||
- 'Oversize_white_cotton_T_Shirt-variant-0' | ||
is_packed: true | ||
|
||
- bundle: 'Not_packed_T_Shirt_bundle' | ||
items: | ||
- 'Everyday_white_basic_T_Shirt-variant-0' | ||
- 'Loose_white_designer_T_Shirt-variant-0' | ||
- 'Ribbed_copper_slim_fit_Tee-variant-0' | ||
is_packed: false |