Completed
Pull Request — master (#81)
by Mikołaj
01:23
created

BlockFixtureFactory::load()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.439
c 0
b 0
f 0
cc 6
eloc 25
nc 7
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\CmsPlugin\Fixture\Factory;
14
15
use BitBag\CmsPlugin\Entity\BlockInterface;
16
use BitBag\CmsPlugin\Entity\BlockTranslationInterface;
17
use BitBag\CmsPlugin\Entity\BlockImage;
18
use BitBag\CmsPlugin\Factory\BlockFactoryInterface;
19
use BitBag\CmsPlugin\Repository\BlockRepositoryInterface;
20
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
use Symfony\Component\HttpFoundation\File\UploadedFile;
23
24
/**
25
 * @author Mikołaj Król <[email protected]>
26
 */
27
final class BlockFixtureFactory implements FixtureFactoryInterface
28
{
29
    /**
30
     * @var BlockFactoryInterface
31
     */
32
    private $blockFactory;
33
34
    /**
35
     * @var FactoryInterface
36
     */
37
    private $blockTranslationFactory;
38
39
    /**
40
     * @var BlockRepositoryInterface
41
     */
42
    private $blockRepository;
43
44
    /**
45
     * @var ImageUploaderInterface
46
     */
47
    private $imageUploader;
48
49
    /**
50
     * @param BlockFactoryInterface $blockFactory
51
     * @param FactoryInterface $blockTranslationFactory
52
     * @param BlockRepositoryInterface $blockRepository
53
     * @param ImageUploaderInterface $imageUploader
54
     */
55
    public function __construct(
56
        BlockFactoryInterface $blockFactory,
57
        FactoryInterface $blockTranslationFactory,
58
        BlockRepositoryInterface $blockRepository,
59
        ImageUploaderInterface $imageUploader
60
    )
61
    {
62
        $this->blockFactory = $blockFactory;
63
        $this->blockTranslationFactory = $blockTranslationFactory;
64
        $this->blockRepository = $blockRepository;
65
        $this->imageUploader = $imageUploader;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function load(array $data): void
72
    {
73
        foreach ($data as $code => $fields) {
74
            if (
75
                true === $fields['remove_existing'] &&
76
                null !== $block = $this->blockRepository->findOneBy(['code' => $code])
77
            ) {
78
                $this->blockRepository->remove($block);
79
            }
80
81
            $type = $fields['type'];
82
            $block = $this->blockFactory->createWithType($type);
83
84
            $block->setCode($code);
85
            $block->setEnabled($fields['enabled']);
86
87
            foreach ($fields['translations'] as $localeCode => $translation) {
88
                /** @var BlockTranslationInterface $blockTranslation */
89
                $blockTranslation = $this->blockTranslationFactory->createNew();
90
91
                $blockTranslation->setLocale($localeCode);
92
                $blockTranslation->setName($translation['name']);
93
                $blockTranslation->setContent($translation['content']);
94
                $blockTranslation->setLink($translation['link']);
95
96
                if (BlockInterface::IMAGE_BLOCK_TYPE === $type) {
97
                    $image = new BlockImage();
98
                    $path = $translation['image_path'];
99
                    $uploadedImage = new UploadedFile($path, md5($path) . '.jpg');
100
101
                    $image->setFile($uploadedImage);
102
                    $blockTranslation->setImage($image);
103
104
                    $this->imageUploader->upload($image);
105
                }
106
107
                $block->addTranslation($blockTranslation);
108
            }
109
110
            $this->blockRepository->add($block);
111
        }
112
    }
113
}
114