vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Fixture/StickerProductFixture.php line 16

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. @trigger_error('The "StickerProductFixture" class is deprecated since Sylius 1.5 Use new product fixtures class located at "src/Sylius/Bundle/CoreBundle/Fixture/" instead.'\E_USER_DEPRECATED);
  13. use Faker\Factory;
  14. use Faker\Generator;
  15. use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
  16. use Sylius\Component\Attribute\AttributeType\TextAttributeType;
  17. use Sylius\Component\Core\Model\ProductInterface;
  18. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. class StickerProductFixture extends AbstractFixture
  21. {
  22.     private Generator $faker;
  23.     private OptionsResolver $optionsResolver;
  24.     public function __construct(
  25.         private AbstractResourceFixture $taxonFixture,
  26.         private AbstractResourceFixture $productAttributeFixture,
  27.         private AbstractResourceFixture $productOptionFixture,
  28.         private AbstractResourceFixture $productFixture,
  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 'sticker_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' => 'stickers',
  50.                     'translations' => [
  51.                         'en_US' => [
  52.                             'name' => 'Stickers',
  53.                         ],
  54.                         'fr_FR' => [
  55.                             'name' => 'Étiquettes',
  56.                         ],
  57.                     ],
  58.                 ],
  59.             ],
  60.         ]]]);
  61.         $this->productAttributeFixture->load(['custom' => [
  62.             ['name' => 'Sticker paper''code' => 'sticker_paper''type' => TextAttributeType::TYPE],
  63.             ['name' => 'Sticker resolution''code' => 'sticker_resolution''type' => TextAttributeType::TYPE],
  64.         ]]);
  65.         $this->productOptionFixture->load(['custom' => [
  66.             [
  67.                 'name' => 'Sticker size',
  68.                 'code' => 'sticker_size',
  69.                 'values' => [
  70.                     'sticker_size_3' => '3"',
  71.                     'sticker_size_5' => '5"',
  72.                     'sticker_size_7' => '7"',
  73.                 ],
  74.             ],
  75.         ]]);
  76.         $products = [];
  77.         $productsNames $this->getUniqueNames($options['amount']);
  78.         for ($i 0$i $options['amount']; ++$i) {
  79.             $products[] = [
  80.                 'name' => sprintf('Sticker "%s"'$productsNames[$i]),
  81.                 'code' => $this->faker->uuid,
  82.                 'main_taxon' => 'stickers',
  83.                 'taxons' => ['stickers'],
  84.                 'variant_selection_method' => ProductInterface::VARIANT_SELECTION_CHOICE,
  85.                 'product_attributes' => [
  86.                     'sticker_paper' => sprintf('Paper from tree %s'$this->faker->randomElement(['Wung''Tanajno''Lemon-San''Me-Gusta'])),
  87.                     'sticker_resolution' => $this->faker->randomElement(['JKM XD''476DPI''FULL HD''200DPI']),
  88.                 ],
  89.                 'product_options' => ['sticker_size'],
  90.                 'images' => [
  91.                     [
  92.                         'path' => sprintf('%s/../Resources/fixtures/%s'__DIR__'stickers.jpg'),
  93.                         'type' => 'main',
  94.                     ],
  95.                     [
  96.                         'path' => sprintf('%s/../Resources/fixtures/%s'__DIR__'stickers.jpg'),
  97.                         'type' => 'thumbnail',
  98.                     ],
  99.                 ],
  100.             ];
  101.         }
  102.         $this->productFixture->load(['custom' => $products]);
  103.     }
  104.     protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
  105.     {
  106.         $optionsNode
  107.             ->children()
  108.                 ->integerNode('amount')->isRequired()->min(0)->end()
  109.         ;
  110.     }
  111.     private function getUniqueNames(int $amount): array
  112.     {
  113.         $productsNames = [];
  114.         for ($i 0$i $amount; ++$i) {
  115.             $name $this->faker->word;
  116.             while (in_array($name$productsNames)) {
  117.                 $name $this->faker->word;
  118.             }
  119.             $productsNames[] = $name;
  120.         }
  121.         return $productsNames;
  122.     }
  123. }