Passed
Pull Request — master (#30)
by
unknown
04:17
created

BrandExampleFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 12 1
A __construct() 0 12 1
A configureOptions() 0 10 1
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace Loevgaard\SyliusBrandPlugin\Fixture\Factory;
14
15
use Loevgaard\SyliusBrandPlugin\Assigner\ProductsAssignerInterface;
16
use Loevgaard\SyliusBrandPlugin\Entity\BrandInterface;
17
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory;
18
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
19
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
20
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Symfony\Component\OptionsResolver\OptionsResolver;
23
24
final class BrandExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
25
{
26
    /** @var OptionsResolver */
27
    private $optionsResolver;
28
29
    /** @var ProductRepositoryInterface */
30
    private $productRepository;
31
32
    /** @var ProductsAssignerInterface */
33
    private $productAssigner;
34
35
    /** @var FactoryInterface */
36
    protected $brandFactory;
37
38
    /**
39
     * @param ProductRepositoryInterface $productRepository
40
     * @param ProductsAssignerInterface $productAssigner
41
     * @param FactoryInterface $brandFactory
42
     */
43
    public function __construct(
44
        ProductRepositoryInterface $productRepository,
45
        ProductsAssignerInterface $productAssigner,
46
        FactoryInterface $brandFactory)
47
    {
48
        $this->productRepository = $productRepository;
49
        $this->productAssigner = $productAssigner;
50
        $this->brandFactory = $brandFactory;
51
52
        $this->optionsResolver = new OptionsResolver();
53
54
        $this->configureOptions($this->optionsResolver);
55
    }
56
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function configureOptions(OptionsResolver $resolver): void
62
    {
63
        $resolver
64
            ->setRequired('name')
65
            ->setAllowedTypes('name', 'string')
66
            ->setRequired('slug')
67
            ->setAllowedTypes('slug', 'string')
68
            ->setDefault('products', [])
69
            ->setAllowedTypes('products', 'array')
70
            ->setNormalizer('products', LazyOption::findBy($this->productRepository, 'code'))
71
        ;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function create(array $options = []): BrandInterface
78
    {
79
        $options = $this->optionsResolver->resolve($options);
80
81
        /** @var BrandInterface $brand */
82
        $brand = $this->brandFactory->createNew();
83
        $brand->setName($options['name']);
84
        $brand->setSlug($options['slug']);
85
86
        $this->productAssigner->assign($brand, $options['products']);
87
88
        return $brand;
89
    }
90
}
91