Completed
Pull Request — master (#213)
by
unknown
01:40
created

BlockFixtureFactory::resolveSections()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file has been created by 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\Assigner\ChannelsAssignerInterface;
16
use BitBag\SyliusCmsPlugin\Assigner\ProductsAssignerInterface;
17
use BitBag\SyliusCmsPlugin\Assigner\SectionsAssignerInterface;
18
use BitBag\SyliusCmsPlugin\Entity\BlockInterface;
19
use BitBag\SyliusCmsPlugin\Entity\BlockTranslationInterface;
20
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface;
21
use Sylius\Component\Channel\Context\ChannelContextInterface;
22
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
23
use Sylius\Component\Locale\Context\LocaleContextInterface;
24
use Sylius\Component\Resource\Factory\FactoryInterface;
25
26
final class BlockFixtureFactory implements FixtureFactoryInterface
27
{
28
    /** @var FactoryInterface */
29
    private $blockFactory;
30
31
    /** @var FactoryInterface */
32
    private $blockTranslationFactory;
33
34
    /** @var BlockRepositoryInterface */
35
    private $blockRepository;
36
37
    /** @var ChannelContextInterface */
38
    private $channelContext;
39
40
    /** @var LocaleContextInterface */
41
    private $localeContext;
42
43
    /** @var ProductRepositoryInterface */
44
    private $productRepository;
45
46
    /** @var ProductsAssignerInterface */
47
    private $productsAssigner;
48
49
    /** @var SectionsAssignerInterface */
50
    private $sectionsAssigner;
51
52
    /** @var ChannelsAssignerInterface */
53
    private $channelAssigner;
54
55
    public function __construct(
56
        FactoryInterface $blockFactory,
57
        FactoryInterface $blockTranslationFactory,
58
        ProductRepositoryInterface $productRepository,
59
        BlockRepositoryInterface $blockRepository,
60
        ProductsAssignerInterface $productsAssigner,
61
        SectionsAssignerInterface $sectionsAssigner,
62
        ChannelsAssignerInterface $channelAssigner
63
    ) {
64
        $this->blockFactory = $blockFactory;
65
        $this->blockTranslationFactory = $blockTranslationFactory;
66
        $this->blockRepository = $blockRepository;
67
        $this->productRepository = $productRepository;
68
        $this->productsAssigner = $productsAssigner;
69
        $this->sectionsAssigner = $sectionsAssigner;
70
        $this->channelAssigner = $channelAssigner;
71
    }
72
73
    public function load(array $data): void
74
    {
75
        foreach ($data as $code => $fields) {
76
            if (
77
                true === $fields['remove_existing'] &&
78
                null !== $block = $this->blockRepository->findOneBy(['code' => $code])
79
            ) {
80
                $this->blockRepository->remove($block);
81
            }
82
83
            if (null !== $fields['number']) {
84
                for ($i = 0; $i < $fields['number']; ++$i) {
85
                    $this->createBlock(md5(uniqid()), $fields);
86
                }
87
            } else {
88
                $this->createBlock($code, $fields);
89
            }
90
        }
91
    }
92
93
    private function createBlock(string $code, array $blockData): void
94
    {
95
        /** @var BlockInterface $block */
96
        $block = $this->blockFactory->createNew();
97
98
        $products = $blockData['products'];
99
        if ($products !== null) {
100
            $this->resolveProducts($block, $products);
101
        }
102
103
        $this->sectionsAssigner->assign($block, $blockData['sections']);
104
        $this->productsAssigner->assign($block, $blockData['productCodes']);
105
        $this->channelAssigner->assign($block, $blockData['channels']);
106
107
        $block->setCode($code);
108
        $block->setEnabled($blockData['enabled']);
109
110
        foreach ($blockData['translations'] as $localeCode => $translation) {
111
            /** @var BlockTranslationInterface $blockTranslation */
112
            $blockTranslation = $this->blockTranslationFactory->createNew();
113
114
            $blockTranslation->setLocale($localeCode);
115
            $blockTranslation->setName($translation['name']);
116
            $blockTranslation->setContent($translation['content']);
117
            $blockTranslation->setLink($translation['link']);
118
            $block->addTranslation($blockTranslation);
119
        }
120
121
        $this->blockRepository->add($block);
122
    }
123
124
    private function resolveProducts(BlockInterface $block, int $limit): void
125
    {
126
        $products = $this->productRepository->findLatestByChannel(
127
            $this->channelContext->getChannel(),
128
            $this->localeContext->getLocaleCode(),
129
            $limit
130
        );
131
        foreach ($products as $product) {
132
            $block->addProduct($product);
133
        }
134
    }
135
}
136