Last active
November 17, 2020 12:26
-
-
Save vvasiloi/a3a8a7b151775bb58cd122a47b2cc4dd to your computer and use it in GitHub Desktop.
Extract options when decorating an example factory. See example usage in ProductExampleFactory.php. #sylius #fixtures #example #factory
This file contains hidden or 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
<?php | |
namespace App\Fixture\Factory; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
trait ExampleFactoryDecoratorTrait | |
{ | |
/** | |
* @param array $options | |
* @param OptionsResolver $resolver | |
* @return array | |
*/ | |
private function extractOptions(array $options, OptionsResolver $resolver): array | |
{ | |
$optionsToExtract = array_fill_keys($resolver->getDefinedOptions(), null); | |
$extractedOptions = array_intersect_key($options, $optionsToExtract); | |
$remainingOptions = array_diff_key($options, $optionsToExtract); | |
return [$resolver->resolve($extractedOptions), $remainingOptions]; | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\Fixture\Factory; | |
use App\Entity\Product\Product; | |
use App\Entity\Product\ProductVariant; | |
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; | |
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface; | |
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; | |
use Sylius\Component\Core\Model\ProductInterface; | |
use Sylius\Component\Taxation\Model\TaxCategoryInterface; | |
use Sylius\Component\Taxation\Repository\TaxCategoryRepositoryInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
final class ProductExampleFactory extends AbstractExampleFactory | |
{ | |
use ExampleFactoryDecoratorTrait; | |
/** | |
* @var ExampleFactoryInterface | |
*/ | |
private $factory; | |
/** | |
* @var \Faker\Generator | |
*/ | |
private $faker; | |
/** | |
* @var OptionsResolver | |
*/ | |
private $optionsResolver; | |
/** | |
* @var TaxCategoryRepositoryInterface | |
*/ | |
private $taxCategoryRepository; | |
/** | |
* @param ExampleFactoryInterface $factory | |
* @param TaxCategoryRepositoryInterface $taxCategoryRepository | |
*/ | |
public function __construct(ExampleFactoryInterface $factory, TaxCategoryRepositoryInterface $taxCategoryRepository) | |
{ | |
$this->factory = $factory; | |
$this->taxCategoryRepository = $taxCategoryRepository; | |
$this->faker = \Faker\Factory::create(); | |
$this->optionsResolver = new OptionsResolver(); | |
$this->configureOptions($this->optionsResolver); | |
} | |
/** | |
* @param OptionsResolver $resolver | |
*/ | |
protected function configureOptions(OptionsResolver $resolver): void | |
{ | |
$resolver | |
->setDefault('tax_category', LazyOption::randomOne($this->taxCategoryRepository)) | |
->setAllowedTypes('tax_category', ['null', 'string', TaxCategoryInterface::class]) | |
->setNormalizer('tax_category', LazyOption::findOneBy($this->taxCategoryRepository, 'code')) | |
; | |
} | |
/** | |
* @param array $options | |
* @return ProductInterface | |
*/ | |
public function create(array $options = []): ProductInterface | |
{ | |
[$extracted, $remaining] = $this->extractOptions($options, $this->optionsResolver); | |
/** @var Product $product */ | |
$product = $this->factory->create($remaining); | |
/** @var ProductVariant $variant */ | |
foreach ($product->getVariants() as $variant) { | |
$variant->setTaxCategory($extracted['tax_category']); | |
} | |
return $product; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment