Passed
Branch main (b90ec4)
by Thierry
20:23 queued 14:04
created

ReportController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 150
rs 10
c 1
b 0
f 0
wmc 12
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\Response;
7
use Illuminate\View\View;
8
use Siak\Tontine\Service\Planning\RoundService;
9
use Siak\Tontine\Service\Planning\SessionService;
10
use Siak\Tontine\Service\Report\Pdf\PdfPrinterService;
11
use Siak\Tontine\Service\Report\ReportService;
12
use Sqids\SqidsInterface;
13
14
use function base64_decode;
15
use function response;
16
use function trans;
17
use function view;
18
19
class ReportController extends Controller
20
{
21
    /**
22
     * @param SessionService $sessionService
23
     * @param RoundService $roundService
24
     * @param ReportService $reportService
25
     * @param PdfPrinterService $printerService
26
     */
27
    public function __construct(private SessionService $sessionService,
28
        private RoundService $roundService, private ReportService $reportService,
29
        private PdfPrinterService $printerService)
30
    {}
31
32
    /**
33
     * @param string $content
34
     * @param string $filename
35
     * @param string $title
36
     *
37
     * @return View|Response
38
     */
39
    private function pdfContent(string $content, string $filename, string $title)
40
    {
41
        return response(base64_decode($content), 200)
42
            ->header('Content-Description', $title)
43
            ->header('Content-Type', 'application/pdf')
44
            ->header('Content-Disposition', "inline; filename=$filename")
45
            ->header('Content-Transfer-Encoding', 'binary')
46
            ->header('Expires', '0')
47
            ->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
48
            ->header('Pragma', 'public');
49
    }
50
51
    /**
52
     * @param Request $request
53
     * @param int $sessionId
54
     *
55
     * @return View|Response
56
     */
57
    public function sessionById(Request $request, int $sessionId)
58
    {
59
        $session = $this->sessionService->getSession($sessionId);
60
        view()->share($this->reportService->getSessionReport($session));
61
        // Show the html page
62
        if($request->has('html'))
63
        {
64
            return view($this->printerService->getSessionReportPath());
65
        }
66
67
        // Print the pdf
68
        return $this->pdfContent($this->printerService->getSessionReport(),
69
            $this->printerService->getSessionReportFilename($session),
70
            trans('tontine.report.titles.session'));
71
    }
72
73
    /**
74
     * @param Request $request
75
     * @param SqidsInterface $sqids
76
     * @param string $sessionId
77
     *
78
     * @return View|Response
79
     */
80
    public function session(Request $request, SqidsInterface $sqids, string $sessionSqid)
81
    {
82
        [$sessionId] = $sqids->decode($sessionSqid);
83
84
        return $this->sessionById($request, $sessionId);
85
    }
86
87
    /**
88
     * @param Request $request
89
     * @param SqidsInterface $sqids
90
     * @param string $sessionId
91
     *
92
     * @return View|Response
93
     */
94
    public function savings(Request $request, SqidsInterface $sqids, string $sessionSqid)
95
    {
96
        [$sessionId] = $sqids->decode($sessionSqid);
97
        $session = $this->sessionService->getSession($sessionId);
98
        view()->share($this->reportService->getSavingsReport($session));
99
        // Show the html page
100
        if($request->has('html'))
101
        {
102
            return view($this->printerService->getSavingsReportPath());
103
        }
104
105
        // Print the pdf
106
        return $this->pdfContent($this->printerService->getSavingsReport(),
107
            $this->printerService->getSavingsReportFilename($session),
108
            trans('tontine.report.titles.savings'));
109
    }
110
111
    /**
112
     * @param Request $request
113
     * @param SqidsInterface $sqids
114
     * @param string $sessionSqid
115
     *
116
     * @return View|Response
117
     */
118
    public function credit(Request $request, SqidsInterface $sqids, string $sessionSqid)
119
    {
120
        [$sessionId] = $sqids->decode($sessionSqid);
121
        $session = $this->sessionService->getSession($sessionId);
122
        view()->share($this->reportService->getCreditReport($session));
123
        // Show the html page
124
        if($request->has('html'))
125
        {
126
            return view($this->printerService->getCreditReportPath());
127
        }
128
129
        // Print the pdf
130
        return $this->pdfContent($this->printerService->getCreditReport(),
131
            $this->printerService->getCreditReportFilename($session),
132
            trans('tontine.report.titles.credit'));
133
    }
134
135
    /**
136
     * @param Request $request
137
     * @param int $roundId
138
     *
139
     * @return View|Response
140
     */
141
    public function roundById(Request $request, int $roundId)
142
    {
143
        $round = $this->roundService->getRound($roundId);
144
        view()->share($this->reportService->getRoundReport($round));
145
        // Show the html page
146
        if($request->has('html'))
147
        {
148
            return view($this->printerService->getRoundReportPath());
149
        }
150
151
        // Print the pdf
152
        return $this->pdfContent($this->printerService->getRoundReport(),
153
            $this->printerService->getRoundReportFilename($round),
154
            trans('tontine.report.titles.round'));
155
    }
156
157
    /**
158
     * @param Request $request
159
     * @param SqidsInterface $sqids
160
     * @param string $roundSqid
161
     *
162
     * @return View|Response
163
     */
164
    public function round(Request $request, SqidsInterface $sqids, string $roundSqid)
165
    {
166
        [$roundId] = $sqids->decode($roundSqid);
167
168
        return $this->roundById($request, $roundId);
169
    }
170
}
171