vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Fixture/MugProductFixture.php line 19

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\CoreBundle\Fixture;
  12. use Faker\Factory;
  13. use Faker\Generator;
  14. @trigger_error('The "MugProductFixture" class is deprecated since Sylius 1.5 Use new product fixtures class located at "src/Sylius/Bundle/CoreBundle/Fixture/" instead.'\E_USER_DEPRECATED);
  15. use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
  16. use Sylius\Component\Attribute\AttributeType\SelectAttributeType;
  17. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. class MugProductFixture extends AbstractFixture
  20. {
  21.     private Generator $faker;
  22.     private OptionsResolver $optionsResolver;
  23.     public function __construct(
  24.         private AbstractResourceFixture $taxonFixture,
  25.         private AbstractResourceFixture $productAttributeFixture,
  26.         private AbstractResourceFixture $productOptionFixture,
  27.         private AbstractResourceFixture $productFixture,
  28.         private string $baseLocaleCode,
  29.     ) {
  30.         $this->faker Factory::create();
  31.         $this->optionsResolver =
  32.             (new OptionsResolver())
  33.                 ->setRequired('amount')
  34.                 ->setAllowedTypes('amount''int')
  35.         ;
  36.     }
  37.     public function getName(): string
  38.     {
  39.         return 'mug_product';
  40.     }
  41.     public function load(array $options): void
  42.     {
  43.         $options $this->optionsResolver->resolve($options);
  44.         $this->taxonFixture->load(['custom' => [[
  45.             'code' => 'category',
  46.             'name' => 'Category',
  47.             'children' => [
  48.                 [
  49.                     'code' => 'mugs',
  50.                     'translations' => [
  51.                         'en_US' => [
  52.                             'name' => 'Mugs',
  53.                         ],
  54.                         'fr_FR' => [
  55.                             'name' => 'Tasses',
  56.                         ],
  57.                     ],
  58.                 ],
  59.             ],
  60.         ]]]);
  61.         $mugMaterials = [
  62.             $this->faker->uuid => [$this->baseLocaleCode => 'Invisible porcelain'],
  63.             $this->faker->uuid => [$this->baseLocaleCode => 'Banana skin'],
  64.             $this->faker->uuid => [$this->baseLocaleCode => 'Porcelain'],
  65.             $this->faker->uuid => [$this->baseLocaleCode => 'Centipede'],
  66.         ];
  67.         $this->productAttributeFixture->load(['custom' => [
  68.             [
  69.                 'name' => 'Mug material',
  70.                 'code' => 'mug_material',
  71.                 'type' => SelectAttributeType::TYPE,
  72.                 'configuration' => [
  73.                     'multiple' => false,
  74.                     'choices' => $mugMaterials,
  75.                 ],
  76.             ],
  77.         ]]);
  78.         $this->productOptionFixture->load(['custom' => [
  79.             [
  80.                 'name' => 'Mug type',
  81.                 'code' => 'mug_type',
  82.                 'values' => [
  83.                     'mug_type_medium' => 'Medium mug',
  84.                     'mug_type_double' => 'Double mug',
  85.                     'mug_type_monster' => 'Monster mug',
  86.                 ],
  87.             ],
  88.         ]]);
  89.         $products = [];
  90.         $productsNames $this->getUniqueNames($options['amount']);
  91.         for ($i 0$i $options['amount']; ++$i) {
  92.             $products[] = [
  93.                 'name' => sprintf('Mug "%s"'$productsNames[$i]),
  94.                 'code' => $this->faker->uuid,
  95.                 'main_taxon' => 'mugs',
  96.                 'taxons' => ['mugs'],
  97.                 'product_attributes' => [
  98.                     'mug_material' => [$this->faker->randomKey($mugMaterials)],
  99.                 ],
  100.                 'product_options' => ['mug_type'],
  101.                 'images' => [
  102.                     [
  103.                         'path' => sprintf('%s/../Resources/fixtures/%s'__DIR__'mugs.jpg'),
  104.                         'type' => 'main',
  105.                     ],
  106.                     [
  107.                         'path' => sprintf('%s/../Resources/fixtures/%s'__DIR__'mugs.jpg'),
  108.                         'type' => 'thumbnail',
  109.                     ],
  110.                 ],
  111.             ];
  112.         }
  113.         $this->productFixture->load(['custom' => $products]);
  114.     }
  115.     protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
  116.     {
  117.         $optionsNode
  118.             ->children()
  119.                 ->integerNode('amount')->isRequired()->min(0)->end()
  120.         ;
  121.     }
  122.     private function getUniqueNames(int $amount): array
  123.     {
  124.         $productsNames = [];
  125.         for ($i 0$i $amount; ++$i) {
  126.             $name $this->faker->word;
  127.             while (in_array($name$productsNames)) {
  128.                 $name $this->faker->word;
  129.             }
  130.             $productsNames[] = $name;
  131.         }
  132.         return $productsNames;
  133.     }
  134. }