EvaluationService::getActiveSemesterEvaluation()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 17
rs 9.9332
cc 3
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of the vseth-semesterly-reports 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\Service;
13
14
use App\Entity\Event;
15
use App\Entity\Organisation;
16
use App\Entity\SemesterReport;
17
use App\Form\Type\SemesterType;
18
use App\Model\SemesterEvaluation;
19
use App\Model\SemesterEvaluation\OrganisationEvaluation;
20
use App\Service\Interfaces\EvaluationServiceInterface;
21
use Doctrine\Persistence\ManagerRegistry;
22
23
class EvaluationService implements EvaluationServiceInterface
24
{
25
    /**
26
     * @var ManagerRegistry
27
     */
28
    private $doctrine;
29
30
    /**
31
     * EvaluationService constructor.
32
     */
33
    public function __construct(ManagerRegistry $doctrine)
34
    {
35
        $this->doctrine = $doctrine;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getActiveSemesterEvaluation()
42
    {
43
        $organisationEvaluations = $this->getOrganisationEvaluationsForSemester(SemesterType::getCurrentSemester());
44
45
        /** @var Organisation[] $organisations */
46
        $organisations = $this->doctrine->getRepository(Organisation::class)->findActive();
47
        $orderedEvaluations = [];
48
        $missingOrganisations = [];
49
        foreach ($organisations as $organisation) {
50
            if (!isset($organisationEvaluations[$organisation->getId()])) {
51
                $missingOrganisations[] = $organisation;
52
            } else {
53
                $orderedEvaluations[] = $organisationEvaluations[$organisation->getId()];
54
            }
55
        }
56
57
        return new SemesterEvaluation($orderedEvaluations, $missingOrganisations);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getSemesterEvaluation(int $semester)
64
    {
65
        $organisationEvaluations = $this->getOrganisationEvaluationsForSemester($semester);
66
67
        return new SemesterEvaluation($organisationEvaluations, []);
68
    }
69
70
    private function getOrganisationEvaluationsForSemester(int $semester)
71
    {
72
        $events = $this->doctrine->getRepository(Event::class)->findBy(['semester' => $semester]);
73
        $eventsByOrganisation = [];
74
        foreach ($events as $event) {
75
            $id = $event->getOrganisation()->getId();
76
            if (!isset($eventsByOrganisation[$id])) {
77
                $eventsByOrganisation[$id] = [];
78
            }
79
80
            $eventsByOrganisation[$id][] = $event;
81
        }
82
83
        /** @var SemesterReport[] $semesterReports */
84
        $semesterReports = $this->doctrine->getRepository(SemesterReport::class)->findBy(['semester' => $semester]);
85
        $organisationEvaluations = [];
86
        foreach ($semesterReports as $semesterReport) {
87
            $organisationId = $semesterReport->getOrganisation()->getId();
88
            $events = isset($eventsByOrganisation[$organisationId]) ? $eventsByOrganisation[$organisationId] : [];
89
            $organisationEvaluation = new OrganisationEvaluation($semesterReport->getOrganisation(), $semesterReport, $events);
90
            $organisationEvaluations[$organisationId] = $organisationEvaluation;
91
        }
92
93
        return $organisationEvaluations;
94
    }
95
}
96