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\Model\BrandImageInterface; |
9
|
|
|
use Loevgaard\SyliusBrandPlugin\Model\BrandInterface; |
10
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; |
11
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; |
12
|
|
|
use Sylius\Component\Core\Repository\ProductRepositoryInterface; |
13
|
|
|
use Sylius\Component\Core\Uploader\ImageUploaderInterface; |
14
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
16
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
17
|
|
|
|
18
|
|
|
final class BrandExampleFactory extends AbstractExampleFactory |
19
|
|
|
{ |
20
|
|
|
/** @var OptionsResolver */ |
21
|
|
|
private $optionsResolver; |
22
|
|
|
|
23
|
|
|
/** @var ProductRepositoryInterface */ |
24
|
|
|
private $productRepository; |
25
|
|
|
|
26
|
|
|
/** @var ProductsAssignerInterface */ |
27
|
|
|
private $productAssigner; |
28
|
|
|
|
29
|
|
|
/** @var FactoryInterface */ |
30
|
|
|
protected $brandFactory; |
31
|
|
|
|
32
|
|
|
/** @var FactoryInterface */ |
33
|
|
|
protected $productImageFactory; |
34
|
|
|
|
35
|
|
|
/** @var ImageUploaderInterface */ |
36
|
|
|
private $imageUploader; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ProductRepositoryInterface $productRepository |
40
|
|
|
* @param ProductsAssignerInterface $productAssigner |
41
|
|
|
* @param FactoryInterface $brandFactory |
42
|
|
|
* @param FactoryInterface $productImageFactory |
43
|
|
|
* @param ImageUploaderInterface $imageUploader |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
ProductRepositoryInterface $productRepository, |
47
|
|
|
ProductsAssignerInterface $productAssigner, |
48
|
|
|
FactoryInterface $brandFactory, |
49
|
|
|
FactoryInterface $productImageFactory, |
50
|
|
|
ImageUploaderInterface $imageUploader |
51
|
|
|
) { |
52
|
|
|
$this->productRepository = $productRepository; |
53
|
|
|
$this->productAssigner = $productAssigner; |
54
|
|
|
$this->brandFactory = $brandFactory; |
55
|
|
|
|
56
|
|
|
$this->productImageFactory = $productImageFactory; |
57
|
|
|
$this->imageUploader = $imageUploader; |
58
|
|
|
|
59
|
|
|
$this->optionsResolver = new OptionsResolver(); |
60
|
|
|
|
61
|
|
|
$this->configureOptions($this->optionsResolver); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
68
|
|
|
{ |
69
|
|
|
$resolver |
70
|
|
|
->setRequired('name') |
71
|
|
|
->setAllowedTypes('name', 'string') |
72
|
|
|
->setRequired('code') |
73
|
|
|
->setAllowedTypes('code', 'string') |
74
|
|
|
|
75
|
|
|
->setDefault('images', []) |
76
|
|
|
->setAllowedTypes('images', 'array') |
77
|
|
|
|
78
|
|
|
->setDefault('products', []) |
79
|
|
|
->setAllowedTypes('products', 'array') |
80
|
|
|
->setNormalizer('products', LazyOption::findBy($this->productRepository, 'code')) |
81
|
|
|
; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function create(array $options = []): BrandInterface |
88
|
|
|
{ |
89
|
|
|
$options = $this->optionsResolver->resolve($options); |
90
|
|
|
|
91
|
|
|
/** @var BrandInterface $brand */ |
92
|
|
|
$brand = $this->brandFactory->createNew(); |
93
|
|
|
$brand->setName($options['name']); |
94
|
|
|
$brand->setCode($options['code']); |
95
|
|
|
|
96
|
|
|
$this->createImages($brand, $options); |
97
|
|
|
|
98
|
|
|
$this->productAssigner->assign($brand, $options['products']); |
99
|
|
|
|
100
|
|
|
return $brand; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param BrandInterface $brand |
105
|
|
|
* @param array $options |
106
|
|
|
*/ |
107
|
|
|
private function createImages(BrandInterface $brand, array $options): void |
108
|
|
|
{ |
109
|
|
|
foreach ($options['images'] as $image) { |
110
|
|
|
$imagePath = $image['path']; |
111
|
|
|
$imageType = $image['type'] ?? null; |
112
|
|
|
|
113
|
|
|
$uploadedImage = new UploadedFile($imagePath, basename($imagePath)); |
114
|
|
|
|
115
|
|
|
/** @var BrandImageInterface $brandImage */ |
116
|
|
|
$brandImage = $this->productImageFactory->createNew(); |
117
|
|
|
$brandImage->setFile($uploadedImage); |
118
|
|
|
$brandImage->setType($imageType); |
119
|
|
|
|
120
|
|
|
$this->imageUploader->upload($brandImage); |
121
|
|
|
|
122
|
|
|
$brand->addImage($brandImage); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|