Passed
Push — master ( 571a8c...e3dde1 )
by Andrea
21:35 queued 12s
created

FiCoreTabellaControllerTrait::setTabellaxls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
            $entity,
35
            ['attr' => [
36 7
                        'id' => 'formdati' . $controller,
37
                    ],
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
                ]
41
        );
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
        );
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
            if (file_exists($filexls)) {
79
                $response = [
80 3
                    'status' => '200',
81 3
                    'file' => 'data:application/vnd.ms-excel;base64,' . base64_encode(file_get_contents($filexls)),
82
                ];
83
                try {
84 3
                    unlink($filexls);
85 3
                } catch (\Exception $e) {
86
                    //IGNORE THE ERROR OF UNLINK
87
                }
88
            } else {
89
                $response = [
90 3
                    'status' => '501',
91
                    'file' => 'Impossibile generare il file excel',
92
                ];
93
            }
94
        } catch (\Exception $exc) {
95
            $response = [
96
                'status' => '500',
97
                'file' => $exc->getFile() . ' -> Riga: ' . $exc->getLine() . ' -> ' . $exc->getMessage(),
98
            ];
99
        }
100
101 3
        return new JsonResponse($response);
102
    }
103
104
    /**
105
     *
106
     * @param array<string> $parametripassati
107
     * @return array<string>
108
     */
109 7
    protected function getParametriTabella(ManagerRegistry $doctrine, array $parametripassati)
110
    {
111 7
        $configurazionetabella = new Tabella($doctrine, $parametripassati);
112
        $parametritabella = [
113 7
            'parametritabella' => $configurazionetabella->getConfigurazionecolonnetabella(),
114 7
            'recordstabella' => $this->getRecordsTabella($this->isApi($parametripassati), $configurazionetabella),
115 7
            'paginacorrente' => $configurazionetabella->getPaginacorrente(),
116 7
            'paginetotali' => $configurazionetabella->getPaginetotali(),
117 7
            'righetotali' => $configurazionetabella->getRighetotali(),
118 7
            'traduzionefiltri' => $configurazionetabella->getTraduzionefiltri(),
119
        ];
120
121 7
        return $parametritabella;
122
    }
123
124
    /**
125
     * Append records to the given parameters of table.
126
     * It deals the difference between an API service or a ORM service.
127
     *
128
     * @param bool $isApi
129
     * @param mixed $configurazionetabella
130
     * @return mixed
131
     */
132 7
    private function getRecordsTabella(bool $isApi, &$configurazionetabella)
133
    {
134 7
        $results = [];
135 7
        if ($isApi) {
136
            $results = $configurazionetabella->getApiRecordstabella();
137
        } else {
138 7
            $results = $configurazionetabella->getRecordstabella();
139
        }
140 7
        return $results;
141
    }
142
143
    /**
144
     *
145
     * @param array<string> $parametripassati
146
     * @return array<string>
147
     */
148 3
    protected function getParametriTabellaXls(ManagerRegistry $doctrine, array $parametripassati) : array
149
    {
150 3
        $configurazionetabella = new Tabella($doctrine, $parametripassati);
151
        $parametritabella = [
152 3
            'parametritabella' => $configurazionetabella->getConfigurazionecolonnetabella(),
153 3
            'recordstabella' => $this->getRecordsTabella($this->isApi($parametripassati), $configurazionetabella),
154 3
            'paginacorrente' => $configurazionetabella->getPaginacorrente(),
155 3
            'paginetotali' => $configurazionetabella->getPaginetotali(),
156 3
            'righetotali' => $configurazionetabella->getRighetotali(),
157 3
            'traduzionefiltri' => $configurazionetabella->getTraduzionefiltri(),
158 3
            'nomecontroller' => ParametriTabella::getParameter($parametripassati['nomecontroller']),
159
        ];
160
161 3
        return $parametritabella;
162
    }
163
    
164
    /**
165
     *
166
     * @param string $controller
167
     * @return array<string>
168
     */
169 7
    protected function getTabellaTemplateInformations(string $controller) : array
170
    {
171 7
        $template = $controller . '/Tabella/tabellacontainer.html.twig';
172 7
        $path = $controller . '/';
173 7
        if (!$this->template->getLoader()->exists($template)) {
174 6
            $template = '@BiCore/' . $controller . '/Tabella/tabellacontainer.html.twig';
175 6
            $path = '@BiCore/' . $controller . '/';
176 6
            if (!$this->template->getLoader()->exists($template)) {
177 5
                $path = '@BiCore/Standard/';
178 5
                $template = $path . 'Tabella/tabellacontainer.html.twig';
179
            }
180
        }
181
182 7
        return ['path' => $path, 'template' => $template];
183
    }
184
}
185