1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Loevgaard\SyliusBrandPlugin\Fixture\Factory; |
6
|
|
|
|
7
|
|
|
use Loevgaard\SyliusBrandPlugin\Assigner\ProductsAssignerInterface; |
8
|
|
|
use Loevgaard\SyliusBrandPlugin\Entity\BrandInterface; |
9
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; |
10
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface; |
11
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; |
12
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
13
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
14
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
15
|
|
|
|
16
|
|
|
final class BrandExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface |
17
|
|
|
{ |
18
|
|
|
/** @var OptionsResolver */ |
19
|
|
|
private $optionsResolver; |
20
|
|
|
|
21
|
|
|
/** @var ProductRepositoryInterface */ |
22
|
|
|
private $productRepository; |
23
|
|
|
|
24
|
|
|
/** @var ProductsAssignerInterface */ |
25
|
|
|
private $productAssigner; |
26
|
|
|
|
27
|
|
|
/** @var FactoryInterface */ |
28
|
|
|
protected $brandFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ProductRepositoryInterface $productRepository |
32
|
|
|
* @param ProductsAssignerInterface $productAssigner |
33
|
|
|
* @param FactoryInterface $brandFactory |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
ProductRepositoryInterface $productRepository, |
37
|
|
|
ProductsAssignerInterface $productAssigner, |
38
|
|
|
FactoryInterface $brandFactory) |
39
|
|
|
{ |
40
|
|
|
$this->productRepository = $productRepository; |
41
|
|
|
$this->productAssigner = $productAssigner; |
42
|
|
|
$this->brandFactory = $brandFactory; |
43
|
|
|
|
44
|
|
|
$this->optionsResolver = new OptionsResolver(); |
45
|
|
|
|
46
|
|
|
$this->configureOptions($this->optionsResolver); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
54
|
|
|
{ |
55
|
|
|
$resolver |
56
|
|
|
->setRequired('name') |
57
|
|
|
->setAllowedTypes('name', 'string') |
58
|
|
|
->setRequired('slug') |
59
|
|
|
->setAllowedTypes('slug', 'string') |
60
|
|
|
->setDefault('products', []) |
61
|
|
|
->setAllowedTypes('products', 'array') |
62
|
|
|
->setNormalizer('products', LazyOption::findBy($this->productRepository, 'code')) |
63
|
|
|
; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function create(array $options = []): BrandInterface |
70
|
|
|
{ |
71
|
|
|
$options = $this->optionsResolver->resolve($options); |
72
|
|
|
|
73
|
|
|
/** @var BrandInterface $brand */ |
74
|
|
|
$brand = $this->brandFactory->createNew(); |
75
|
|
|
$brand->setName($options['name']); |
76
|
|
|
$brand->setSlug($options['slug']); |
77
|
|
|
|
78
|
|
|
$this->productAssigner->assign($brand, $options['products']); |
79
|
|
|
|
80
|
|
|
return $brand; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|