PaymentOrderMailLinkGenerator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 72
rs 10
c 2
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generateContactMailLink() 0 13 3
A getHHVMailLink() 0 20 2
A getSubject() 0 9 1
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;
20
21
use App\Controller\Admin\PaymentOrderCrudController;
22
use App\Entity\PaymentOrder;
23
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
24
use SteveGrunwell\MailToLinkFormatter\MailTo;
25
use Symfony\Contracts\Translation\TranslatorInterface;
26
27
/**
28
 * This service generates email links for a payment order, including adresses, subject and body.
29
 */
30
class PaymentOrderMailLinkGenerator
31
{
32
    private $translator;
33
    private $adminURLGenerator;
34
35
    private $hhv_email;
36
37
    public function __construct(TranslatorInterface $translator, AdminUrlGenerator $adminUrlGenerator, string $hhv_email)
38
    {
39
        $this->translator = $translator;
40
        $this->hhv_email = $hhv_email;
41
        $this->adminURLGenerator = $adminUrlGenerator;
42
    }
43
44
    /**
45
     * Generates a "mailto:" string to contact the HHV for the given payment order. It includes a link to the payment
46
     * order.
47
     * If no payment order is passed (null) only the mailto: link for the HHV email is returned.
48
     */
49
    public function getHHVMailLink(?PaymentOrder $paymentOrder = null): string
50
    {
51
        $mailTo = new MailTo();
52
        $mailTo->setRecipients($this->hhv_email);
53
54
        if (null !== $paymentOrder) {
55
            //Add subject
56
            $mailTo->setHeader('subject', $this->getSubject($paymentOrder));
57
58
            $content = 'Link: '.
59
                $this->adminURLGenerator->setController(PaymentOrderCrudController::class)
60
                    ->setEntityId($paymentOrder->getId())
61
                    ->setAction('detail')
62
                    ->removeReferrer()
63
                    ->unset('filters');
64
65
            $mailTo->setBody($content);
66
        }
67
68
        return $mailTo->getLink();
69
    }
70
71
    /**
72
     * Generates a "mailto:" string to contact the responsible people for the given payment order.
73
     * Returns null, if no contact emails are associated with the department.
74
     */
75
    public function generateContactMailLink(PaymentOrder $paymentOrder): string
76
    {
77
        $mailTo = new MailTo();
78
79
        if (!empty($paymentOrder->getContactEmail())) {
80
            $mailTo->setRecipients($paymentOrder->getContactEmail());
81
        } elseif (!empty($paymentOrder->getDepartment()->getContactEmails())) {
82
            $mailTo->setRecipients($paymentOrder->getDepartment()->getContactEmails());
83
        }
84
85
        $mailTo->setHeader('subject', $this->getSubject($paymentOrder));
86
87
        return $mailTo->getLink();
88
    }
89
90
    /**
91
     * Determines a good subject for an email.
92
     */
93
    protected function getSubject(PaymentOrder $paymentOrder): string
94
    {
95
        return sprintf(
96
            '%s - %s: %s [%s]',
97
            $this->translator->trans('payment_order.mail.subject'),
98
            $paymentOrder->getDepartment()
99
                ->getName(),
100
            $paymentOrder->getProjectName(),
101
            $paymentOrder->getIDString()
102
        );
103
    }
104
}
105