ManualConfirmationHelper::confirmManually()   B
last analyzed

Complexity

Conditions 7
Paths 18

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
nc 18
nop 3
dl 0
loc 31
rs 8.8333
c 2
b 0
f 0
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Services\EmailConfirmation;
20
21
use App\Entity\PaymentOrder;
22
use App\Entity\User;
23
use Carbon\Carbon;
24
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
25
use Symfony\Component\Mailer\MailerInterface;
26
use Symfony\Component\Mime\Email;
27
use Symfony\Component\Security\Core\Security;
28
use Symfony\Contracts\Translation\TranslatorInterface;
29
30
class ManualConfirmationHelper
31
{
32
    private $security;
33
    private $notifications_risky;
34
    private $fsb_email;
35
    private $hhv_email;
36
    private $translator;
37
    private $mailer;
38
39
    public function __construct(Security $security, TranslatorInterface $translator, MailerInterface $mailer,
40
        array $notifications_risky, string $fsb_email, string $hhv_email)
41
    {
42
        $this->security = $security;
43
        $this->notifications_risky = array_filter($notifications_risky);
44
        $this->fsb_email = $fsb_email;
45
        $this->hhv_email = $hhv_email;
46
        $this->translator = $translator;
47
48
        $this->mailer = $mailer;
49
    }
50
51
    /**
52
     * Confirm the given PaymentOrder manually. The user, datetime and reason for this is logged.
53
     * Generates an comment, sends an email to confirmation people and confirm the payment order.
54
     * The DB is not flushed, so you have to do this outside.
55
     *
56
     * @param User|null $user Specify the user that should be shown in email/comment. If null the current user is used.
57
     */
58
    public function confirmManually(PaymentOrder $paymentOrder, string $reason, ?User $user = null): void
59
    {
60
        if ($paymentOrder->isConfirmed()) {
61
            throw new \RuntimeException('You can not manually confirm an already confirmed payment order!');
62
        }
63
64
        if (null === $user) {
65
            if (!$this->security->getUser() instanceof User) {
66
                throw new \RuntimeException('$user must be an User entity object!');
67
            }
68
            $user = $this->security->getUser();
69
        }
70
71
        //Add a comment about the manual confirmation
72
        $tmp = $paymentOrder->getComment();
73
        //Add line breaks if comment is not empty.
74
        if (!empty($tmp)) {
75
            $tmp .= '<br><br>';
76
        }
77
        $tmp .= $this->generateComment($paymentOrder, $reason, $user);
78
        $paymentOrder->setComment($tmp);
79
80
        //Send emails that payment order we manually confirmed
81
        $this->sendNotification($paymentOrder, $reason, $user);
82
83
        //Do the confirmation process where it was not needed
84
        if (null === $paymentOrder->getConfirm1Timestamp()) {
85
            $paymentOrder->setConfirm1Timestamp(new \DateTime());
86
        }
87
        if (null === $paymentOrder->getConfirm2Timestamp()) {
88
            $paymentOrder->setConfirm2Timestamp(new \DateTime());
89
        }
90
    }
91
92
    private function sendNotification(PaymentOrder $paymentOrder, string $reason, User $user): void
93
    {
94
        //We can not continue if the payment order is not serialized / has an ID (as we cannot generate an URL for it)
95
        if (null === $paymentOrder->getId()) {
0 ignored issues
show
introduced by
The condition null === $paymentOrder->getId() is always false.
Loading history...
96
            throw new \RuntimeException('$paymentOrder must be serialized / have an ID so than an confirmation URL can be generated!');
97
        }
98
99
        $email = new TemplatedEmail();
100
101
        $email->priority(Email::PRIORITY_HIGHEST);
102
        $email->replyTo($paymentOrder->getDepartment()->isFSR() ? $this->fsb_email : $this->hhv_email);
103
104
        $email->subject(
105
            $this->translator->trans(
106
                'payment_order.manual_confirmation.email.subject',
107
                [
108
                    '%project%' => $paymentOrder->getProjectName(),
109
                ]
110
            ));
111
112
        $email->htmlTemplate('mails/manual_confirmation.html.twig');
113
        $email->context([
114
            'payment_order' => $paymentOrder,
115
            'reason' => $reason,
116
            'user' => $user,
117
        ]);
118
119
        //Add confirmation 1 people
120
        $email->addBcc(...$paymentOrder->getDepartment()->getEmailHhv());
121
        //Add confirmation 2 people
122
        $email->addBcc(...$paymentOrder->getDepartment()->getEmailTreasurer());
123
124
        //Add risky notification people
125
        $email->addBcc(...$this->notifications_risky);
126
127
        $this->mailer->send($email);
128
    }
129
130
    private function generateComment(PaymentOrder $paymentOrder, string $reason, User $user): string
0 ignored issues
show
Unused Code introduced by
The parameter $paymentOrder is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

130
    private function generateComment(/** @scrutinizer ignore-unused */ PaymentOrder $paymentOrder, string $reason, User $user): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
    {
132
        $date = Carbon::now()->toDateTimeLocalString();
133
134
        return '<h4>Manuelle Bestätigung</h4>'
135
            .'durch '.$user->getFullName().' ('.$user->getUsername().'), '.$date.'<br>'
136
            .'<b>Begründung: </b>'.$reason;
137
    }
138
}
139