LoadParticipants::load()   B
last analyzed

Complexity

Conditions 8
Paths 15

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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