Passed
Pull Request — master (#35)
by Igor
03:31
created

BrandExampleFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 5
dl 0
loc 17
rs 10
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
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function configureOptions(OptionsResolver $resolver): void
70
    {
71
        $resolver
72
            ->setRequired('name')
73
            ->setAllowedTypes('name', 'string')
74
            ->setRequired('slug')
75
            ->setAllowedTypes('slug', 'string')
76
77
            ->setDefault('images', [])
78
            ->setAllowedTypes('images', 'array')
79
80
            ->setDefault('products', [])
81
            ->setAllowedTypes('products', 'array')
82
            ->setNormalizer('products', LazyOption::findBy($this->productRepository, 'code'))
83
        ;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function create(array $options = []): BrandInterface
90
    {
91
        $options = $this->optionsResolver->resolve($options);
92
93
        /** @var BrandInterface $brand */
94
        $brand = $this->brandFactory->createNew();
95
        $brand->setName($options['name']);
96
        $brand->setSlug($options['slug']);
97
98
        $this->createImages($brand, $options);
99
100
        $this->productAssigner->assign($brand, $options['products']);
101
102
        return $brand;
103
    }
104
105
    /**
106
     * @param BrandInterface $brand
107
     * @param array $options
108
     */
109
    private function createImages(BrandInterface $brand, array $options): void
110
    {
111
        foreach ($options['images'] as $image) {
112
            $imagePath = $image['path'];
113
            $imageType = $image['type'] ?? null;
114
115
            $uploadedImage = new UploadedFile($imagePath, basename($imagePath));
116
117
            /** @var BrandImageInterface $brandImage */
118
            $brandImage = $this->productImageFactory->createNew();
119
            $brandImage->setFile($uploadedImage);
120
            $brandImage->setType($imageType);
121
122
            $this->imageUploader->upload($brandImage);
123
124
            $brand->addImage($brandImage);
125
        }
126
    }
127
128
}
129