|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Loevgaard\SyliusBrandPlugin\Fixture\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use Loevgaard\SyliusBrandPlugin\Doctrine\ORM\BrandRepositoryInterface; |
|
8
|
|
|
use Loevgaard\SyliusBrandPlugin\Model\BrandAwareInterface; |
|
9
|
|
|
use Loevgaard\SyliusBrandPlugin\Model\BrandInterface; |
|
10
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; |
|
11
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
12
|
|
|
use Webmozart\Assert\Assert; |
|
13
|
|
|
|
|
14
|
|
|
trait BrandsAwareExampleFactoryTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var BrandRepositoryInterface */ |
|
17
|
|
|
protected $brandRepository; |
|
18
|
|
|
|
|
19
|
|
|
/** @var \Faker\Generator */ |
|
20
|
|
|
protected $faker; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(BrandRepositoryInterface $brandRepository) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->brandRepository = $brandRepository; |
|
25
|
|
|
if (null === $this->faker) { |
|
26
|
|
|
$this->faker = \Faker\Factory::create(); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function configureBrandsOptions(OptionsResolver $resolver, int $amount = 10): void |
|
31
|
|
|
{ |
|
32
|
|
|
$resolver |
|
33
|
|
|
->setDefault('brands', LazyOption::randomOnes($this->brandRepository, $amount)) |
|
34
|
|
|
->setAllowedTypes('brands', ['array']) |
|
35
|
|
|
->setNormalizer('brands', LazyOption::findBy($this->brandRepository, 'code')) |
|
36
|
|
|
; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function setBrandField(BrandAwareInterface $brandAware, array $resolvedOptions = []): void |
|
40
|
|
|
{ |
|
41
|
|
|
$brand = $this->faker->randomElement($resolvedOptions['brands']); |
|
42
|
|
|
Assert::isInstanceOf($brand, BrandInterface::class); |
|
43
|
|
|
|
|
44
|
|
|
$brandAware->setBrand($brand); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|