Passed
Push — master ( 78fe79...12c952 )
by Florian
01:53
created

UserPaymentService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 4
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\PaymentRemainder;
15
use App\Entity\User;
16
use App\Enum\PaymentRemainderStatusType;
17
use App\Service\Interfaces\EmailServiceInterface;
18
use App\Service\Interfaces\PaymentServiceInterface;
19
use App\Service\Interfaces\UserPaymentServiceInterface;
20
use Doctrine\Persistence\ManagerRegistry;
21
use Symfony\Component\Routing\RouterInterface;
22
23
class UserPaymentService implements UserPaymentServiceInterface
24
{
25
    /**
26
     * @var PaymentServiceInterface
27
     */
28
    private $paymentService;
29
30
    /**
31
     * @var ManagerRegistry
32
     */
33
    private $doctrine;
34
35
    /**
36
     * @var EmailServiceInterface
37
     */
38
    private $emailService;
39
40
    /**
41
     * @var RouterInterface
42
     */
43
    private $router;
44
45
    /**
46
     * UserPaymentService constructor.
47
     *
48
     * @param EmailServiceInterface $emailService
49
     */
50
    public function __construct(PaymentServiceInterface $paymentService, ManagerRegistry $doctrine, Interfaces\EmailServiceInterface $emailService, \Symfony\Component\Routing\RouterInterface $router)
51
    {
52
        $this->paymentService = $paymentService;
53
        $this->doctrine = $doctrine;
54
        $this->emailService = $emailService;
55
        $this->router = $router;
56
    }
57
58
    /**
59
     * @throws \Payrexx\PayrexxException
60
     */
61
    public function closeInvoice(User $user)
62
    {
63
        $this->paymentService->closePayment($user->getPaymentInfo());
64
        $user->setPaymentRemainderStatus(PaymentRemainderStatusType::PAYMENT_ABORTED);
65
        $user->clearPaymentInfo();
66
67
        $this->save($user);
68
    }
69
70
    public function sendPaymentRemainder(User $user)
71
    {
72
        $paymentRemainder = $this->doctrine->getRepository(PaymentRemainder::class)->findActive();
73
74
        $body = $paymentRemainder->getBody();
75
        $url = $this->router->generate('login_code', ['code' => $user->getAuthenticationCode()]);
76
        $body = str_replace('(url)', $url, $body);
77
        $name = $user->getGivenName() . ' ' . $user->getFamilyName();
78
        $body = str_replace('(name)', $name, $body);
79
80
        $this->emailService->sendEmail($user->getEmail(), $paymentRemainder->getSubject(), $body);
81
82
        $user->setPaymentRemainder($paymentRemainder);
83
        $user->setPaymentRemainderStatus(PaymentRemainderStatusType::SENT);
84
        $this->save($user);
85
    }
86
87
    private function save(User $user)
88
    {
89
        $manager = $this->doctrine->getManager();
90
        $manager->persist($user);
91
        $manager->flush();
92
    }
93
}
94