Passed
Push — master ( c64209...e51ff5 )
by Florian
03:00
created

EventFixtures::load()   B

Complexity

Conditions 7
Paths 33

Size

Total Lines 66
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 37
c 1
b 0
f 1
dl 0
loc 66
rs 8.3946
cc 7
nc 33
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the TheAlternativeZurich/events project.
5
 *
6
 * (c) Florian Moser <[email protected]>
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 App\DataFixtures;
13
14
use App\Entity\Event;
15
use App\Entity\Registration;
16
use App\Entity\User;
17
use Doctrine\Bundle\FixturesBundle\Fixture;
18
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
19
use Doctrine\Persistence\ObjectManager;
20
21
class EventFixtures extends Fixture implements OrderedFixtureInterface
22
{
23
    const ORDER = UserFixtures::ORDER + 1;
24
25
    /**
26
     * @throws \Exception
27
     */
28
    public function load(ObjectManager $manager)
29
    {
30
        /** @var User $user */
31
        $user = $this->getReference(UserFixtures::TESTER_REFERENCE);
32
33
        $entries = [
34
            [
35
                ['TheAlternative', 'Console Toolkit 2020', 'Learn how to master the console!', '2020-12-22T18:00:00', null], // event details
36
                [null, null, null, false], // registration restrictions: no restrictions
37
            ],
38
            [
39
                ['TheAlternative', 'Free Software & Open Source 2020', 'The basics about Free Software & Open Source and why you should use it.', '2020-12-26T18:00:00', '2020-12-26T20:00:00'], // event details
40
                [10, null, null, false], // registration restrictions: max 10 participants
41
            ],
42
            [
43
                ['TheAlternative', 'Bash Workshops 2020', 'Bash commands & Linux Basics to master your studies.', '2020-12-26T18:00:00', null], // event details
44
                [10, '2019-12-26T18:00:00', '2022-12-26T18:00:00', false], // registration restrictions: open
45
            ],
46
            [
47
                ['TheAlternative', 'Bash Workshops 2019', 'Bash commands & Linux Basics to master your studies.', '2020-12-26T18:00:00', null], // event details
48
                [10, '2019-12-26T18:00:00', '2019-12-26T20:00:00', false], // registration restrictions: closed
49
            ],
50
            [
51
                ['TheAlternative', 'Console Toolkit 2019', 'Learn how to master the console!', '2020-12-22T18:00:00', null], // event details
52
                [null, null, null, true], // registration restrictions: closed
53
            ],
54
            [
55
                ['TheAlternative', 'Free Software & Open Source 2019', 'The basics about Free Software & Open Source and why you should use it.', '2020-12-26T18:00:00', '2020-12-26T20:00:00'], // event details
56
                [null, null, null, true], // registration restrictions: max 10 participants
57
            ],
58
        ];
59
60
        foreach ($entries as $entry) {
61
            $event = new Event();
62
63
            $eventDetails = $entry[0];
64
            $event->setOrganizer($eventDetails[0]);
65
            $event->setName($eventDetails[1]);
66
            $event->setDescription($eventDetails[2]);
67
            $event->setStartDate(new \DateTime($eventDetails[3]));
68
            if (null !== $eventDetails[4]) {
69
                $event->setEndDate(new \DateTime($eventDetails[4]));
70
            }
71
72
            $registrationRestrictions = $entry[1];
73
            if (null !== $registrationRestrictions[0]) {
74
                $event->setMaximumAttendeeCapacity($registrationRestrictions[0]);
75
            }
76
            if (null !== $registrationRestrictions[1]) {
77
                $event->setRegistrationOpen(new \DateTime($registrationRestrictions[1]));
78
            }
79
            if (null !== $registrationRestrictions[2]) {
80
                $event->setRegistrationClose(new \DateTime($registrationRestrictions[2]));
81
            }
82
            if ($registrationRestrictions[3]) {
83
                $event->close();
84
            }
85
86
            $manager->getRepository(Event::class)->save($event);
87
88
            $registration = Registration::createFromUser($event, $user, true);
89
            $registrationRepository = $manager->getRepository(Registration::class);
90
            $registrationRepository->save($registration);
91
        }
92
93
        $manager->flush();
94
    }
95
96
    public function getOrder()
97
    {
98
        return self::ORDER;
99
    }
100
}
101