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

PaymentRemainderController::editAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 3
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\Administration;
13
14
use App\Controller\Administration\Base\BaseController;
15
use App\Entity\PaymentRemainder;
16
use App\Model\Breadcrumb;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Routing\Annotation\Route;
20
use Symfony\Contracts\Translation\TranslatorInterface;
21
22
/**
23
 * @Route("/payment_remainder")
24
 */
25
class PaymentRemainderController extends BaseController
26
{
27
    /**
28
     * @Route("/new", name="administration_payment_remainder_new")
29
     *
30
     * @throws \Exception
31
     *
32
     * @return Response
33
     */
34
    public function newAction(Request $request, TranslatorInterface $translator)
35
    {
36
        $paymentRemainder = new PaymentRemainder();
37
        $paymentRemainder->setName($translator->trans('default.name', [], 'entity_payment_remainder'));
38
        $paymentRemainder->setSubject($translator->trans('default.subject', [], 'entity_payment_remainder'));
39
        $paymentRemainder->setBody($translator->trans('default.body', ['support_email' => $this->getParameter('REPLY_EMAIL')], 'entity_payment_remainder'));
40
41
        $paymentRemainder->setFee(0);
42
        $paymentRemainder->setDueAt((new \DateTime())->add(new \DateInterval('P1M')));
43
44
        //process form
45
        $saved = false;
46
        $myForm = $this->handleCreateForm(
47
            $request,
48
            $paymentRemainder,
49
            function () use ($paymentRemainder, $translator, &$saved) {
50
                if (!$this->ensureValidPaymentRemainder($paymentRemainder, $translator)) {
51
                    return false;
52
                }
53
54
                $saved = true;
55
56
                return true;
57
            }
58
        );
59
        if ($myForm instanceof Response) {
60
            return $myForm;
61
        }
62
63
        if ($saved) {
64
            return $this->redirectToRoute('administration');
65
        }
66
67
        return $this->render('administration/payment_remainder/new.html.twig', ['form' => $myForm->createView()]);
68
    }
69
70
    /**
71
     * @Route("/{paymentRemainder}/edit", name="administration_payment_remainder_edit")
72
     *
73
     * @return Response
74
     */
75
    public function editAction(Request $request, PaymentRemainder $paymentRemainder, TranslatorInterface $translator)
76
    {
77
        //process form
78
        $myForm = $this->handleUpdateForm($request, $paymentRemainder, function () use ($paymentRemainder, $translator) {
79
            return $this->ensureValidPaymentRemainder($paymentRemainder, $translator);
80
        });
81
82
        if ($myForm instanceof Response) {
83
            return $myForm;
84
        }
85
86
        return $this->render('administration/payment_remainder/edit.html.twig', ['form' => $myForm->createView()]);
87
    }
88
89
    /**
90
     * @return bool
91
     */
92
    private function ensureValidPaymentRemainder(PaymentRemainder $paymentRemainder, TranslatorInterface $translator)
93
    {
94
        if (mb_strrpos($paymentRemainder->getBody(), '(url)') === false) {
95
            $error = $translator->trans('new.error.missing_url', [], 'administration_payment_remainder');
96
            $this->displayError($error);
97
98
            return false;
99
        }
100
101
        return true;
102
    }
103
104
    /**
105
     * get the breadcrumbs leading to this controller.
106
     *
107
     * @return Breadcrumb[]
108
     */
109
    protected function getIndexBreadcrumbs()
110
    {
111
        return array_merge(parent::getIndexBreadcrumbs(), [
112
            new Breadcrumb(
113
                $this->generateUrl('administration'),
114
                $this->getTranslator()->trans('index.title', [], 'administration')
115
            ),
116
        ]);
117
    }
118
}
119