Passed
Push — main ( 249507...a3fb5c )
by Thierry
06:22
created

ReportController::roundById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 20
rs 9.8333
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Facades\PdfGenerator;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Illuminate\View\View;
9
use Siak\Tontine\Service\Report\ReportService;
10
use Siak\Tontine\Service\TenantService;
11
use Sqids\SqidsInterface;
12
13
use function base64_decode;
14
use function view;
15
use function response;
16
17
class ReportController extends Controller
18
{
19
    /**
20
     * @var TenantService
21
     */
22
    protected TenantService $tenantService;
23
24
    /**
25
     * @var ReportService
26
     */
27
    protected ReportService $reportService;
28
29
    /**
30
     * @param TenantService $tenantService
31
     * @param ReportService $reportService
32
     */
33
    public function __construct(TenantService $tenantService, ReportService $reportService)
34
    {
35
        $this->tenantService = $tenantService;
36
        $this->reportService = $reportService;
37
    }
38
39
    /**
40
     * @param Request $request
41
     * @param int $sessionId
42
     *
43
     * @return View|Response
44
     */
45
    public function sessionById(Request $request, int $sessionId)
46
    {
47
        $session = $this->tenantService->getSession($sessionId);
48
        $html = view('tontine.report.session', $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

48
        $html = view('tontine.report.session', $this->reportService->getSessionReport(/** @scrutinizer ignore-type */ $session));
Loading history...
49
        // Show the html page
50
        if($request->has('html'))
51
        {
52
            return $html;
53
        }
54
55
        // Print the pdf
56
        $filename = $this->reportService->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

56
        $filename = $this->reportService->getSessionReportFilename(/** @scrutinizer ignore-type */ $session);
Loading history...
57
        return response(base64_decode(PdfGenerator::getPdf("$html")), 200)
58
            ->header('Content-Description', 'Session Report')
59
            ->header('Content-Type', 'application/pdf')
60
            ->header('Content-Disposition', "inline; filename=$filename")
61
            ->header('Content-Transfer-Encoding', 'binary')
62
            ->header('Expires', '0')
63
            ->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
64
            ->header('Pragma', 'public');
65
    }
66
67
    /**
68
     * @param Request $request
69
     * @param SqidsInterface $sqids
70
     * @param string $sessionId
71
     *
72
     * @return View|Response
73
     */
74
    public function session(Request $request, SqidsInterface $sqids, string $sessionSqid)
75
    {
76
        [$sessionId] = $sqids->decode($sessionSqid);
77
        return $this->sessionById($request, $sessionId);
78
    }
79
80
    /**
81
     * @param Request $request
82
     * @param int $roundId
83
     *
84
     * @return View|Response
85
     */
86
    public function roundById(Request $request, int $roundId)
87
    {
88
        $round = $this->tenantService->getRound($roundId);
89
        $html = view('tontine.report.round', $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

89
        $html = view('tontine.report.round', $this->reportService->getRoundReport(/** @scrutinizer ignore-type */ $round));
Loading history...
90
        // Show the html page
91
        if($request->has('html'))
92
        {
93
            return $html;
94
        }
95
96
        // Print the pdf
97
        $filename = $this->reportService->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

97
        $filename = $this->reportService->getRoundReportFilename(/** @scrutinizer ignore-type */ $round);
Loading history...
98
        return response(base64_decode(PdfGenerator::getPdf("$html")), 200)
99
            ->header('Content-Description', 'Round Report')
100
            ->header('Content-Type', 'application/pdf')
101
            ->header('Content-Disposition', "inline; filename=$filename")
102
            ->header('Content-Transfer-Encoding', 'binary')
103
            ->header('Expires', '0')
104
            ->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
105
            ->header('Pragma', 'public');
106
    }
107
108
    /**
109
     * @param Request $request
110
     * @param SqidsInterface $sqids
111
     * @param string $roundSqid
112
     *
113
     * @return View|Response
114
     */
115
    public function round(Request $request, SqidsInterface $sqids, string $roundSqid)
116
    {
117
        [$roundId] = $sqids->decode($roundSqid);
118
        return $this->roundById($request, $roundId);
119
    }
120
}
121