Passed
Push — main ( baa5e8...aad503 )
by Thierry
05:54
created

ReportController::credit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 15
rs 10
c 0
b 0
f 0
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\PrinterService;
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 PrinterService $printerService
26
     */
27
    public function __construct(private SessionService $sessionService,
28
        private RoundService $roundService, private ReportService $reportService,
29
        private PrinterService $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));
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type null; however, parameter $session of Siak\Tontine\Service\Rep...ice::getSessionReport() does only seem to accept Siak\Tontine\Model\Session, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        view()->share($this->reportService->getSessionReport(/** @scrutinizer ignore-type */ $session));
Loading history...
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),
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type null; however, parameter $session of Siak\Tontine\Service\Rep...SessionReportFilename() does only seem to accept Siak\Tontine\Model\Session, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
            $this->printerService->getSessionReportFilename(/** @scrutinizer ignore-type */ $session),
Loading history...
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));
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type null; however, parameter $session of Siak\Tontine\Service\Rep...ice::getSavingsReport() does only seem to accept Siak\Tontine\Model\Session, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

98
        view()->share($this->reportService->getSavingsReport(/** @scrutinizer ignore-type */ $session));
Loading history...
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),
0 ignored issues
show
Bug introduced by
It seems like $session can also be of type null; however, parameter $session of Siak\Tontine\Service\Rep...SavingsReportFilename() does only seem to accept Siak\Tontine\Model\Session, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
            $this->printerService->getSavingsReportFilename(/** @scrutinizer ignore-type */ $session),
Loading history...
108
            trans('tontine.report.titles.savings'));
109
    }
110
111
    /**
112
     * @param Request $request
113
     * @param int $roundId
114
     *
115
     * @return View|Response
116
     */
117
    public function roundById(Request $request, int $roundId)
118
    {
119
        $round = $this->roundService->getRound($roundId);
120
        view()->share($this->reportService->getRoundReport($round));
0 ignored issues
show
Bug introduced by
It seems like $round can also be of type null; however, parameter $round of Siak\Tontine\Service\Rep...rvice::getRoundReport() does only seem to accept Siak\Tontine\Model\Round, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

120
        view()->share($this->reportService->getRoundReport(/** @scrutinizer ignore-type */ $round));
Loading history...
121
        // Show the html page
122
        if($request->has('html'))
123
        {
124
            return view($this->printerService->getRoundReportPath());
125
        }
126
127
        // Print the pdf
128
        return $this->pdfContent($this->printerService->getRoundReport(),
129
            $this->printerService->getRoundReportFilename($round),
0 ignored issues
show
Bug introduced by
It seems like $round can also be of type null; however, parameter $round of Siak\Tontine\Service\Rep...etRoundReportFilename() does only seem to accept Siak\Tontine\Model\Round, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
            $this->printerService->getRoundReportFilename(/** @scrutinizer ignore-type */ $round),
Loading history...
130
            trans('tontine.report.titles.round'));
131
    }
132
133
    /**
134
     * @param Request $request
135
     * @param SqidsInterface $sqids
136
     * @param string $roundSqid
137
     *
138
     * @return View|Response
139
     */
140
    public function round(Request $request, SqidsInterface $sqids, string $roundSqid)
141
    {
142
        [$roundId] = $sqids->decode($roundSqid);
143
144
        return $this->roundById($request, $roundId);
145
    }
146
147
    /**
148
     * @param Request $request
149
     * @param SqidsInterface $sqids
150
     * @param string $roundSqid
151
     *
152
     * @return View|Response
153
     */
154
    public function credit(Request $request, SqidsInterface $sqids, string $roundSqid)
155
    {
156
        [$roundId] = $sqids->decode($roundSqid);
157
        $round = $this->roundService->getRound($roundId);
158
        view()->share($this->reportService->getCreditReport($round));
0 ignored issues
show
Bug introduced by
It seems like $round can also be of type null; however, parameter $round of Siak\Tontine\Service\Rep...vice::getCreditReport() does only seem to accept Siak\Tontine\Model\Round, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
        view()->share($this->reportService->getCreditReport(/** @scrutinizer ignore-type */ $round));
Loading history...
159
        // Show the html page
160
        if($request->has('html'))
161
        {
162
            return view($this->printerService->getCreditReportPath());
163
        }
164
165
        // Print the pdf
166
        return $this->pdfContent($this->printerService->getCreditReport(),
167
            $this->printerService->getCreditReportFilename($round),
0 ignored issues
show
Bug introduced by
It seems like $round can also be of type null; however, parameter $round of Siak\Tontine\Service\Rep...tCreditReportFilename() does only seem to accept Siak\Tontine\Model\Round, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

167
            $this->printerService->getCreditReportFilename(/** @scrutinizer ignore-type */ $round),
Loading history...
168
            trans('tontine.report.titles.credit'));
169
    }
170
}
171