Passed
Push — master ( a040f6...1671cd )
by Jan
04:44
created

FileContoller   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 85
rs 10
c 3
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sepaExportXMLFile() 0 10 1
B checkPermission() 0 25 8
A paymentOrderReferences() 0 15 2
A paymentOrderForm() 0 15 2
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\Controller;
20
21
use App\Entity\PaymentOrder;
22
use App\Entity\SEPAExport;
23
use RuntimeException;
24
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpFoundation\Response;
27
use Symfony\Component\Routing\Annotation\Route;
28
use Vich\UploaderBundle\Handler\DownloadHandler;
29
30
/**
31
 * @Route("/file")
32
 */
33
class FileContoller extends AbstractController
34
{
35
    /**
36
     * @Route("/sepa_export/{id}/xml", name="file_sepa_export_xml")
37
     */
38
    public function sepaExportXMLFile(SEPAExport $SEPAExport, DownloadHandler $downloadHandler, Request $request): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request 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

38
    public function sepaExportXMLFile(SEPAExport $SEPAExport, DownloadHandler $downloadHandler, /** @scrutinizer ignore-unused */ Request $request): Response

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...
39
    {
40
        $this->denyAccessUnlessGranted('ROLE_SHOW_SEPA_EXPORTS', $SEPAExport);
41
42
        return $downloadHandler->downloadObject(
43
            $SEPAExport,
44
            'xml_file',
45
            null,
46
            $SEPAExport->getXmlFile()->getFilename(),
47
            true
48
        );
49
    }
50
51
    /**
52
     * @Route("/payment_order/{id}/form", name="file_payment_order_form")
53
     */
54
    public function paymentOrderForm(PaymentOrder $paymentOrder, DownloadHandler $downloadHandler, Request $request): Response
55
    {
56
        $this->checkPermission($paymentOrder, $request);
57
58
        if (null === $paymentOrder->getPrintedFormFile()) {
59
            throw new RuntimeException('The passed paymentOrder does not have an associated form file!');
60
        }
61
62
        return $downloadHandler->downloadObject(
63
            $paymentOrder,
64
            'printed_form_file',
65
            null,
66
            $paymentOrder->getPrintedFormFile()
67
                ->getFilename(),
68
            false
69
        );
70
    }
71
72
    /**
73
     * @Route("/payment_order/{id}/references", name="file_payment_order_references")
74
     */
75
    public function paymentOrderReferences(PaymentOrder $paymentOrder, DownloadHandler $downloadHandler, Request $request): Response
76
    {
77
        $this->checkPermission($paymentOrder, $request);
78
79
        if (null === $paymentOrder->getReferencesFile()) {
80
            throw new RuntimeException('The passed paymentOrder does not have an associated references file!');
81
        }
82
83
        return $downloadHandler->downloadObject(
84
            $paymentOrder,
85
            'references_file',
86
            null,
87
            $paymentOrder->getReferencesFile()
88
                ->getFilename(),
89
            false
90
        );
91
    }
92
93
    private function checkPermission(PaymentOrder $paymentOrder, Request $request): void
94
    {
95
        //Check if a valid confirmation token was given, then give access without proper role
96
        if ($request->query->has('token') && $request->query->has('confirm')) {
97
            //Check if we have one of the valid confirm numbers
98
            $confirm_step = $request->query->getInt('confirm');
99
            if (1 !== $confirm_step && 2 !== $confirm_step) {
100
                throw new RuntimeException('Invalid value for confirm! Expected 1 or 2');
101
            }
102
103
            //Check if given token is correct for this step
104
            $correct_token = 1 === $confirm_step ? $paymentOrder->getConfirm1Token() : $paymentOrder->getConfirm2Token();
105
            if (null === $correct_token) {
106
                throw new RuntimeException('This payment_order can not be confirmed! No token is set.');
107
            }
108
109
            $given_token = (string) $request->query->get('token');
110
            if (password_verify($given_token, $correct_token)) {
111
                //If password is correct, skip role checking.
112
                return;
113
            }
114
        }
115
116
        //If we dont return anywhere before, we has to check the user roles
117
        $this->denyAccessUnlessGranted('ROLE_SHOW_PAYMENT_ORDERS');
118
    }
119
}
120