Completed
Pull Request — master (#73)
by Mikołaj
16:15
created

FrequentlyAskedQuestionFixture::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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
66
            if (null !== $this->frequentlyAskedQuestionRepository->findOneBy(['code' => $code])) {
67
                continue;
68
            }
69
70
            /** @var FrequentlyAskedQuestionInterface $frequentlyAskedQuestion */
71
            $frequentlyAskedQuestion = $this->frequentlyAskedQuestionFactory->createNew();
72
73
            $frequentlyAskedQuestion->setCode($code);
74
            $frequentlyAskedQuestion->setEnabled($fields['enabled']);
75
            $frequentlyAskedQuestion->setPosition($fields['position']);
76
77
            foreach ($fields['translations'] as $localeCode => $translation) {
78
                /** @var FrequentlyAskedQuestionTranslationInterface $frequentlyAskedQuestionTranslation */
79
                $frequentlyAskedQuestionTranslation = $this->frequentlyAskedQuestionFactory->createNew();
80
81
                $frequentlyAskedQuestionTranslation->setLocale($localeCode);
82
                $frequentlyAskedQuestionTranslation->setQuestion($translation['question']);
83
                $frequentlyAskedQuestionTranslation->setAnswer($translation['answer']);
84
85
                $frequentlyAskedQuestion->addTranslation($frequentlyAskedQuestionTranslation);
86
            }
87
88
            $this->frequentlyAskedQuestionRepository->add($frequentlyAskedQuestion);
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getName(): string
96
    {
97
        return 'bitbag_cms_frequently_asked_question';
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
104
    {
105
        $optionsNode
106
            ->children()
107
                ->arrayNode('frequently_asked_questions')
108
                    ->prototype('array')
109
                        ->children()
110
                            ->scalarNode('enabled')->defaultTrue()->end()
111
                            ->scalarNode('position')->defaultNull()->end()
112
                            ->arrayNode('translations')
113
                                ->prototype('array')
114
                                    ->children()
115
                                        ->scalarNode('question')->defaultNull()->end()
116
                                        ->scalarNode('answer')->defaultNull()->end()
117
                                    ->end()
118
                                ->end()
119
                            ->end()
120
                        ->end()
121
                    ->end()
122
                ->end()
123
            ->end()
124
        ;
125
    }
126
}
127