Completed
Push — master ( 5e4ebc...f96e76 )
by Florian
13s queued 11s
created

EvaluationService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 27
c 1
b 0
f 0
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getOrganisationEvaluationsForSemester() 0 23 5
A getSemesterEvaluation() 0 5 1
A getActiveSemesterEvaluation() 0 13 3
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
        $organisations = $this->doctrine->getRepository(Organisation::class)->findActive();
46
        $missingOrganisations = [];
47
        foreach ($organisations as $organisation) {
48
            if (!isset($organisationEvaluations[$organisation->getId()])) {
49
                $missingOrganisations[] = $organisation;
50
            }
51
        }
52
53
        return new SemesterEvaluation($organisationEvaluations, $missingOrganisations);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getSemesterEvaluation(int $semester)
60
    {
61
        $organisationEvaluations = $this->getOrganisationEvaluationsForSemester($semester);
62
63
        return new SemesterEvaluation($organisationEvaluations, []);
64
    }
65
66
    private function getOrganisationEvaluationsForSemester(int $semester)
67
    {
68
        $events = $this->doctrine->getRepository(Event::class)->findBy(['semester' => $semester]);
69
        $eventsByOrganisation = [];
70
        foreach ($events as $event) {
71
            $id = $event->getOrganisation()->getId();
72
            if (!isset($eventsByOrganisation[$id])) {
73
                $eventsByOrganisation[$id] = [];
74
            }
75
76
            $eventsByOrganisation[$id][] = $event;
77
        }
78
79
        $semesterReports = $this->doctrine->getRepository(SemesterReport::class)->findBy(['semester' => $semester]);
80
        $organisationEvaluations = [];
81
        foreach ($semesterReports as $semesterReport) {
82
            $organisationId = $semesterReport->getOrganisation()->getId();
83
            $events = isset($eventsByOrganisation[$organisationId]) ? $eventsByOrganisation[$organisationId] : [];
84
            $organisationEvaluation = new OrganisationEvaluation($semesterReport->getOrganisation(), $semesterReport, $events);
85
            $organisationEvaluations[$organisationId] = $organisationEvaluation;
86
        }
87
88
        return $organisationEvaluations;
89
    }
90
}
91