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

BrandAwareExampleFactoryTrait::setBrandField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
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
13
trait BrandAwareExampleFactoryTrait
14
{
15
    /** @var BrandRepositoryInterface */
16
    protected $brandRepository;
17
18
    public function __construct(BrandRepositoryInterface $brandRepository)
19
    {
20
        $this->brandRepository = $brandRepository;
21
    }
22
23
    protected function configureBrandOptions(OptionsResolver $resolver, int $chanceOfRandomBrand = 90): void
24
    {
25
        $resolver
26
            ->setDefault('brand', LazyOption::randomOneOrNull($this->brandRepository, $chanceOfRandomBrand))
27
            ->setAllowedTypes('brand', ['null', 'string', BrandInterface::class])
28
            ->setNormalizer('brand', LazyOption::findOneBy($this->brandRepository, 'code'))
29
        ;
30
    }
31
32
    protected function setBrandField(BrandAwareInterface $brandAware, array $resolvedOptions = []): void
33
    {
34
        $brandAware->setBrand($resolvedOptions['brand']);
35
    }
36
}
37