Passed
Push — master ( 9b3357...4668c8 )
by Andrea
18:03
created

FiCoreController::getValoreCampoExcel()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 12
ccs 0
cts 10
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Fi\CoreBundle\Controller;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
8
class FiCoreController extends FiController
9
{
10
11 2
    public function stampatabellaAction(Request $request)
12
    {
13 2
        $this->setup($request);
14 2
        $pdf = new StampatabellaController($this->container);
15
16 2
        $parametri = $this->prepareOutput($request);
17
18 2
        $pdf->stampa($parametri);
19
20 2
        return new Response('OK');
21
    }
22
23 2
    public function esportaexcelAction(Request $request)
24
    {
25 2
        $this->setup($request);
26 2
        $xls = new StampatabellaController($this->container);
27
28 2
        $parametri = $this->prepareOutput($request);
29
30 2
        $fileexcel = $xls->esportaexcel($parametri);
31 2
        $response = new Response();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
32
33 2
        $response->headers->set('Content-Type', 'text/csv');
34 2
        $response->headers->set('Content-Disposition', 'attachment;filename="' . basename($fileexcel) . '"');
35
36 2
        $response->setContent(file_get_contents($fileexcel));
37
        
38 2
        @unlink($fileexcel);
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

38
        /** @scrutinizer ignore-unhandled */ @unlink($fileexcel);

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...
39
40 2
        return $response;
41
    }
42
43 4
    public function prepareOutput($request)
44
    {
45 4
        $namespace = $this->getNamespace();
46 4
        $bundle = $this->getBundle();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
47 4
        $container = $this->container;
48
49 4
        $nomebundle = $namespace . $bundle . 'Bundle';
50
51 4
        $em = $this->getDoctrine()->getManager();
52
53
        $paricevuti = array(
54 4
            'nomebundle' => $nomebundle,
55 4
            'nometabella' => $request->get('nometabella'),
56 4
            'container' => $container,
57 4
            'request' => $request,
58 4
        );
59
60 4
        $parametripertestatagriglia = $this->getParametersTestataPerGriglia($request, $container, $em, $paricevuti);
61
62 4
        $testatagriglia = Griglia::testataPerGriglia($parametripertestatagriglia);
63
64 4
        if ($request->get('titolo')) {
65
            $testatagriglia['titolo'] = $request->get('titolo');
66
        }
67 4
        $parametridatipergriglia = $this->getParametersDatiPerGriglia($request, $container, $em, $paricevuti);
68 4
        $corpogriglia = Griglia::datiPerGriglia($parametridatipergriglia);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
69
70 4
        $parametri = array('request' => $request, 'testata' => $testatagriglia, 'griglia' => $corpogriglia);
71 4
        return $parametri;
72
    }
73
74
    /*public function importaexcelAction(Request $request)
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
    {
76
        $this->setup($request);
77
        $return = "OK";
78
        try {
79
            $em = $this->getDoctrine()->getManager();
80
            $file = $request->files->get('file');
81
            $tablenamefile = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file->getClientOriginalName());
82
            $parametri = json_decode($request->get("parametrigriglia"));
83
            $bundle = $parametri->nomebundle;
84
            $controller = $parametri->nometabella;
85
            $repo = $em->getRepository($bundle . ":" . $tablenamefile);
86
            $className = $repo->getClassName();
87
88
            $classentitypath = "\\" . $className;
89
            if (strtolower($controller) != strtolower($tablenamefile)) {
90
                $response = new Response("Si sta cercando di caricare i dati di " . $tablenamefile . " in " . $controller);
91
                return $response;
92
            }
93
94
            if (is_a($repo, "Doctrine\ORM\EntityRepository")) {
95
                $entitycolumns = $em->getClassMetadata($bundle . ":" . $tablenamefile);
96
                $objPHPExcel = \PHPExcel_IOFactory::load($file);
97
                foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
98
                    $highestRow = $worksheet->getHighestRow();
99
                    $highestColumn = $worksheet->getHighestColumn();
100
                    $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
101
                    $colonne = array();
102
                    for ($index = 0; $index < $highestColumnIndex; $index++) {
103
                        $columnname = strtolower($worksheet->getCellByColumnAndRow($index, 1)->getValue());
104
                        if (!$entitycolumns->hasField($columnname) || $columnname == "id") {
105
                            //the entity does not have a such property $columnname or id column
106
                            continue;
107
                        } else {
108
                            $colonne[] = array("colxls" => $index, "name" => $columnname, "type" => $entitycolumns->getTypeOfField($columnname));
109
                        }
110
                    }
111
112
                    for ($rows = 2; $rows < $highestRow + 1; $rows++) {
113
                        $newentity = new $classentitypath();
114
                        foreach ($colonne as $colonna) {
115
                            $cols = $colonna["colxls"];
116
                            $valore = $worksheet->getCellByColumnAndRow($cols, $rows)->getValue();
117
                            if ($valore) {
118
                                $fieldset = "set" . ucfirst($colonna["name"]);
119
                                $valore = $this->getValoreCampoExcel($valore, $colonna);
120
                                $newentity->$fieldset($valore);
121
                            }
122
                        }
123
                        $em->persist($newentity);
124
                    }
125
                    $em->flush();
126
                }
127
            } else {
128
                $response = new Response($return);
129
                return $response;
130
            }
131
        } catch (\Exception $exc) {
132
            $response = new Response($exc->getMessage());
133
            return $response;
134
        }
135
136
        $response = new Response($return);
137
        return $response;
138
    }
139
140
    private function getValoreCampoExcel($valoreexcel, $colonna)
141
    {
142
        $valore = $valoreexcel;
143
        if ($colonna["type"] === "date") {
144
            $exceldata = \PHPExcel_Style_NumberFormat::toFormattedString($valoreexcel, 'YYYY-MM-DD');
145
            $nuovadata = \DateTime::createFromFormat('Y-m-d', $exceldata);
146
            $valore = $nuovadata;
147
        } elseif ($colonna["type"] === "boolean") {
148
            $valore = (($valoreexcel == 'SI') ? true : false);
149
        }
150
151
        return $valore;
152
    }*/
153
}
154