SectionFixtureFactory::load()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 26
rs 9.5222
cc 5
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\Entity\SectionInterface;
14
use BitBag\SyliusCmsPlugin\Entity\SectionTranslationInterface;
15
use BitBag\SyliusCmsPlugin\Repository\SectionRepositoryInterface;
16
use Sylius\Component\Resource\Factory\FactoryInterface;
17
18
final class SectionFixtureFactory implements FixtureFactoryInterface
19
{
20
    /** @var FactoryInterface */
21
    private $sectionFactory;
22
23
    /** @var FactoryInterface */
24
    private $sectionTranslationFactory;
25
26
    /** @var SectionRepositoryInterface */
27
    private $sectionRepository;
28
29
    public function __construct(
30
        FactoryInterface $sectionFactory,
31
        FactoryInterface $sectionTranslationFactory,
32
        SectionRepositoryInterface $sectionRepository
33
    ) {
34
        $this->sectionFactory = $sectionFactory;
35
        $this->sectionTranslationFactory = $sectionTranslationFactory;
36
        $this->sectionRepository = $sectionRepository;
37
    }
38
39
    public function load(array $data): void
40
    {
41
        foreach ($data as $code => $fields) {
42
            if (
43
                true === $fields['remove_existing'] &&
44
                null !== $section = $this->sectionRepository->findOneBy(['code' => $code])
45
            ) {
46
                $this->sectionRepository->remove($section);
47
            }
48
49
            /** @var SectionInterface $section */
50
            $section = $this->sectionFactory->createNew();
51
52
            $section->setCode($code);
53
54
            foreach ($fields['translations'] as $localeCode => $translation) {
55
                /** @var SectionTranslationInterface $sectionTranslation */
56
                $sectionTranslation = $this->sectionTranslationFactory->createNew();
57
58
                $sectionTranslation->setLocale($localeCode);
59
                $sectionTranslation->setName($translation['name']);
60
61
                $section->addTranslation($sectionTranslation);
62
            }
63
64
            $this->sectionRepository->add($section);
65
        }
66
    }
67
}
68