Passed
Push — master ( d26004...ab777b )
by Jan
04:54
created

PartFixtures::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\DataFixtures;
23
24
25
use App\Entity\Attachments\AttachmentType;
26
use App\Entity\Attachments\PartAttachment;
27
use App\Entity\Parts\Category;
28
use App\Entity\Parts\Footprint;
29
use App\Entity\Parts\Manufacturer;
30
use App\Entity\Parts\Part;
31
use App\Entity\Parts\PartLot;
32
use App\Entity\Parts\Storelocation;
33
use App\Entity\Parts\Supplier;
34
use App\Entity\PriceInformations\Orderdetail;
35
use App\Entity\PriceInformations\Pricedetail;
36
use Doctrine\Bundle\FixturesBundle\Fixture;
37
use Doctrine\ORM\EntityManagerInterface;
38
use Doctrine\Persistence\ObjectManager;
39
40
class PartFixtures extends Fixture
41
{
42
43
    protected $em;
44
45
    public function __construct(EntityManagerInterface $entityManager)
46
    {
47
        $this->em = $entityManager;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function load(ObjectManager $manager)
54
    {
55
        $table_name = $this->em->getClassMetadata(Part::class)->getTableName();
0 ignored issues
show
Bug introduced by
The method getTableName() does not exist on Doctrine\Persistence\Mapping\ClassMetadata. It seems like you code against a sub-type of Doctrine\Persistence\Mapping\ClassMetadata such as Doctrine\ORM\Mapping\ClassMetadataInfo. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        $table_name = $this->em->getClassMetadata(Part::class)->/** @scrutinizer ignore-call */ getTableName();
Loading history...
56
        $this->em->getConnection()->exec("ALTER TABLE `${table_name}` AUTO_INCREMENT = 1;");
57
58
        /** Simple part */
59
        $part = new Part();
60
        $part->setName('Part 1');
61
        $part->setCategory($manager->find(Category::class, 1));
0 ignored issues
show
Bug introduced by
It seems like $manager->find(App\Entit...rts\Category::class, 1) can also be of type null; however, parameter $category of App\Entity\Parts\Part::setCategory() does only seem to accept App\Entity\Parts\Category, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        $part->setCategory(/** @scrutinizer ignore-type */ $manager->find(Category::class, 1));
Loading history...
62
        $manager->persist($part);
63
64
        /** More complex part */
65
        $part = new Part();
66
        $part->setName('Part 2');
67
        $part->setCategory($manager->find(Category::class, 1));
68
        $part->setFootprint($manager->find(Footprint::class, 1));
69
        $part->setManufacturer($manager->find(Manufacturer::class, 1));
70
        $part->setTags('test, Test, Part2');
71
        $part->setMass(100.2);
72
        $part->setNeedsReview(true);
73
        $part->setManufacturingStatus('active');
74
        $manager->persist($part);
75
76
        /** Part with orderdetails, storelocations and Attachments */
77
        $part = new Part();
78
        $part->setFavorite(true);
79
        $part->setName('Part 2');
80
        $part->setCategory($manager->find(Category::class, 1));
81
        $partLot1 = new PartLot();
82
        $partLot1->setAmount(1.0);
83
        $partLot1->setStorageLocation($manager->find(Storelocation::class, 1));
84
        $part->addPartLot($partLot1);
85
86
        $partLot2 = new PartLot();
87
        $partLot2->setExpirationDate(new \DateTime());
88
        $partLot2->setComment('Test');
89
        $partLot2->setNeedsRefill(true);
90
        $partLot2->setStorageLocation($manager->find(Storelocation::class, 3));
91
        $part->addPartLot($partLot2);
92
93
        $orderdetail = new Orderdetail();
94
        $orderdetail->setSupplier($manager->find(Supplier::class, 1));
0 ignored issues
show
Bug introduced by
It seems like $manager->find(App\Entit...rts\Supplier::class, 1) can also be of type null; however, parameter $new_supplier of App\Entity\PriceInformat...erdetail::setSupplier() does only seem to accept App\Entity\Parts\Supplier, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
        $orderdetail->setSupplier(/** @scrutinizer ignore-type */ $manager->find(Supplier::class, 1));
Loading history...
95
        $orderdetail->addPricedetail((new Pricedetail())->setPriceRelatedQuantity(1.0)->setPrice(10));
96
        $orderdetail->addPricedetail((new Pricedetail())->setPriceRelatedQuantity(10.0)->setPrice(15));
97
        $part->addOrderdetail($orderdetail);
98
99
        $orderdetail = new Orderdetail();
100
        $orderdetail->setSupplierpartnr('BC 547');
101
        $orderdetail->setObsolete(true);
102
        $orderdetail->setSupplier($manager->find(Supplier::class, 1));
103
        $orderdetail->addPricedetail((new Pricedetail())->setPriceRelatedQuantity(1.0)->setPrice(10));
104
        $orderdetail->addPricedetail((new Pricedetail())->setPriceRelatedQuantity(10.0)->setPrice(15));
105
        $part->addOrderdetail($orderdetail);
106
107
        $attachment = new PartAttachment();
108
        $attachment->setName('TestAttachment');
109
        $attachment->setURL('www.foo.bar');
110
        $attachment->setAttachmentType($manager->find(AttachmentType::class, 1));
0 ignored issues
show
Bug introduced by
It seems like $manager->find(App\Entit...tachmentType::class, 1) can also be of type null; however, parameter $attachement_type of App\Entity\Attachments\A...nt::setAttachmentType() does only seem to accept App\Entity\Attachments\AttachmentType, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
        $attachment->setAttachmentType(/** @scrutinizer ignore-type */ $manager->find(AttachmentType::class, 1));
Loading history...
111
        $part->addAttachment($attachment);
112
113
        $attachment = new PartAttachment();
114
        $attachment->setName('Test2');
115
        $attachment->setPath('invalid');
116
        $attachment->setShowInTable(true);
117
        $attachment->setAttachmentType($manager->find(AttachmentType::class, 1));
118
        $part->addAttachment($attachment);
119
120
        $manager->persist($part);
121
        $manager->flush();
122
    }
123
}