Passed
Push — master ( 7e8752...dd1dc5 )
by Jan
04:19
created

DataStructureFixtures::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\DataFixtures;
33
34
35
use App\Entity\Attachments\AttachmentType;
36
use App\Entity\Base\StructuralDBElement;
37
use App\Entity\Devices\Device;
38
use App\Entity\Parts\Category;
39
use App\Entity\Parts\Footprint;
40
use App\Entity\Parts\Manufacturer;
41
use App\Entity\Parts\MeasurementUnit;
42
use App\Entity\Parts\Storelocation;
43
use App\Entity\Parts\Supplier;
44
use App\Entity\PriceInformations\Currency;
45
use Doctrine\Bundle\FixturesBundle\Fixture;
46
use Doctrine\Common\Persistence\ObjectManager;
47
use Doctrine\ORM\EntityManagerInterface;
48
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
49
50
class DataStructureFixtures extends Fixture
51
{
52
53
    protected $em;
54
55
    public function __construct(EntityManagerInterface $entityManager)
56
    {
57
        $this->em = $entityManager;
58
    }
59
60
    /**
61
     * Load data fixtures with the passed EntityManager
62
     *
63
     * @param ObjectManager $manager
64
     */
65
    public function load(ObjectManager $manager)
66
    {
67
        //Reset autoincrement
68
        $types = [AttachmentType::class, Device::class, Category::class, Footprint::class, Manufacturer::class,
69
            MeasurementUnit::class, Storelocation::class, Supplier::class];
70
71
        foreach ($types as $type) {
72
            $this->createNodesForClass($type, $manager);
73
        }
74
75
        $manager->flush();
76
    }
77
78
    /**
79
     * Creates a datafixture with serveral nodes for the given class.
80
     * @param string $class The class for which the nodes should be generated (must be a StructuralDBElement child)
81
     * @param ObjectManager $manager The ObjectManager that should be used to persist the nodes
82
     */
83
    public function createNodesForClass(string $class, ObjectManager $manager)
84
    {
85
        if (!new $class() instanceof StructuralDBElement) {
86
            throw new \InvalidArgumentException('$class must be a StructuralDBElement!');
87
        }
88
89
        $table_name = $this->em->getClassMetadata($class)->getTableName();
90
        $this->em->getConnection()->exec("ALTER TABLE `$table_name` AUTO_INCREMENT = 1;");
91
92
        /** @var StructuralDBElement $node1 */
93
        $node1 = new $class();
94
        $node1->setName('Node 1');
95
96
        /** @var StructuralDBElement $node2 */
97
        $node2 = new $class();
98
        $node2->setName('Node 2');
99
100
        /** @var StructuralDBElement $node3 */
101
        $node3 = new $class();
102
        $node3->setName('Node 3');
103
104
        $node1_1 = new $class();
105
        $node1_1->setName('Node 1.1');
106
        $node1_1->setParent($node1);
107
108
        $node1_2 = new $class();
109
        $node1_2->setName('Node 1.2');
110
        $node1_2->setParent($node1);
111
112
        $node2_1 = new $class();
113
        $node2_1->setName('Node 2.1');
114
        $node2_1->setParent($node2);
115
116
        $node1_1_1 = new $class();
117
        $node1_1_1->setName('Node 1.1.1');
118
        $node1_1_1->setParent($node1_1);
119
120
        $manager->persist($node1);
121
        $manager->persist($node2);
122
        $manager->persist($node3);
123
        $manager->persist($node1_1);
124
        $manager->persist($node1_2);
125
        $manager->persist($node2_1);
126
        $manager->persist($node1_1_1);
127
    }
128
}