Completed
Pull Request — master (#34)
by Igor
04:25
created

BrandExampleFactory::createImages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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