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\SyliusCmsPlugin\Fixture\Factory; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusCmsPlugin\Entity\FrequentlyAskedQuestionInterface; |
16
|
|
|
use BitBag\SyliusCmsPlugin\Entity\FrequentlyAskedQuestionTranslationInterface; |
17
|
|
|
use BitBag\SyliusCmsPlugin\Repository\FrequentlyAskedQuestionRepositoryInterface; |
18
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Mikołaj Król <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class FrequentlyAskedQuestionFixtureFactory implements FixtureFactoryInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var FactoryInterface |
27
|
|
|
*/ |
28
|
|
|
private $frequentlyAskedQuestionFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var FactoryInterface |
32
|
|
|
*/ |
33
|
|
|
private $frequentlyAskedQuestionTranslationFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var FrequentlyAskedQuestionRepositoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $frequentlyAskedQuestionRepository; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param FactoryInterface $frequentlyAskedQuestionFactory |
42
|
|
|
* @param FactoryInterface $frequentlyAskedQuestionTranslationFactory |
43
|
|
|
* @param FrequentlyAskedQuestionRepositoryInterface $frequentlyAskedQuestionRepository |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
FactoryInterface $frequentlyAskedQuestionFactory, |
47
|
|
|
FactoryInterface $frequentlyAskedQuestionTranslationFactory, |
48
|
|
|
FrequentlyAskedQuestionRepositoryInterface $frequentlyAskedQuestionRepository |
49
|
|
|
) |
50
|
|
|
{ |
51
|
|
|
$this->frequentlyAskedQuestionFactory = $frequentlyAskedQuestionFactory; |
52
|
|
|
$this->frequentlyAskedQuestionTranslationFactory = $frequentlyAskedQuestionTranslationFactory; |
53
|
|
|
$this->frequentlyAskedQuestionRepository = $frequentlyAskedQuestionRepository; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function load(array $data): void |
60
|
|
|
{ |
61
|
|
|
foreach ($data as $code => $fields) { |
62
|
|
|
if ( |
63
|
|
|
true === $fields['remove_existing'] && |
64
|
|
|
null !== $frequentlyAskedQuestion = $this->frequentlyAskedQuestionRepository->findOneBy(['code' => $code]) |
65
|
|
|
) { |
66
|
|
|
$this->frequentlyAskedQuestionRepository->remove($frequentlyAskedQuestion); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (null !== $fields['number']) { |
70
|
|
|
for ($i = 1; $i <= $fields['number']; $i++) { |
71
|
|
|
$this->createFrequentlyAskedQuestion(md5(uniqid()), $fields, $i); |
72
|
|
|
} |
73
|
|
|
} else { |
74
|
|
|
$this->createFrequentlyAskedQuestion($code, $fields, $fields['position']); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string $code |
81
|
|
|
* @param array $frequentlyAskedQuestionData |
82
|
|
|
* @param int $position |
83
|
|
|
*/ |
84
|
|
|
private function createFrequentlyAskedQuestion(string $code, array $frequentlyAskedQuestionData, int $position): void |
85
|
|
|
{ |
86
|
|
|
/** @var FrequentlyAskedQuestionInterface $frequentlyAskedQuestion */ |
87
|
|
|
$frequentlyAskedQuestion = $this->frequentlyAskedQuestionFactory->createNew(); |
88
|
|
|
|
89
|
|
|
$frequentlyAskedQuestion->setCode($code); |
90
|
|
|
$frequentlyAskedQuestion->setEnabled($frequentlyAskedQuestionData['enabled']); |
91
|
|
|
$frequentlyAskedQuestion->setPosition($position); |
92
|
|
|
|
93
|
|
|
foreach ($frequentlyAskedQuestionData['translations'] as $localeCode => $translation) { |
94
|
|
|
/** @var FrequentlyAskedQuestionTranslationInterface $frequentlyAskedQuestionTranslation */ |
95
|
|
|
$frequentlyAskedQuestionTranslation = $this->frequentlyAskedQuestionTranslationFactory->createNew(); |
96
|
|
|
|
97
|
|
|
$frequentlyAskedQuestionTranslation->setLocale($localeCode); |
98
|
|
|
$frequentlyAskedQuestionTranslation->setQuestion($translation['question']); |
99
|
|
|
$frequentlyAskedQuestionTranslation->setAnswer($translation['answer']); |
100
|
|
|
|
101
|
|
|
$frequentlyAskedQuestion->addTranslation($frequentlyAskedQuestionTranslation); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->frequentlyAskedQuestionRepository->add($frequentlyAskedQuestion); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|