FiCoreTabellaControllerTrait::exportXls()   A
last analyzed

Complexity

Conditions 5
Paths 11

Size

Total Lines 39
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5.9256

Importance

Changes 0
Metric Value
cc 5
eloc 26
nc 11
nop 3
dl 0
loc 39
ccs 18
cts 27
cp 0.6667
crap 5.9256
rs 9.1928
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Controller;
4
5
use Cdf\BiCoreBundle\Utils\Export\TabellaXls;
6
use Cdf\BiCoreBundle\Utils\Tabella\ParametriTabella;
7
use Cdf\BiCoreBundle\Utils\Tabella\Tabella;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
12
use Doctrine\Persistence\ManagerRegistry;
13
14
trait FiCoreTabellaControllerTrait
15
{
16
17 8
    public function tabella(Request $request, ManagerRegistry $doctrine): Response
18
    {
19 8
        if (!$this->permessi->canRead($this->getController())) {
0 ignored issues
show
Bug introduced by
It seems like getController() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

19
        if (!$this->permessi->canRead($this->/** @scrutinizer ignore-call */ getController())) {
Loading history...
20 1
            throw new AccessDeniedException('Non si hanno i permessi per visualizzare questo contenuto');
21
        }
22
23 7
        $parametripassati = array_merge($request->get('parametri'), ['user' => $this->getUser()]);
0 ignored issues
show
Bug introduced by
It seems like getUser() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

23
        $parametripassati = array_merge($request->get('parametri'), ['user' => $this->/** @scrutinizer ignore-call */ getUser()]);
Loading history...
Bug introduced by
It seems like $request->get('parametri') can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, 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

23
        $parametripassati = array_merge(/** @scrutinizer ignore-type */ $request->get('parametri'), ['user' => $this->getUser()]);
Loading history...
24 7
        $parametriform = isset($parametripassati['parametriform']) ?
25 7
                json_decode(ParametriTabella::getParameter($parametripassati['parametriform']), true) : [];
26 7
        $classbundle = ParametriTabella::getParameter($parametripassati['entityclass']);
27 7
        $formbundle = ParametriTabella::getParameter($parametripassati['formclass']);
28 7
        $formType = $formbundle . 'Type';
29
30 7
        $entity = new $classbundle();
31 7
        $controller = ParametriTabella::getParameter($parametripassati['nomecontroller']);
32 7
        $form = $this->createForm(
0 ignored issues
show
Bug introduced by
It seems like createForm() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

32
        /** @scrutinizer ignore-call */ 
33
        $form = $this->createForm(
Loading history...
33 7
            $formType,
34 7
            $entity,
35 7
            ['attr' => [
36 7
                        'id' => 'formdati' . $controller,
37 7
                    ],
38 7
                    'action' => $this->generateUrl($controller . '_new'),
0 ignored issues
show
Bug introduced by
It seems like generateUrl() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
                    'action' => $this->/** @scrutinizer ignore-call */ generateUrl($controller . '_new'),
Loading history...
39 7
                    'parametriform' => $parametriform,
40 7
                ]
41 7
        );
42
43 7
        $parametri = array_merge($parametripassati, $this->getParametriTabella($doctrine, $parametripassati));
44 7
        $parametri['form'] = $form->createView();
45 7
        $templateobj = $this->getTabellaTemplateInformations($controller);
46 7
        $parametri['templatelocation'] = $templateobj['path'];
47
48 7
        return $this->render(
0 ignored issues
show
Bug introduced by
It seems like render() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

48
        return $this->/** @scrutinizer ignore-call */ render(
Loading history...
49 7
            $templateobj['template'],
50 7
            ['parametri' => $parametri]
51 7
        );
52
    }
53
54
    /**
55
     *
56
     * @codeCoverageIgnore
57
     * It returns true if the request belongs to an API service, false otherwise
58
     *
59
     * @param array<string> $parametripassati
60
     * @return bool
61
     */
62
    private function isApi(&$parametripassati): bool
63
    {
64
        $isapi = false;
65
        if (isset($parametripassati['isapi'])) {
66
            $isapi = true;
67
        }
68
        return $isapi;
69
    }
70
71 3
    public function exportXls(Request $request, TabellaXls $tabellaxls, ManagerRegistry $doctrine): JsonResponse
72
    {
73
        try {
74 3
            $parametripassati = array_merge($request->get('parametri'), ['user' => $this->getUser()]);
0 ignored issues
show
Bug introduced by
It seems like $request->get('parametri') can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, 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

74
            $parametripassati = array_merge(/** @scrutinizer ignore-type */ $request->get('parametri'), ['user' => $this->getUser()]);
Loading history...
75 3
            $parametripassati['estraituttirecords'] = ParametriTabella::setParameter('1');
76
77 3
            $filexls = $tabellaxls->esportaexcel($this->getParametriTabellaXls($doctrine, $parametripassati));
78 3
            $filexlsbuffer = file_get_contents($filexls);
79 3
            if ($filexlsbuffer === false) {
80
                $response = [
81
                    'status' => '500',
82
                    'file' => 'Impossibile leggere il file excel generato',
83
                ];
84
            } else {
85 3
                if (file_exists($filexls)) {
86 3
                    $response = [
87 3
                        'status' => '200',
88 3
                        'file' => 'data:application/vnd.ms-excel;base64,' . base64_encode($filexlsbuffer),
89 3
                    ];
90
                    try {
91 3
                        unlink($filexls);
92 3
                    } catch (\Exception $e) {
93
                        //IGNORE THE ERROR OF UNLINK
94
                    }
95
                } else {
96 3
                    $response = [
97 3
                        'status' => '501',
98 3
                        'file' => 'Impossibile generare il file excel',
99 3
                    ];
100
                }
101
            }
102
        } catch (\Exception $exc) {
103
            $response = [
104
                'status' => '500',
105
                'file' => $exc->getFile() . ' -> Riga: ' . $exc->getLine() . ' -> ' . $exc->getMessage(),
106
            ];
107
        }
108
109 3
        return new JsonResponse($response);
110
    }
111
112
    /**
113
     *
114
     * @param array<mixed> $parametripassati
115
     * @return array<mixed>
116
     */
117 7
    protected function getParametriTabella(ManagerRegistry $doctrine, array $parametripassati)
118
    {
119 7
        $configurazionetabella = new Tabella($doctrine, $parametripassati);
120 7
        $parametritabella = [
121 7
            'parametritabella' => $configurazionetabella->getConfigurazionecolonnetabella(),
122 7
            'recordstabella' => $this->getRecordsTabella($this->isApi($parametripassati), $configurazionetabella),
123 7
            'paginacorrente' => $configurazionetabella->getPaginacorrente(),
124 7
            'paginetotali' => $configurazionetabella->getPaginetotali(),
125 7
            'righetotali' => $configurazionetabella->getRighetotali(),
126 7
            'traduzionefiltri' => $configurazionetabella->getTraduzionefiltri(),
127 7
        ];
128
129 7
        return $parametritabella;
130
    }
131
132
    /**
133
     * Append records to the given parameters of table.
134
     * It deals the difference between an API service or a ORM service.
135
     *
136
     * @param bool $isApi
137
     * @param mixed $configurazionetabella
138
     * @return mixed
139
     */
140 7
    private function getRecordsTabella(bool $isApi, &$configurazionetabella)
141
    {
142 7
        $results = [];
143 7
        if ($isApi) {
144
            $results = $configurazionetabella->getApiRecordstabella();
145
        } else {
146 7
            $results = $configurazionetabella->getRecordstabella();
147
        }
148 7
        return $results;
149
    }
150
151
    /**
152
     *
153
     * @param array<mixed> $parametripassati
154
     * @return array<mixed>
155
     */
156 3
    protected function getParametriTabellaXls(ManagerRegistry $doctrine, array $parametripassati): array
157
    {
158 3
        $configurazionetabella = new Tabella($doctrine, $parametripassati);
159 3
        $parametritabella = [
160 3
            'parametritabella' => $configurazionetabella->getConfigurazionecolonnetabella(),
161 3
            'recordstabella' => $this->getRecordsTabella($this->isApi($parametripassati), $configurazionetabella),
162 3
            'paginacorrente' => $configurazionetabella->getPaginacorrente(),
163 3
            'paginetotali' => $configurazionetabella->getPaginetotali(),
164 3
            'righetotali' => $configurazionetabella->getRighetotali(),
165 3
            'traduzionefiltri' => $configurazionetabella->getTraduzionefiltri(),
166 3
            'nomecontroller' => ParametriTabella::getParameter($parametripassati['nomecontroller']),
167 3
        ];
168
169 3
        return $parametritabella;
170
    }
171
172
    /**
173
     *
174
     * @param string $controller
175
     * @return array<string>
176
     */
177 7
    protected function getTabellaTemplateInformations(string $controller): array
178
    {
179 7
        $template = $controller . '/Tabella/tabellacontainer.html.twig';
180 7
        $path = $controller . '/';
181 7
        if (!$this->template->getLoader()->exists($template)) {
182 6
            $template = '@BiCore/' . $controller . '/Tabella/tabellacontainer.html.twig';
183 6
            $path = '@BiCore/' . $controller . '/';
184 6
            if (!$this->template->getLoader()->exists($template)) {
185 5
                $path = '@BiCore/Standard/';
186 5
                $template = $path . 'Tabella/tabellacontainer.html.twig';
187
            }
188
        }
189
190 7
        return ['path' => $path, 'template' => $template];
191
    }
192
}
193