FrequentlyAskedQuestionFixtureFactory::load()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 16
rs 9.2222
cc 6
nc 5
nop 1
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\Entity\FrequentlyAskedQuestionInterface;
15
use BitBag\SyliusCmsPlugin\Entity\FrequentlyAskedQuestionTranslationInterface;
16
use BitBag\SyliusCmsPlugin\Repository\FrequentlyAskedQuestionRepositoryInterface;
17
use Sylius\Component\Resource\Factory\FactoryInterface;
18
19
final class FrequentlyAskedQuestionFixtureFactory implements FixtureFactoryInterface
20
{
21
    /** @var FactoryInterface */
22
    private $frequentlyAskedQuestionFactory;
23
24
    /** @var FactoryInterface */
25
    private $frequentlyAskedQuestionTranslationFactory;
26
27
    /** @var FrequentlyAskedQuestionRepositoryInterface */
28
    private $frequentlyAskedQuestionRepository;
29
30
    /** @var ChannelsAssignerInterface */
31
    private $channelAssigner;
32
33
    public function __construct(
34
        FactoryInterface $frequentlyAskedQuestionFactory,
35
        FactoryInterface $frequentlyAskedQuestionTranslationFactory,
36
        FrequentlyAskedQuestionRepositoryInterface $frequentlyAskedQuestionRepository,
37
        ChannelsAssignerInterface $channelAssigner
38
    ) {
39
        $this->frequentlyAskedQuestionFactory = $frequentlyAskedQuestionFactory;
40
        $this->frequentlyAskedQuestionTranslationFactory = $frequentlyAskedQuestionTranslationFactory;
41
        $this->frequentlyAskedQuestionRepository = $frequentlyAskedQuestionRepository;
42
        $this->channelAssigner = $channelAssigner;
43
    }
44
45
    public function load(array $data): void
46
    {
47
        foreach ($data as $code => $fields) {
48
            if (
49
                true === $fields['remove_existing'] &&
50
                null !== $frequentlyAskedQuestion = $this->frequentlyAskedQuestionRepository->findOneBy(['code' => $code])
51
            ) {
52
                $this->frequentlyAskedQuestionRepository->remove($frequentlyAskedQuestion);
53
            }
54
55
            if (null !== $fields['number']) {
56
                for ($i = 1; $i <= $fields['number']; ++$i) {
57
                    $this->createFrequentlyAskedQuestion(md5(uniqid()), $fields, $i);
58
                }
59
            } else {
60
                $this->createFrequentlyAskedQuestion($code, $fields, $fields['position']);
61
            }
62
        }
63
    }
64
65
    private function createFrequentlyAskedQuestion(
66
        string $code,
67
        array $frequentlyAskedQuestionData,
68
        int $position
69
    ): void {
70
        /** @var FrequentlyAskedQuestionInterface $frequentlyAskedQuestion */
71
        $frequentlyAskedQuestion = $this->frequentlyAskedQuestionFactory->createNew();
72
73
        $frequentlyAskedQuestion->setCode($code);
74
        $frequentlyAskedQuestion->setEnabled($frequentlyAskedQuestionData['enabled']);
75
        $frequentlyAskedQuestion->setPosition($position);
76
77
        $this->channelAssigner->assign($frequentlyAskedQuestion, $frequentlyAskedQuestionData['channels']);
78
79
        foreach ($frequentlyAskedQuestionData['translations'] as $localeCode => $translation) {
80
            /** @var FrequentlyAskedQuestionTranslationInterface $frequentlyAskedQuestionTranslation */
81
            $frequentlyAskedQuestionTranslation = $this->frequentlyAskedQuestionTranslationFactory->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