Completed
Pull Request — master (#82)
by
unknown
01:17
created

SectionFixture::configureOptionsNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 17
nc 1
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;
14
15
use BitBag\CmsPlugin\Entity\SectionInterface;
16
use BitBag\CmsPlugin\Entity\SectionTranslationInterface;
17
use BitBag\CmsPlugin\Repository\SectionRepositoryInterface;
18
use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
19
use Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface;
20
use Sylius\Component\Resource\Factory\FactoryInterface;
21
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
22
23
/**
24
 * @author Patryk Drapik <[email protected]>
25
 */
26
final class SectionFixture extends AbstractFixture implements FixtureInterface
27
{
28
    /**
29
     * @var FactoryInterface
30
     */
31
    private $sectionFactory;
32
33
    /**
34
     * @var FactoryInterface
35
     */
36
    private $sectionTranslationFactory;
37
38
    /**
39
     * @var SectionRepositoryInterface
40
     */
41
    private $sectionRepository;
42
43
    /**
44
     * @param FactoryInterface $sectionFactory
45
     * @param FactoryInterface $sectionTranslationFactory
46
     * @param SectionRepositoryInterface $sectionRepository
47
     */
48
    public function __construct(
49
        FactoryInterface $sectionFactory,
50
        FactoryInterface $sectionTranslationFactory,
51
        SectionRepositoryInterface $sectionRepository
52
    )
53
    {
54
        $this->sectionFactory = $sectionFactory;
55
        $this->sectionTranslationFactory = $sectionTranslationFactory;
56
        $this->sectionRepository = $sectionRepository;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function load(array $options): void
63
    {
64
        foreach ($options['sections'] as $code => $fields) {
65
66
            if (null !== $this->sectionRepository->findOneBy(['code' => $code])) {
67
                continue;
68
            }
69
70
            /** @var SectionInterface $section */
71
            $section = $this->sectionFactory->createNew();
72
73
            $section->setCode($code);
74
75
            foreach ($fields['translations'] as $localeCode => $translation) {
76
                /** @var SectionTranslationInterface $sectionTranslation */
77
                $sectionTranslation = $this->sectionTranslationFactory->createNew();
78
79
                $sectionTranslation->setName($translation['name']);
80
81
                $section->addTranslation($sectionTranslation);
82
            }
83
84
            $this->sectionRepository->add($section);
85
        }
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91
    public function getName(): string
92
    {
93
        return 'bitbag_cms_section';
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
100
    {
101
        $optionsNode
102
            ->children()
103
                ->arrayNode('sections')
104
                    ->prototype('array')
105
                        ->children()
106
                            ->arrayNode('translations')
107
                                ->prototype('array')
108
                                    ->children()
109
                                        ->scalarNode('name')->defaultNull()->end()
110
                                    ->end()
111
                                ->end()
112
                            ->end()
113
                        ->end()
114
                    ->end()
115
                ->end()
116
            ->end();
117
    }
118
}
119