Completed
Pull Request — master (#95)
by
unknown
01:20
created

BlockFixtureFactory::load()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
rs 8.8571
cc 6
eloc 11
nc 5
nop 1
1
<?php
2
3
/**
4
 * This file was created by the developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\Fixture\Factory;
14
15
use BitBag\SyliusCmsPlugin\Entity\BlockImage;
16
use BitBag\SyliusCmsPlugin\Entity\BlockInterface;
17
use BitBag\SyliusCmsPlugin\Entity\BlockTranslationInterface;
18
use BitBag\SyliusCmsPlugin\Entity\SectionInterface;
19
use BitBag\SyliusCmsPlugin\Factory\BlockFactoryInterface;
20
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface;
21
use BitBag\SyliusCmsPlugin\Repository\SectionRepositoryInterface;
22
use Sylius\Component\Channel\Context\ChannelContextInterface;
23
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
24
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
25
use Sylius\Component\Locale\Context\LocaleContextInterface;
26
use Sylius\Component\Resource\Factory\FactoryInterface;
27
use Symfony\Component\HttpFoundation\File\UploadedFile;
28
29
/**
30
 * @author Mikołaj Król <[email protected]>
31
 */
32
final class BlockFixtureFactory implements FixtureFactoryInterface
33
{
34
    /**
35
     * @var BlockFactoryInterface
36
     */
37
    private $blockFactory;
38
39
    /**
40
     * @var FactoryInterface
41
     */
42
    private $blockTranslationFactory;
43
44
    /**
45
     * @var BlockRepositoryInterface
46
     */
47
    private $blockRepository;
48
49
    /**
50
     * @var ImageUploaderInterface
51
     */
52
    private $imageUploader;
53
54
    /**
55
     * @var ProductRepositoryInterface
56
     */
57
    private $productRepository;
58
59
    /**
60
     * @var SectionRepositoryInterface
61
     */
62
    private $sectionRepository;
63
64
    /**
65
     * @var ChannelContextInterface
66
     */
67
    private $channelContext;
68
69
    /**
70
     * @var LocaleContextInterface
71
     */
72
    private $localeContext;
73
74
    /**
75
     * @param BlockFactoryInterface $blockFactory
76
     * @param FactoryInterface $blockTranslationFactory
77
     * @param BlockRepositoryInterface $blockRepository
78
     * @param ImageUploaderInterface $imageUploader
79
     * @param ProductRepositoryInterface $productRepository
80
     * @param SectionRepositoryInterface $sectionRepository
81
     * @param ChannelContextInterface $channelContext
82
     * @param LocaleContextInterface $localeContext
83
     */
84
    public function __construct(
85
        BlockFactoryInterface $blockFactory,
86
        FactoryInterface $blockTranslationFactory,
87
        BlockRepositoryInterface $blockRepository,
88
        ImageUploaderInterface $imageUploader,
89
        ProductRepositoryInterface $productRepository,
90
        SectionRepositoryInterface $sectionRepository,
91
        ChannelContextInterface $channelContext,
92
        LocaleContextInterface $localeContext
93
    )
94
    {
95
        $this->blockFactory = $blockFactory;
96
        $this->blockTranslationFactory = $blockTranslationFactory;
97
        $this->blockRepository = $blockRepository;
98
        $this->imageUploader = $imageUploader;
99
        $this->productRepository = $productRepository;
100
        $this->sectionRepository = $sectionRepository;
101
        $this->channelContext = $channelContext;
102
        $this->localeContext = $localeContext;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function load(array $data): void
109
    {
110
        foreach ($data as $code => $fields) {
111
            if (
112
                true === $fields['remove_existing'] &&
113
                null !== $block = $this->blockRepository->findOneBy(['code' => $code])
114
            ) {
115
                $this->blockRepository->remove($block);
116
            }
117
118
            if (null !== $fields['number']) {
119
                for ($i = 0; $i < $fields['number']; $i++) {
120
                    $this->createBlock(md5(uniqid()), $fields);
121
                }
122
            } else {
123
                $this->createBlock($code, $fields);
124
            }
125
        }
126
    }
127
128
    private function createBlock(string $code, array $blockData): void
129
    {
130
        $type = $blockData['type'];
131
        $block = $this->blockFactory->createWithType($type);
132
        $products = $blockData['products'];
133
134
        if (null !== $products) {
135
            $this->resolveProducts($block, $products);
136
        }
137
138
        $this->resolveSections($block, $blockData['sections']);
139
140
        $block->setCode($code);
141
        $block->setEnabled($blockData['enabled']);
142
143
        foreach ($blockData['translations'] as $localeCode => $translation) {
144
            /** @var BlockTranslationInterface $blockTranslation */
145
            $blockTranslation = $this->blockTranslationFactory->createNew();
146
147
            $blockTranslation->setLocale($localeCode);
148
            $blockTranslation->setName($translation['name']);
149
            $blockTranslation->setContent($translation['content']);
150
            $blockTranslation->setLink($translation['link']);
151
152
            if (BlockInterface::IMAGE_BLOCK_TYPE === $type) {
153
                $image = new BlockImage();
154
                $path = $translation['image_path'];
155
                $uploadedImage = new UploadedFile($path, md5($path) . '.jpg');
156
157
                $image->setFile($uploadedImage);
158
                $blockTranslation->setImage($image);
159
160
                $this->imageUploader->upload($image);
161
            }
162
163
            $block->addTranslation($blockTranslation);
164
        }
165
166
        $this->blockRepository->add($block);
167
    }
168
169
170
    /**
171
     * @param BlockInterface $block
172
     * @param int $limit
173
     */
174
    private function resolveProducts(BlockInterface $block, int $limit): void
175
    {
176
        $products = $this->productRepository->findLatestByChannel(
177
            $this->channelContext->getChannel(),
178
            $this->localeContext->getLocaleCode(),
179
            $limit
180
        );
181
182
        foreach ($products as $product) {
183
            $block->addProduct($product);
184
        }
185
    }
186
187
    /**
188
     * @param BlockInterface $block
189
     * @param array $sections
190
     */
191
    private function resolveSections(BlockInterface $block, array $sections): void
192
    {
193
        foreach ($sections as $sectionCode) {
194
            /** @var SectionInterface $section */
195
            $section = $this->sectionRepository->findOneBy(['code' => $sectionCode]);
196
197
            $block->addSection($section);
198
        }
199
    }
200
}
201