Completed
Pull Request — master (#81)
by Mikołaj
01:19
created

PageFixtureFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
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\Factory;
14
15
use BitBag\CmsPlugin\Entity\PageInterface;
16
use BitBag\CmsPlugin\Entity\PageTranslationInterface;
17
use BitBag\CmsPlugin\Entity\SectionInterface;
18
use BitBag\CmsPlugin\Repository\PageRepositoryInterface;
19
use BitBag\CmsPlugin\Repository\SectionRepositoryInterface;
20
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
21
use Sylius\Component\Resource\Factory\FactoryInterface;
22
23
/**
24
 * @author Mikołaj Król <[email protected]>
25
 */
26
final class PageFixtureFactory implements FixtureFactoryInterface
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
     * @var ProductRepositoryInterface
45
     */
46
    private $productRepository;
47
48
    /**
49
     * @var SectionRepositoryInterface
50
     */
51
    private $sectionRepository;
52
53
    /**
54
     * @param FactoryInterface $pageFactory
55
     * @param FactoryInterface $pageTranslationFactory
56
     * @param PageRepositoryInterface $pageRepository
57
     * @param ProductRepositoryInterface $productRepository
58
     * @param SectionRepositoryInterface $sectionRepository
59
     */
60
    public function __construct(
61
        FactoryInterface $pageFactory,
62
        FactoryInterface $pageTranslationFactory,
63
        PageRepositoryInterface $pageRepository,
64
        ProductRepositoryInterface $productRepository,
65
        SectionRepositoryInterface $sectionRepository
66
    )
67
    {
68
        $this->pageFactory = $pageFactory;
69
        $this->pageTranslationFactory = $pageTranslationFactory;
70
        $this->pageRepository = $pageRepository;
71
        $this->productRepository = $productRepository;
72
        $this->sectionRepository = $sectionRepository;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function load(array $data): void
79
    {
80
        foreach ($data as $code => $fields) {
81
            if (
82
                true === $fields['remove_existing'] &&
83
                null !== $page = $this->pageRepository->findOneBy(['code' => $code])
84
            ) {
85
                $this->pageRepository->remove($page);
86
            }
87
88
            if (null !== $fields['number']) {
89
                for ($i = 0; $i < $fields['number']; $i++) {
90
                    $this->createPage(md5(uniqid()), $fields,true);
91
                }
92
            } else {
93
                $this->createPage($code, $fields);
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param string $code
100
     * @param array $pageData
101
     * @param bool $generateSlug
102
     */
103
    private function createPage(string $code, array $pageData, bool $generateSlug = false): void
104
    {
105
        /** @var PageInterface $page */
106
        $page = $this->pageFactory->createNew();
107
        $products = $pageData['products'];
108
109
        if (null !== $products) {
110
            $this->resolveProducts($page, $products);
111
        }
112
113
        $this->resolveSections($page, $pageData['sections']);
114
115
        $page->setCode($code);
116
        $page->setEnabled($pageData['enabled']);
117
118
        foreach ($pageData['translations'] as $localeCode => $translation) {
119
            /** @var PageTranslationInterface $pageTranslation */
120
            $pageTranslation = $this->pageTranslationFactory->createNew();
121
            $slug = true === $generateSlug ? md5(uniqid()) : $translation['slug'];
122
123
            $pageTranslation->setLocale($localeCode);
124
            $pageTranslation->setSlug($slug);
125
            $pageTranslation->setName($translation['name']);
126
            $pageTranslation->setMetaKeywords($translation['meta_keywords']);
127
            $pageTranslation->setMetaDescription($translation['meta_description']);
128
            $pageTranslation->setContent($translation['content']);
129
130
            $page->addTranslation($pageTranslation);
131
        }
132
133
        $this->pageRepository->add($page);
134
    }
135
136
    /**
137
     * @param int $limit
138
     * @param PageInterface $page
139
     */
140
    private function resolveProducts(PageInterface $page, int $limit): void
141
    {
142
        $products = $this->productRepository->findBy([], null, $limit);
143
144
        foreach ($products as $product) {
145
            $page->addProduct($product);
146
        }
147
    }
148
149
    /**
150
     * @param PageInterface $page
151
     * @param array $sections
152
     */
153
    private function resolveSections(PageInterface $page, array $sections): void
154
    {
155
        foreach ($sections as $sectionCode) {
156
            /** @var SectionInterface $section */
157
            $section = $this->sectionRepository->findOneBy(['code' => $sectionCode]);
158
159
            $page->addSection($section);
160
        }
161
    }
162
}
163