Passed
Push — main ( 85783c...df4f12 )
by Iain
07:13
created

InvoiceController::chargeAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2024 Iain Cambridge
7
 *
8
 *     This program is free software: you can redistribute it and/or modify
9
 *     it under the terms of the GNU General Public License as published by
10
 *     the Free Software Foundation, either version 3 of the License, or
11
 *     (at your option) any later version.
12
 *
13
 *     This program is distributed in the hope that it will be useful,
14
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *     GNU General Public License for more details.
17
 *
18
 *     You should have received a copy of the GNU General Public License
19
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\Billing\Controller;
23
24
use Parthenon\Billing\CustomerProviderInterface;
25
use Parthenon\Billing\Invoice\InvoiceChargerInterface;
26
use Parthenon\Billing\Invoice\InvoiceProviderInterface;
27
use Symfony\Component\HttpFoundation\BinaryFileResponse;
28
use Symfony\Component\HttpFoundation\JsonResponse;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpFoundation\Response;
31
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
32
use Symfony\Component\Routing\Attribute\Route;
33
use Symfony\Component\Serializer\SerializerInterface;
34
35
class InvoiceController
36
{
37
    #[Route('/billing/invoices', name: 'parthenon_billing_invoices_list')]
38
    public function listAction(
39
        CustomerProviderInterface $customerProvider,
40
        InvoiceProviderInterface $invoiceProvider,
41
        SerializerInterface $serializer,
42
    ): Response {
43
        $customer = $customerProvider->getCurrentCustomer();
44
        $invoices = $invoiceProvider->fetchInvoices($customer);
45
46
        $returnData = $serializer->serialize(['invoices' => $invoices], 'json');
47
48
        return JsonResponse::fromJsonString($returnData);
49
    }
50
51
    #[Route('/billing/invoices/{id}/download', name: 'parthenon_billing_invoices_download')]
52
    public function downloadAction(
53
        Request $request,
54
        InvoiceProviderInterface $invoiceProvider,
55
    ): Response {
56
        $download = $invoiceProvider->downloadInvoiceById($request->attributes->get('id'));
57
58
        $tmpFile = tempnam('/tmp', 'pdf');
59
        file_put_contents($tmpFile, $download);
60
61
        $response = new BinaryFileResponse($tmpFile);
62
        $filename = sprintf('invoice-%s.pdf', $request->get('id'));
63
64
        $response->headers->set('Content-Type', 'application/pdf');
65
        $response->setContentDisposition(
66
            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
67
            $filename
68
        );
69
70
        return $response;
71
    }
72
73
    #[Route('/billing/invoices/{id}/charge', name: 'parthenon_billing_invoices_charge')]
74
    public function chargeAction(
75
        Request $request,
76
        InvoiceChargerInterface $invoiceCharger,
77
    ): Response {
78
        $paid = $invoiceCharger->chargeInvoice($request->get('id'));
79
80
        return new JsonResponse(['paid' => $paid]);
81
    }
82
}
83