Passed
Pull Request — master (#27)
by Florian
03:25
created

exportAuthenticationLinksAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 16
rs 9.9666
cc 2
nc 2
nop 1
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\Controller;
13
14
use App\Controller\Administration\Base\BaseController;
15
use App\Entity\Reminder;
16
use App\Form\Reminder\ReminderType;
17
use App\Form\Type\SemesterType;
18
use App\Service\Interfaces\EmailServiceInterface;
19
use App\Service\Interfaces\EvaluationServiceInterface;
20
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\Routing\Annotation\Route;
24
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
25
use Symfony\Contracts\Translation\TranslatorInterface;
26
27
/**
28
 * @Route("/administration")
29
 */
30
class AdministrationController extends BaseController
31
{
32
    /**
33
     * @Route("", name="administration")
34
     *
35
     * @return Response
36
     */
37
    public function indexAction(EvaluationServiceInterface $evaluationService)
38
    {
39
        $semesterEvaluation = $evaluationService->getActiveSemesterEvaluation();
40
        $currentSemester = SemesterType::getCurrentSemester();
41
42
        return $this->render('administration.html.twig', ['semesterEvaluation' => $semesterEvaluation, 'currentSemester' => $currentSemester]);
43
    }
44
45
    /**
46
     * @Route("/send_reminder", name="administration_send_reminder")
47
     *
48
     * @return Response
49
     */
50
    public function sendReminder(Request $request, EmailServiceInterface $emailService, TranslatorInterface $translator, EvaluationServiceInterface $evaluationService)
51
    {
52
        $reminderEMail = $this->getOrCreateReminder($translator);
53
54
        $myOnSuccessCallable = function ($form) use ($reminderEMail, $emailService, $translator, $evaluationService, &$saved) {
55
            $this->fastSave($reminderEMail);
56
57
            if (mb_strpos($reminderEMail->getBody(), '(url)') === false) {
58
                $error = $this->getTranslator()->trans('send_reminder.error.no_url_placeholder', [], 'administration');
59
                $this->displayError($error);
60
61
                return $form;
62
            }
63
64
            if (mb_strpos($reminderEMail->getBody(), '(name)') === false) {
65
                $error = $this->getTranslator()->trans('send_reminder.error.no_name_placeholder', [], 'administration');
66
                $this->displayError($error);
67
68
                return $form;
69
            }
70
71
            $missingOrganisations = $evaluationService->getActiveSemesterEvaluation()->getMissingOrganisations();
72
            foreach ($missingOrganisations as $organisation) {
73
                $url = $this->generateUrl('login_code', ['code' => $organisation->getAuthenticationCode()], UrlGeneratorInterface::ABSOLUTE_URL);
74
                $body = str_replace('(url)', $url, $reminderEMail->getBody());
75
                $body = str_replace('(name)', $organisation->getName(), $body);
76
77
                $emailService->sendEmail($organisation->getEmail(), $reminderEMail->getSubject(), $body);
78
            }
79
80
            $success = $translator->trans('send_reminder.success.sent', ['%count%' => \count($missingOrganisations)], 'administration');
81
            $this->displaySuccess($success);
82
83
            return $this->redirectToRoute('administration');
84
        };
85
86
        $buttonLabel = $translator->trans('send_reminder.send', [], 'administration');
87
        $myForm = $this->handleForm(
88
            $this->createForm(ReminderType::class, $reminderEMail)
89
                ->add('submit', SubmitType::class, ['label' => $buttonLabel, 'translation_domain' => false]),
90
            $request,
91
            $myOnSuccessCallable
92
        );
93
94
        if ($myForm instanceof Response) {
95
            return $myForm;
96
        }
97
98
        return $this->render('administration/send_reminder.html.twig', ['form' => $myForm->createView()]);
99
    }
100
101
    public function getOrCreateReminder(TranslatorInterface $translator): Reminder
102
    {
103
        /** @var Reminder|null $reminderEMail */
104
        $reminderEMail = $this->getDoctrine()->getRepository(Reminder::class)->findOneBy([]);
105
        if ($reminderEMail !== null) {
106
            return $reminderEMail;
107
        }
108
109
        $reminderEMail = new Reminder();
110
111
        $subject = $translator->trans('send_reminder.default.subject', [], 'administration');
112
        $reminderEMail->setSubject($subject);
113
114
        $body = $translator->trans('send_reminder.default.body', [], 'administration');
115
        $reminderEMail->setBody($body);
116
117
        $this->fastSave($reminderEMail);
118
119
        return $reminderEMail;
120
    }
121
}
122