Completed
Pull Request — master (#83)
by Mikołaj
01:18
created

BlockFixtureFactory::createBlock()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 24
nc 6
nop 2
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\CmsPlugin\Fixture\Factory;
14
15
use BitBag\CmsPlugin\Entity\BlockImage;
16
use BitBag\CmsPlugin\Entity\BlockInterface;
17
use BitBag\CmsPlugin\Entity\BlockTranslationInterface;
18
use BitBag\CmsPlugin\Entity\SectionInterface;
19
use BitBag\CmsPlugin\Factory\BlockFactoryInterface;
20
use BitBag\CmsPlugin\Repository\BlockRepositoryInterface;
21
use BitBag\CmsPlugin\Repository\SectionRepositoryInterface;
22
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
23
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
24
use Sylius\Component\Resource\Factory\FactoryInterface;
25
use Symfony\Component\HttpFoundation\File\UploadedFile;
26
27
/**
28
 * @author Mikołaj Król <[email protected]>
29
 */
30
final class BlockFixtureFactory implements FixtureFactoryInterface
31
{
32
    /**
33
     * @var BlockFactoryInterface
34
     */
35
    private $blockFactory;
36
37
    /**
38
     * @var FactoryInterface
39
     */
40
    private $blockTranslationFactory;
41
42
    /**
43
     * @var BlockRepositoryInterface
44
     */
45
    private $blockRepository;
46
47
    /**
48
     * @var ImageUploaderInterface
49
     */
50
    private $imageUploader;
51
52
    /**
53
     * @var ProductRepositoryInterface
54
     */
55
    private $productRepository;
56
57
    /**
58
     * @var SectionRepositoryInterface
59
     */
60
    private $sectionRepository;
61
62
    /**
63
     * @param BlockFactoryInterface $blockFactory
64
     * @param FactoryInterface $blockTranslationFactory
65
     * @param BlockRepositoryInterface $blockRepository
66
     * @param ImageUploaderInterface $imageUploader
67
     * @param ProductRepositoryInterface $productRepository
68
     * @param SectionRepositoryInterface $sectionRepository
69
     */
70
    public function __construct(
71
        BlockFactoryInterface $blockFactory,
72
        FactoryInterface $blockTranslationFactory,
73
        BlockRepositoryInterface $blockRepository,
74
        ImageUploaderInterface $imageUploader,
75
        ProductRepositoryInterface $productRepository,
76
        SectionRepositoryInterface $sectionRepository
77
    )
78
    {
79
        $this->blockFactory = $blockFactory;
80
        $this->blockTranslationFactory = $blockTranslationFactory;
81
        $this->blockRepository = $blockRepository;
82
        $this->imageUploader = $imageUploader;
83
        $this->productRepository = $productRepository;
84
        $this->sectionRepository = $sectionRepository;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function load(array $data): void
91
    {
92
        foreach ($data as $code => $fields) {
93
            if (
94
                true === $fields['remove_existing'] &&
95
                null !== $block = $this->blockRepository->findOneBy(['code' => $code])
96
            ) {
97
                $this->blockRepository->remove($block);
98
            }
99
100
            if (null !== $fields['number']) {
101
                for ($i = 0; $i < $fields['number']; $i++) {
102
                    $this->createBlock(md5(uniqid()), $fields);
103
                }
104
            } else {
105
                $this->createBlock($code, $fields);
106
            }
107
        }
108
    }
109
110
    private function createBlock(string $code, array $blockData): void
111
    {
112
        $type = $blockData['type'];
113
        $block = $this->blockFactory->createWithType($type);
114
        $products = $blockData['products'];
115
116
        if (null !== $products) {
117
            $this->resolveProducts($block, $products);
118
        }
119
120
        $this->resolveSections($block, $blockData['sections']);
121
122
        $block->setCode($code);
123
        $block->setEnabled($blockData['enabled']);
124
125
        foreach ($blockData['translations'] as $localeCode => $translation) {
126
            /** @var BlockTranslationInterface $blockTranslation */
127
            $blockTranslation = $this->blockTranslationFactory->createNew();
128
129
            $blockTranslation->setLocale($localeCode);
130
            $blockTranslation->setName($translation['name']);
131
            $blockTranslation->setContent($translation['content']);
132
            $blockTranslation->setLink($translation['link']);
133
134
            if (BlockInterface::IMAGE_BLOCK_TYPE === $type) {
135
                $image = new BlockImage();
136
                $path = $translation['image_path'];
137
                $uploadedImage = new UploadedFile($path, md5($path) . '.jpg');
138
139
                $image->setFile($uploadedImage);
140
                $blockTranslation->setImage($image);
141
142
                $this->imageUploader->upload($image);
143
            }
144
145
            $block->addTranslation($blockTranslation);
146
        }
147
148
        $this->blockRepository->add($block);
149
    }
150
151
152
    /**
153
     * @param BlockInterface $block
154
     * @param int $limit
155
     */
156
    private function resolveProducts(BlockInterface $block, int $limit): void
157
    {
158
        $products = $this->productRepository->findBy([], null, $limit);
159
160
        foreach ($products as $product) {
161
            $block->addProduct($product);
162
        }
163
    }
164
165
    /**
166
     * @param BlockInterface $block
167
     * @param array $sections
168
     */
169
    private function resolveSections(BlockInterface $block, array $sections): void
170
    {
171
        foreach ($sections as $sectionCode) {
172
            /** @var SectionInterface $section */
173
            $section = $this->sectionRepository->findOneBy(['code' => $sectionCode]);
174
175
            $block->addSection($section);
176
        }
177
    }
178
}
179