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

PageFixture::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
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\PageInterface;
16
use BitBag\CmsPlugin\Entity\PageTranslationInterface;
17
use BitBag\CmsPlugin\Repository\PageRepositoryInterface;
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 PageFixture extends AbstractFixture implements FixtureInterface
27
{
28
    /**
29
     * @var FactoryInterface
30
     */
31
    private $pageFactory;
32
33
    /**
34
     * @var FactoryInterface
35
     */
36
    private $pageTranslationFactory;
37
38
    /**
39
     * @var PageRepositoryInterface
40
     */
41
    private $pageRepository;
42
43
    /**
44
     * @param FactoryInterface $pageFactory
45
     * @param FactoryInterface $pageTranslationFactory
46
     * @param PageRepositoryInterface $pageRepository
47
     */
48
    public function __construct(
49
        FactoryInterface $pageFactory,
50
        FactoryInterface $pageTranslationFactory,
51
        PageRepositoryInterface $pageRepository
52
    )
53
    {
54
        $this->pageFactory = $pageFactory;
55
        $this->pageTranslationFactory = $pageTranslationFactory;
56
        $this->pageRepository = $pageRepository;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function load(array $options): void
63
    {
64
        foreach ($options['pages'] as $code => $fields) {
65
66
            if (null !== $this->pageRepository->findOneBy(['code' => $code])) {
67
                continue;
68
            }
69
70
            /** @var PageInterface $page */
71
            $page = $this->pageFactory->createNew();
72
73
            $page->setCode($code);
74
            $page->setEnabled($fields['enabled']);
75
76
            foreach ($fields['translations'] as $localeCode => $translation) {
77
                /** @var PageTranslationInterface $pageTranslation */
78
                $pageTranslation = $this->pageTranslationFactory->createNew();
79
80
                $pageTranslation->setLocale($localeCode);
81
                $pageTranslation->setSlug($translation['slug']);
82
                $pageTranslation->setName($translation['name']);
83
                $pageTranslation->setMetaKeywords($translation['meta_keywords']);
84
                $pageTranslation->setMetaDescription($translation['meta_description']);
85
                $pageTranslation->setContent($translation['content']);
86
87
                $page->addTranslation($pageTranslation);
88
            }
89
90
            $this->pageRepository->add($page);
91
        }
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function getName(): string
98
    {
99
        return 'bitbag_cms_page';
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
106
    {
107
        $optionsNode
108
            ->children()
109
                ->arrayNode('pages')
110
                    ->prototype('array')
111
                        ->children()
112
                            ->scalarNode('enabled')->defaultTrue()->end()
113
                            ->arrayNode('translations')
114
                                ->prototype('array')
115
                                    ->children()
116
                                        ->scalarNode('slug')->defaultNull()->end()
117
                                        ->scalarNode('name')->defaultNull()->end()
118
                                        ->scalarNode('meta_keywords')->defaultNull()->end()
119
                                        ->scalarNode('meta_description')->defaultNull()->end()
120
                                        ->scalarNode('content')->defaultNull()->end()
121
                                    ->end()
122
                                ->end()
123
                            ->end()
124
                        ->end()
125
                    ->end()
126
                ->end()
127
            ->end();
128
    }
129
}
130