Completed
Push — master ( 59db0f...abafe8 )
by Kamil
18:55
created

LoadTaxonsData::createTaxons()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
rs 8.439
cc 6
eloc 21
nc 12
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\FixturesBundle\DataFixtures\ORM;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Sylius\Bundle\FixturesBundle\DataFixtures\DataFixture;
16
use Sylius\Component\Taxonomy\Model\TaxonInterface;
17
18
/**
19
 * @author Paweł Jędrzejewski <[email protected]>
20
 * @author Gonzalo Vilaseca <[email protected]>
21
 */
22
class LoadTaxonsData extends DataFixture
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function load(ObjectManager $manager)
28
    {
29
        $manager->persist($this->createTaxons(
30
            'CATEGORY', [$this->defaultLocale => 'Category', 'es_ES' => 'Categoria'],
31
            [
32
                ['code' => 'TX1', 'locales' => [$this->defaultLocale => 'T-Shirts', 'es_ES' => 'Camisetas']],
33
                ['code' => 'TX2', 'locales' => [$this->defaultLocale => 'Stickers', 'es_ES' => 'Pegatinas']],
34
                ['code' => 'TX3', 'locales' => [$this->defaultLocale => 'Mugs', 'es_ES' => 'Tazas']],
35
                ['code' => 'TX4', 'locales' => [$this->defaultLocale => 'Books', 'es_ES' => 'Libros']],
36
            ]));
37
38
        $manager->persist($this->createTaxons(
39
            'BRAND', [$this->defaultLocale => 'Brand', 'es_ES' => 'Marca'],
40
            [
41
                ['code' => 'TX5', 'locales' => [$this->defaultLocale => 'SuperTees', 'es_ES' => 'SuperCamisetas']],
42
                ['code' => 'TX6', 'locales' => [$this->defaultLocale => 'Stickypicky', 'es_ES' => 'Pegapicky']],
43
                ['code' => 'TX7', 'locales' => [$this->defaultLocale => 'Mugland', 'es_ES' => 'Mundotaza']],
44
                ['code' => 'TX8', 'locales' => [$this->defaultLocale => 'Bookmania', 'es_ES' => 'Libromania']],
45
            ]));
46
47
        $manager->flush();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getOrder()
54
    {
55
        return 10;
56
    }
57
58
    /**
59
     * Create and save taxon with given taxons.
60
     *
61
     * @param string $code
62
     * @param array $rootTaxonName
63
     * @param array $childrenArray
64
     *
65
     * @return TaxonInterface
66
     */
67
    protected function createTaxons($code, array $rootTaxonName, array $childrenArray)
68
    {
69
        /* @var $rootTaxon TaxonInterface */
70
        $rootTaxon = $this->getTaxonFactory()->createNew();
71
        $rootTaxon->setCode($code);
72
73
        foreach ($rootTaxonName as $locale => $name) {
74
            $rootTaxon->setCurrentLocale($locale);
75
            $rootTaxon->setFallbackLocale($locale);
76
            $rootTaxon->setName($name);
77
78
            if ($this->defaultLocale === $locale) {
79
                $this->setReference('Sylius.Taxon.'.$name, $rootTaxon);
80
            }
81
        }
82
83
        foreach ($childrenArray as $taxonArray) {
84
            /* @var $taxon TaxonInterface */
85
            $taxon = $this->getTaxonFactory()->createNew();
86
            $taxon->setCode($taxonArray['code']);
87
88
            foreach ($taxonArray['locales'] as $locale => $taxonName) {
89
                $taxon->setCurrentLocale($locale);
90
                $taxon->setFallbackLocale($locale);
91
                $taxon->setName($taxonName);
92
                $taxon->setDescription($this->fakers[$locale]->paragraph);
93
94
                if ($this->defaultLocale === $locale) {
95
                    $this->setReference('Sylius.Taxon.'.$taxonName, $taxon);
96
                }
97
            }
98
99
            $rootTaxon->addChild($taxon);
100
        }
101
102
        return $rootTaxon;
103
    }
104
}
105