Passed
Push — main ( cce870...daa135 )
by Thierry
06:24
created

FormController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A entry() 0 14 2
A form() 0 18 2
A session() 0 8 1
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\LocaleService;
9
use Siak\Tontine\Service\Meeting\SessionService;
10
use Siak\Tontine\Service\Report\Pdf\PrinterService;
11
use Siak\Tontine\Service\Report\ReportService;
12
use Siak\Tontine\Service\TenantService;
13
use Sqids\SqidsInterface;
14
15
use function base64_decode;
16
use function compact;
17
use function response;
18
use function trans;
19
use function view;
20
21
class FormController extends Controller
22
{
23
    /**
24
     * @param TenantService $tenantService
25
     * @param PrinterService $printerService
26
     * @param SessionService $sessionService
27
     */
28
    public function __construct(private TenantService $tenantService,
29
        private PrinterService $printerService, private SessionService $sessionService)
30
    {}
31
32
    /**
33
     * @param Request $request
34
     * @param string $form
35
     *
36
     * @return View|Response
37
     */
38
    private function form(Request $request, string $form)
39
    {
40
        // Show the html page
41
        if($request->has('html'))
42
        {
43
            return view($this->printerService->getFormViewPath($form));
44
        }
45
46
        // Print the pdf
47
        $filename = $this->printerService->getFormFilename($form);
48
        return response(base64_decode($this->printerService->getEntryForm($form)), 200)
49
            ->header('Content-Description', trans("meeting.entry.titles.$form"))
50
            ->header('Content-Type', 'application/pdf')
51
            ->header('Content-Disposition', "inline; filename=$filename")
52
            ->header('Content-Transfer-Encoding', 'binary')
53
            ->header('Expires', '0')
54
            ->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
55
            ->header('Pragma', 'public');
56
    }
57
58
    /**
59
     * @param Request $request
60
     * @param SqidsInterface $sqids
61
     * @param string $sessionSqid
62
     *
63
     * @return View|Response
64
     */
65
    public function session(Request $request, ReportService $reportService,
66
        SqidsInterface $sqids, string $sessionSqid)
67
    {
68
        [$sessionId] = $sqids->decode($sessionSqid);
69
        $session = $this->sessionService->getSession($sessionId);
70
        view()->share($reportService->getSessionEntry($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...vice::getSessionEntry() 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

70
        view()->share($reportService->getSessionEntry(/** @scrutinizer ignore-type */ $session));
Loading history...
71
72
        return $this->form($request, 'session');
73
    }
74
75
    /**
76
     * @param Request $request
77
     * @param LocaleService $localeService
78
     * @param SqidsInterface $sqids
79
     * @param string $form
80
     * @param string $sessionSqid
81
     *
82
     * @return View|Response
83
     */
84
    public function entry(Request $request, LocaleService $localeService,
85
        SqidsInterface $sqids, string $form, string $sessionSqid = '')
86
    {
87
        $tontine = $this->tenantService->tontine();
88
        [$country] = $localeService->getNameFromTontine($tontine);
0 ignored issues
show
Bug introduced by
It seems like $tontine can also be of type null; however, parameter $tontine of Siak\Tontine\Service\Loc...e::getNameFromTontine() does only seem to accept Siak\Tontine\Model\Tontine, 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

88
        [$country] = $localeService->getNameFromTontine(/** @scrutinizer ignore-type */ $tontine);
Loading history...
89
        $session = null;
90
        if($sessionSqid !== '')
91
        {
92
            [$sessionId] = $sqids->decode($sessionSqid);
93
            $session = $this->sessionService->getSession($sessionId);
94
        }
95
        view()->share(compact('tontine', 'country', 'session'));
96
97
        return $this->form($request, $form);
98
    }
99
}
100