Completed
Push — master ( 72bee5...0c35d9 )
by Florian
04:13
created

LoadParticipants::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the feedback 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\DataFixtures\Base\BaseFixture;
15
use App\Entity\Answer;
16
use App\Entity\Event;
17
use App\Entity\Participant;
18
use Doctrine\Common\Persistence\ObjectManager;
19
use Symfony\Component\Serializer\SerializerInterface;
20
21
class LoadParticipants extends BaseFixture
22
{
23
    const ORDER = LoadEvent::ORDER + 1;
24
25
    /**
26
     * @var SerializerInterface
27
     */
28
    private $serializer;
29
30
    /**
31
     * LoadEvent constructor.
32
     *
33
     * @param SerializerInterface $serializer
34
     */
35
    public function __construct(SerializerInterface $serializer)
36
    {
37
        $this->serializer = $serializer;
38
    }
39
40
    /**
41
     * Load data fixtures with the passed EntityManager.
42
     *
43
     * @param ObjectManager $manager
44
     */
45
    public function load(ObjectManager $manager)
46
    {
47
        //prepare events
48
        $events = $manager->getRepository(Event::class)->findAll();
49
        $now = (new \DateTime())->format('Y-m-d');
50
51
        //prepare participants
52
        $dir = file_get_contents(__DIR__ . '/Resources/participants');
53
        $fileNames = scandir($dir);
54
        /** @var Answer[][] $participantJson */
55
        $participantJson = [];
56
        foreach ($fileNames as $fileName) {
57
            if ($fileName === '.' || $fileName === '..') {
58
                continue;
59
            }
60
            $participantJson[] = file_get_contents($dir . '/' . $fileName);
61
        }
62
63
        //add participants to all events
64
        foreach ($events as $event) {
65
            //skip if not yet happened
66
            if ($event->getDate() >= $now) {
67
                continue;
68
            }
69
70
            foreach ($participantJson as $json) {
71
                $participant = new Participant();
72
                $participant->setIdentifier(uniqid());
73
                $participant->setEvent($event);
74
                $participant->setTimeNeededInSeconds(rand(0, 60 * 3));
75
76
                /** @var Answer[] $answers */
77
                $answers = $this->serializer->deserialize($json, Answer::class . '[]', 'json');
78
                foreach ($answers as $answer) {
79
                    $answer->setParticipant($participant);
80
                    $manager->persist($answer);
81
                }
82
                $manager->persist($participant);
83
            }
84
        }
85
        $manager->flush();
86
    }
87
88
    /**
89
     * Get the order of this fixture.
90
     *
91
     * @return int
92
     */
93
    public function getOrder()
94
    {
95
        return static::ORDER;
96
    }
97
}
98