Completed
Push — master ( b1beea...de00f9 )
by Andrea
55:37 queued 13s
created

getParametriTabellaXls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
ccs 11
cts 11
cp 1
crap 1
rs 9.9
1
<?php
2
3
namespace Cdf\BiCoreBundle\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
7
use Cdf\BiCoreBundle\Utils\Tabella\ParametriTabella;
8
use Cdf\BiCoreBundle\Utils\Tabella\Tabella;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Cdf\BiCoreBundle\Utils\Export\TabellaXls;
11
12
trait FiCoreTabellaControllerTrait
13
{
14
15
    private $tabellaxls;
16
17 8
    public function tabella(Request $request)
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'), array('user' => $this->getUser()));
0 ignored issues
show
Bug introduced by
It seems like $request->get('parametri') can also be of type null; however, parameter $array1 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'), array('user' => $this->getUser()));
Loading history...
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'), array('user' => $this->/** @scrutinizer ignore-call */ getUser()));
Loading history...
24 7
        $parametriform = isset($parametripassati['parametriform']) ?
25 7
                json_decode(ParametriTabella::getParameter($parametripassati['parametriform']), true) : array();
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
            array('attr' => array(
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($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
            array('parametri' => $parametri)
51
        );
52
    }
53 3
    public function setTabellaxls(TabellaXls $tabellaxls)
54
    {
55 3
        $this->tabellaxls = $tabellaxls;
56 3
    }
57 3
    public function exportXls(Request $request)
58
    {
59
        try {
60 3
            $parametripassati = array_merge($request->get('parametri'), array('user' => $this->getUser()));
0 ignored issues
show
Bug introduced by
It seems like $request->get('parametri') can also be of type null; however, parameter $array1 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

60
            $parametripassati = array_merge(/** @scrutinizer ignore-type */ $request->get('parametri'), array('user' => $this->getUser()));
Loading history...
61 3
            $parametripassati['estraituttirecords'] = ParametriTabella::setParameter('1');
62
63 3
            $filexls = $this->tabellaxls->esportaexcel($this->getParametriTabellaXls($parametripassati));
64 3
            if (file_exists($filexls)) {
65
                $response = array(
66 3
                    'status' => '200',
67 3
                    'file' => 'data:application/vnd.ms-excel;base64,' . base64_encode(file_get_contents($filexls)),
68
                );
69 3
                @unlink($filexls);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

69
                /** @scrutinizer ignore-unhandled */ @unlink($filexls);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
70
            } else {
71
                $response = array(
72 3
                    'status' => '501',
73
                    'file' => 'Impossibile generare il file excel',
74
                );
75
            }
76
        } catch (\Exception $exc) {
77
            $response = array(
78
                'status' => '500',
79
                'file' => $exc->getFile() . ' -> Riga: ' . $exc->getLine() . ' -> ' . $exc->getMessage(),
80
            );
81
        }
82
83 3
        return new JsonResponse($response);
84
    }
85 7
    protected function getParametriTabella(array $parametripassati)
86
    {
87 7
        $doctrine = $this->get('doctrine');
0 ignored issues
show
Bug introduced by
It seems like get() 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

87
        /** @scrutinizer ignore-call */ 
88
        $doctrine = $this->get('doctrine');
Loading history...
88 7
        $configurazionetabella = new Tabella($doctrine, $parametripassati);
89
        $parametritabella = array(
90 7
            'parametritabella' => $configurazionetabella->getConfigurazionecolonnetabella(),
91 7
            'recordstabella' => $configurazionetabella->getRecordstabella(),
92 7
            'paginacorrente' => $configurazionetabella->getPaginacorrente(),
93 7
            'paginetotali' => $configurazionetabella->getPaginetotali(),
94 7
            'righetotali' => $configurazionetabella->getRighetotali(),
95 7
            'traduzionefiltri' => $configurazionetabella->getTraduzionefiltri()
96
        );
97 7
        return $parametritabella;
98
    }
99 3
    protected function getParametriTabellaXls(array $parametripassati)
100
    {
101 3
        $doctrine = $this->get('doctrine');
102 3
        $configurazionetabella = new Tabella($doctrine, $parametripassati);
103
        $parametritabella = array(
104 3
            'parametritabella' => $configurazionetabella->getConfigurazionecolonnetabella(),
105 3
            'recordstabella' => $configurazionetabella->getRecordstabella(),
106 3
            'paginacorrente' => $configurazionetabella->getPaginacorrente(),
107 3
            'paginetotali' => $configurazionetabella->getPaginetotali(),
108 3
            'righetotali' => $configurazionetabella->getRighetotali(),
109 3
            'traduzionefiltri' => $configurazionetabella->getTraduzionefiltri(),
110 3
            'nomecontroller' => ParametriTabella::getParameter($parametripassati['nomecontroller']),
111
        );
112 3
        return $parametritabella;
113
    }
114 7
    protected function getTabellaTemplateInformations($controller)
115
    {
116 7
        $template = $controller . '/Tabella/tabellacontainer.html.twig';
117 7
        $path = $controller . '/';
118 7
        if (!$this->get('templating')->exists($template)) {
119 6
            $template = 'BiCoreBundle:' . $controller . ':Tabella/tabellacontainer.html.twig';
120 6
            $path = 'BiCoreBundle:' . $controller . ':';
121 6
            if (!$this->get('templating')->exists($template)) {
122 5
                $path = 'BiCoreBundle:Standard:';
123 5
                $template = $path . 'Tabella/tabellacontainer.html.twig';
124
            }
125
        }
126 7
        return array("path" => $path, "template" => $template);
127
    }
128
}
129