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

FrequentlyAskedQuestionFixture::load()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 5
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\FrequentlyAskedQuestionInterface;
16
use BitBag\CmsPlugin\Entity\FrequentlyAskedQuestionTranslationInterface;
17
use BitBag\CmsPlugin\Repository\FrequentlyAskedQuestionRepositoryInterface;
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 Mikołaj Król <[email protected]>
25
 */
26
final class FrequentlyAskedQuestionFixture extends AbstractFixture implements FixtureInterface
27
{
28
    /**
29
     * @var FactoryInterface
30
     */
31
    private $frequentlyAskedQuestionFactory;
32
33
    /**
34
     * @var FactoryInterface
35
     */
36
    private $frequentlyAskedQuestionTranslationFactory;
37
38
    /**
39
     * @var FrequentlyAskedQuestionRepositoryInterface
40
     */
41
    private $frequentlyAskedQuestionRepository;
42
43
    /**
44
     * @param FactoryInterface $frequentlyAskedQuestionFactory
45
     * @param FactoryInterface $frequentlyAskedQuestionTranslationFactory
46
     * @param FrequentlyAskedQuestionRepositoryInterface $frequentlyAskedQuestionRepository
47
     */
48
    public function __construct(
49
        FactoryInterface $frequentlyAskedQuestionFactory,
50
        FactoryInterface $frequentlyAskedQuestionTranslationFactory,
51
        FrequentlyAskedQuestionRepositoryInterface $frequentlyAskedQuestionRepository
52
    )
53
    {
54
        $this->frequentlyAskedQuestionFactory = $frequentlyAskedQuestionFactory;
55
        $this->frequentlyAskedQuestionTranslationFactory = $frequentlyAskedQuestionTranslationFactory;
56
        $this->frequentlyAskedQuestionRepository = $frequentlyAskedQuestionRepository;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function load(array $options): void
63
    {
64
        foreach ($options['frequently_asked_questions'] as $code => $fields) {
65
            if (
66
                true === $fields['remove_existing'] &&
67
                null !== $frequentlyAskedQuestion = $this->frequentlyAskedQuestionRepository->findOneBy(['code' => $code])
68
            ) {
69
                $this->frequentlyAskedQuestionRepository->remove($frequentlyAskedQuestion);
70
            }
71
72
            /** @var FrequentlyAskedQuestionInterface $frequentlyAskedQuestion */
73
            $frequentlyAskedQuestion = $this->frequentlyAskedQuestionFactory->createNew();
74
75
            $frequentlyAskedQuestion->setCode($code);
76
            $frequentlyAskedQuestion->setEnabled($fields['enabled']);
77
            $frequentlyAskedQuestion->setPosition($fields['position']);
78
79
            foreach ($fields['translations'] as $localeCode => $translation) {
80
                /** @var FrequentlyAskedQuestionTranslationInterface $frequentlyAskedQuestionTranslation */
81
                $frequentlyAskedQuestionTranslation = $this->frequentlyAskedQuestionFactory->createNew();
82
83
                $frequentlyAskedQuestionTranslation->setLocale($localeCode);
84
                $frequentlyAskedQuestionTranslation->setQuestion($translation['question']);
85
                $frequentlyAskedQuestionTranslation->setAnswer($translation['answer']);
86
87
                $frequentlyAskedQuestion->addTranslation($frequentlyAskedQuestionTranslation);
88
            }
89
90
            $this->frequentlyAskedQuestionRepository->add($frequentlyAskedQuestion);
91
        }
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function getName(): string
98
    {
99
        return 'bitbag_cms_frequently_asked_question';
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
106
    {
107
        $optionsNode
108
            ->children()
109
                ->arrayNode('frequently_asked_questions')
110
                    ->prototype('array')
111
                        ->children()
112
                            ->booleanNode('remove_existing')->defaultTrue()->end()
113
                            ->booleanNode('enabled')->defaultTrue()->end()
114
                            ->integerNode('position')->defaultNull()->end()
115
                            ->arrayNode('translations')
116
                                ->prototype('array')
117
                                    ->children()
118
                                        ->scalarNode('question')->defaultNull()->end()
119
                                        ->scalarNode('answer')->defaultNull()->end()
120
                                    ->end()
121
                                ->end()
122
                            ->end()
123
                        ->end()
124
                    ->end()
125
                ->end()
126
            ->end()
127
        ;
128
    }
129
}
130