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