vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Fixture/BookProductFixture.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 "BookProductFixture" 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\IntegerAttributeType;
  17. use Sylius\Component\Attribute\AttributeType\SelectAttributeType;
  18. use Sylius\Component\Attribute\AttributeType\TextAttributeType;
  19. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  20. use Symfony\Component\OptionsResolver\OptionsResolver;
  21. class BookProductFixture extends AbstractFixture
  22. {
  23.     private Generator $faker;
  24.     private OptionsResolver $optionsResolver;
  25.     public function __construct(
  26.         private AbstractResourceFixture $taxonFixture,
  27.         private AbstractResourceFixture $productAttributeFixture,
  28.         private AbstractResourceFixture $productFixture,
  29.         private string $baseLocaleCode,
  30.     ) {
  31.         $this->faker Factory::create();
  32.         $this->optionsResolver =
  33.             (new OptionsResolver())
  34.                 ->setRequired('amount')
  35.                 ->setAllowedTypes('amount''int')
  36.         ;
  37.     }
  38.     public function getName(): string
  39.     {
  40.         return 'book_product';
  41.     }
  42.     public function load(array $options): void
  43.     {
  44.         $options $this->optionsResolver->resolve($options);
  45.         $this->taxonFixture->load(['custom' => [[
  46.             'code' => 'category',
  47.             'name' => 'Category',
  48.             'children' => [
  49.                 [
  50.                     'code' => 'books',
  51.                     'translations' => [
  52.                         'en_US' => [
  53.                             'name' => 'Books',
  54.                         ],
  55.                         'fr_FR' => [
  56.                             'name' => 'Livres',
  57.                         ],
  58.                     ],
  59.                 ],
  60.             ],
  61.         ]]]);
  62.         $bookGenres = [
  63.             $this->faker->uuid => [$this->baseLocaleCode => 'Science Fiction'],
  64.             $this->faker->uuid => [$this->baseLocaleCode => 'Romance'],
  65.             $this->faker->uuid => [$this->baseLocaleCode => 'Thriller'],
  66.             $this->faker->uuid => [$this->baseLocaleCode => 'Sports'],
  67.         ];
  68.         $this->productAttributeFixture->load(['custom' => [
  69.             ['name' => 'Book author''code' => 'book_author''type' => TextAttributeType::TYPE],
  70.             ['name' => 'Book ISBN''code' => 'book_isbn''type' => TextAttributeType::TYPE],
  71.             ['name' => 'Book pages''code' => 'book_pages''type' => IntegerAttributeType::TYPE],
  72.             [
  73.                 'name' => 'Book genre',
  74.                 'code' => 'book_genre',
  75.                 'type' => SelectAttributeType::TYPE,
  76.                 'configuration' => [
  77.                     'multiple' => true,
  78.                     'choices' => $bookGenres,
  79.                 ],
  80.             ],
  81.         ]]);
  82.         $products = [];
  83.         $productsNames $this->getUniqueNames($options['amount']);
  84.         for ($i 0$i $options['amount']; ++$i) {
  85.             $authorName $this->faker->name;
  86.             $products[] = [
  87.                 'name' => sprintf('Book "%s" by %s'$productsNames[$i], $authorName),
  88.                 'code' => $this->faker->uuid,
  89.                 'main_taxon' => 'books',
  90.                 'taxons' => ['books'],
  91.                 'product_attributes' => [
  92.                     'book_author' => $authorName,
  93.                     'book_isbn' => $this->faker->isbn13(),
  94.                     'book_pages' => $this->faker->numberBetween(421024),
  95.                     'book_genre' => $this->faker->randomElements(array_keys($bookGenres), $this->faker->numberBetween(1count($bookGenres))),
  96.                 ],
  97.                 'images' => [
  98.                     [
  99.                         'path' => sprintf('%s/../Resources/fixtures/%s'__DIR__'books.jpg'),
  100.                         'type' => 'main',
  101.                     ],
  102.                     [
  103.                         'path' => sprintf('%s/../Resources/fixtures/%s'__DIR__'books.jpg'),
  104.                         'type' => 'thumbnail',
  105.                     ],
  106.                 ],
  107.             ];
  108.         }
  109.         $this->productFixture->load(['custom' => $products]);
  110.     }
  111.     protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
  112.     {
  113.         $optionsNode
  114.             ->children()
  115.                 ->integerNode('amount')->isRequired()->min(0)->end()
  116.         ;
  117.     }
  118.     private function getUniqueNames(int $amount): array
  119.     {
  120.         $productsNames = [];
  121.         for ($i 0$i $amount; ++$i) {
  122.             $name $this->faker->word;
  123.             while (in_array($name$productsNames)) {
  124.                 $name $this->faker->word;
  125.             }
  126.             $productsNames[] = $name;
  127.         }
  128.         return $productsNames;
  129.     }
  130. }