Completed
Pull Request — master (#95)
by
unknown
01:39
created

SectionFixtureFactory::load()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
rs 8.439
cc 5
eloc 14
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\SyliusCmsPlugin\Fixture\Factory;
14
15
use BitBag\SyliusCmsPlugin\Entity\SectionInterface;
16
use BitBag\SyliusCmsPlugin\Entity\SectionTranslationInterface;
17
use BitBag\SyliusCmsPlugin\Repository\SectionRepositoryInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
20
/**
21
 * @author Mikołaj Król <[email protected]>
22
 */
23
final class SectionFixtureFactory implements FixtureFactoryInterface
24
{
25
    /**
26
     * @var FactoryInterface
27
     */
28
    private $sectionFactory;
29
30
    /**
31
     * @var FactoryInterface
32
     */
33
    private $sectionTranslationFactory;
34
35
    /**
36
     * @var SectionRepositoryInterface
37
     */
38
    private $sectionRepository;
39
40
    /**
41
     * @param FactoryInterface $sectionFactory
42
     * @param FactoryInterface $sectionTranslationFactory
43
     * @param SectionRepositoryInterface $sectionRepository
44
     */
45
    public function __construct(
46
        FactoryInterface $sectionFactory,
47
        FactoryInterface $sectionTranslationFactory,
48
        SectionRepositoryInterface $sectionRepository
49
    )
50
    {
51
        $this->sectionFactory = $sectionFactory;
52
        $this->sectionTranslationFactory = $sectionTranslationFactory;
53
        $this->sectionRepository = $sectionRepository;
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 !== $section = $this->sectionRepository->findOneBy(['code' => $code])
65
            ) {
66
                $this->sectionRepository->remove($section);
67
            }
68
69
            /** @var SectionInterface $section */
70
            $section = $this->sectionFactory->createNew();
71
72
            $section->setCode($code);
73
74
            foreach ($fields['translations'] as $localeCode => $translation) {
75
                /** @var SectionTranslationInterface $sectionTranslation */
76
                $sectionTranslation = $this->sectionTranslationFactory->createNew();
77
78
                $sectionTranslation->setLocale($localeCode);
79
                $sectionTranslation->setName($translation['name']);
80
81
                $section->addTranslation($sectionTranslation);
82
            }
83
84
            $this->sectionRepository->add($section);
85
        }
86
    }
87
}
88