BrandAwareExampleFactoryTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 22
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setBrandField() 0 3 1
A configureBrandOptions() 0 6 1
A __construct() 0 3 1
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