BlockFixtureFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 22
rs 9.9332
cc 1
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusCmsPlugin\Fixture\Factory;
12
13
use BitBag\SyliusCmsPlugin\Assigner\ChannelsAssignerInterface;
14
use BitBag\SyliusCmsPlugin\Assigner\ProductsAssignerInterface;
15
use BitBag\SyliusCmsPlugin\Assigner\SectionsAssignerInterface;
16
use BitBag\SyliusCmsPlugin\Assigner\TaxonsAssignerInterface;
17
use BitBag\SyliusCmsPlugin\Entity\BlockInterface;
18
use BitBag\SyliusCmsPlugin\Entity\BlockTranslationInterface;
19
use BitBag\SyliusCmsPlugin\Repository\BlockRepositoryInterface;
20
use Sylius\Component\Channel\Context\ChannelContextInterface;
21
use Sylius\Component\Core\Model\ChannelInterface;
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 TaxonsAssignerInterface */
50
    private $taxonsAssigner;
51
52
    /** @var SectionsAssignerInterface */
53
    private $sectionsAssigner;
54
55
    /** @var ChannelsAssignerInterface */
56
    private $channelAssigner;
57
58
    public function __construct(
59
        FactoryInterface $blockFactory,
60
        FactoryInterface $blockTranslationFactory,
61
        BlockRepositoryInterface $blockRepository,
62
        ProductRepositoryInterface $productRepository,
63
        ChannelContextInterface $channelContext,
64
        LocaleContextInterface $localeContext,
65
        ProductsAssignerInterface $productsAssigner,
66
        TaxonsAssignerInterface $taxonsAssigner,
67
        SectionsAssignerInterface $sectionsAssigner,
68
        ChannelsAssignerInterface $channelAssigner
69
    ) {
70
        $this->blockFactory = $blockFactory;
71
        $this->blockTranslationFactory = $blockTranslationFactory;
72
        $this->blockRepository = $blockRepository;
73
        $this->productRepository = $productRepository;
74
        $this->channelContext = $channelContext;
75
        $this->localeContext = $localeContext;
76
        $this->productsAssigner = $productsAssigner;
77
        $this->taxonsAssigner = $taxonsAssigner;
78
        $this->sectionsAssigner = $sectionsAssigner;
79
        $this->channelAssigner = $channelAssigner;
80
    }
81
82
    public function load(array $data): void
83
    {
84
        foreach ($data as $code => $fields) {
85
            if (
86
                true === $fields['remove_existing'] &&
87
                null !== $block = $this->blockRepository->findOneBy(['code' => $code])
88
            ) {
89
                $this->blockRepository->remove($block);
90
            }
91
92
            if (null !== $fields['number']) {
93
                for ($i = 0; $i < $fields['number']; ++$i) {
94
                    $this->createBlock(md5(uniqid()), $fields);
95
                }
96
            } else {
97
                $this->createBlock($code, $fields);
98
            }
99
        }
100
    }
101
102
    private function createBlock(string $code, array $blockData): void
103
    {
104
        /** @var BlockInterface $block */
105
        $block = $this->blockFactory->createNew();
106
107
        $products = $blockData['products'];
108
        if (null !== $products) {
109
            $this->resolveProducts($block, $products);
110
        }
111
112
        $this->sectionsAssigner->assign($block, $blockData['sections']);
113
        $this->productsAssigner->assign($block, $blockData['productCodes']);
114
        $this->taxonsAssigner->assign($block, $blockData['taxons']);
115
        $this->channelAssigner->assign($block, $blockData['channels']);
116
117
        $block->setCode($code);
118
        $block->setEnabled($blockData['enabled']);
119
120
        foreach ($blockData['translations'] as $localeCode => $translation) {
121
            /** @var BlockTranslationInterface $blockTranslation */
122
            $blockTranslation = $this->blockTranslationFactory->createNew();
123
124
            $blockTranslation->setLocale($localeCode);
125
            $blockTranslation->setName($translation['name']);
126
            $blockTranslation->setContent($translation['content']);
127
            $blockTranslation->setLink($translation['link']);
128
            $block->addTranslation($blockTranslation);
129
        }
130
131
        $this->blockRepository->add($block);
132
    }
133
134
    private function resolveProducts(BlockInterface $block, int $limit): void
135
    {
136
        /** @var ChannelInterface $channel */
137
        $channel = $this->channelContext->getChannel();
138
        $products = $this->productRepository->findLatestByChannel(
139
            $channel,
140
            $this->localeContext->getLocaleCode(),
141
            $limit
142
        );
143
        foreach ($products as $product) {
144
            $block->addProduct($product);
145
        }
146
    }
147
}
148