Completed
Push — master ( 926d4d...6771bc )
by Igor
14s queued 11s
created

configureBrandsOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
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