Completed
Push — master ( 7ec8e5...bab96a )
by Mikołaj
28s
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
        BlockRepositoryInterface $blockRepository,
59
        ProductRepositoryInterface $productRepository,
60
        ChannelContextInterface $channelContext,
61
        LocaleContextInterface $localeContext,
62
        ProductsAssignerInterface $productsAssigner,
63
        SectionsAssignerInterface $sectionsAssigner,
64
        ChannelsAssignerInterface $channelAssigner
65
    ) {
66
        $this->blockFactory = $blockFactory;
67
        $this->blockTranslationFactory = $blockTranslationFactory;
68
        $this->blockRepository = $blockRepository;
69
        $this->productRepository = $productRepository;
70
        $this->channelContext = $channelContext;
71
        $this->localeContext = $localeContext;
72
        $this->productsAssigner = $productsAssigner;
73
        $this->sectionsAssigner = $sectionsAssigner;
74
        $this->channelAssigner = $channelAssigner;
75
    }
76
77
    public function load(array $data): void
78
    {
79
        foreach ($data as $code => $fields) {
80
            if (
81
                true === $fields['remove_existing'] &&
82
                null !== $block = $this->blockRepository->findOneBy(['code' => $code])
83
            ) {
84
                $this->blockRepository->remove($block);
85
            }
86
87
            if (null !== $fields['number']) {
88
                for ($i = 0; $i < $fields['number']; ++$i) {
89
                    $this->createBlock(md5(uniqid()), $fields);
90
                }
91
            } else {
92
                $this->createBlock($code, $fields);
93
            }
94
        }
95
    }
96
97
    private function createBlock(string $code, array $blockData): void
98
    {
99
        /** @var BlockInterface $block */
100
        $block = $this->blockFactory->createNew();
101
102
        $products = $blockData['products'];
103
        if ($products !== null) {
104
            $this->resolveProducts($block, $products);
105
        }
106
107
        $this->sectionsAssigner->assign($block, $blockData['sections']);
108
        $this->productsAssigner->assign($block, $blockData['productCodes']);
109
        $this->channelAssigner->assign($block, $blockData['channels']);
110
111
        $block->setCode($code);
112
        $block->setEnabled($blockData['enabled']);
113
114
        foreach ($blockData['translations'] as $localeCode => $translation) {
115
            /** @var BlockTranslationInterface $blockTranslation */
116
            $blockTranslation = $this->blockTranslationFactory->createNew();
117
118
            $blockTranslation->setLocale($localeCode);
119
            $blockTranslation->setName($translation['name']);
120
            $blockTranslation->setContent($translation['content']);
121
            $blockTranslation->setLink($translation['link']);
122
            $block->addTranslation($blockTranslation);
123
        }
124
125
        $this->blockRepository->add($block);
126
    }
127
128
    private function resolveProducts(BlockInterface $block, int $limit): void
129
    {
130
        $products = $this->productRepository->findLatestByChannel(
131
            $this->channelContext->getChannel(),
132
            $this->localeContext->getLocaleCode(),
133
            $limit
134
        );
135
        foreach ($products as $product) {
136
            $block->addProduct($product);
137
        }
138
    }
139
}
140