Passed
Push — master ( df9557...e5317d )
by Jan
04:48 queued 11s
created

generateContactMailLink()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 15
rs 9.9666
c 1
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;
20
21
use App\Controller\Admin\PaymentOrderCrudController;
22
use App\Entity\PaymentOrder;
23
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
24
use EasyCorp\Bundle\EasyAdminBundle\Router\CrudUrlGenerator;
25
use LogicException;
26
use SteveGrunwell\MailToLinkFormatter\MailTo;
27
use Symfony\Contracts\Translation\TranslatorInterface;
28
29
/**
30
 * This service generates email links for a payment order, including adresses, subject and body.
31
 */
32
class PaymentOrderMailLinkGenerator
33
{
34
    private $translator;
35
    private $crudUrlGenerator;
0 ignored issues
show
introduced by
The private property $crudUrlGenerator is not used, and could be removed.
Loading history...
36
37
    private $hhv_email;
38
39
    public function __construct(TranslatorInterface $translator, AdminUrlGenerator $adminUrlGenerator, string $hhv_email)
40
    {
41
        $this->translator = $translator;
42
        $this->hhv_email = $hhv_email;
43
        $this->adminURLGenerator = $adminUrlGenerator;
0 ignored issues
show
Bug Best Practice introduced by
The property adminURLGenerator does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
    }
45
46
    /**
47
     * Generates a "mailto:" string to contact the HHV for the given payment order. It includes a link to the payment
48
     * order.
49
     * If no payment order is passed (null) only the mailto: link for the HHV email is returned.
50
     */
51
    public function getHHVMailLink(?PaymentOrder $paymentOrder = null): string
52
    {
53
        $mailTo = new MailTo();
54
        $mailTo->setRecipients($this->hhv_email);
55
56
        if (null !== $paymentOrder) {
57
            //Add subject
58
            $mailTo->setHeader('subject', $this->getSubject($paymentOrder));
59
60
            $content = 'Link: '.
61
                $this->adminURLGenerator->setController(PaymentOrderCrudController::class)
62
                    ->setEntityId($paymentOrder->getId())
63
                    ->setAction('detail')
64
                    ->removeReferrer()
65
                    ->unset('filters');
66
67
            $mailTo->setBody($content);
68
        }
69
70
        return $mailTo->getLink();
71
    }
72
73
    /**
74
     * Generates a "mailto:" string to contact the responsible people for the given payment order.
75
     * Returns null, if no contact emails are associated with the department.
76
     */
77
    public function generateContactMailLink(PaymentOrder $paymentOrder): string
78
    {
79
        $mailTo = new MailTo();
80
81
        if (!empty($paymentOrder->getContactEmail())) {
82
            $mailTo->setRecipients($paymentOrder->getContactEmail());
83
        } elseif (!empty($paymentOrder->getDepartment()->getContactEmails())) {
84
            $mailTo->setRecipients($paymentOrder->getDepartment()->getContactEmails());
85
        } else {
86
            throw new LogicException('No recipeint could be determined for this payment order!');
87
        }
88
89
        $mailTo->setHeader('subject', $this->getSubject($paymentOrder));
90
91
        return $mailTo->getLink();
92
    }
93
94
    /**
95
     * Determines a good subject for an email.
96
     */
97
    protected function getSubject(PaymentOrder $paymentOrder): string
98
    {
99
        return sprintf(
100
            '%s - %s: %s [%s]',
101
            $this->translator->trans('payment_order.mail.subject'),
102
            $paymentOrder->getDepartment()
103
                ->getName(),
104
            $paymentOrder->getProjectName(),
105
            $paymentOrder->getIDString()
106
        );
107
    }
108
}
109