ExcelExportInfections::getData()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 3
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace Hospitalplugin\utils;
4
5
use Hospitalplugin\Entities\InfectionsCRUD;
6
use Hospitalplugin\Entities\Infections;
7
8
class ExcelExportInfections {
9
	private static function getData() {
0 ignored issues
show
Coding Style introduced by
getData uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
10
		$wardId = (! empty ( $_POST ['wardId'] ) ? $_POST ['wardId'] : 0);
11
		$date = (! empty ( $_POST ['date'] ) ? $_POST ['date'] : (new \DateTime ())->format ( "Y-m" ));
12
		$from = new \DateTime ( $date . '-01' );
13
		$fromStr = $from->format ( 'Y-m-01' );
14
		$toStr = $from->format ( 'Y-m-t' );
15
		$infections = InfectionsCRUD::getInfections ( $fromStr, $toStr, $wardId, 'Infections' );
16
		return $infections;
17
	}
18
	private static function getColumns() {
19
		return Infections::getFields ();
20
	}
21
	private static function printHeaders($objPHPExcel, $cols) {
22
		$count = 0;
23
		foreach ( $cols as $col => $sym ) {
24
			$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $count ++, 2, sprintf ( "%7s", $col ) );
25
		}
26
	}
27
	/**
28
	 */
29
	private static function printData($objPHPExcel, $data, $cols) {
30
		$row = 3;
31
		foreach ( $data as $rowValue ) {
32
			$count = 0;
33
			foreach ( $cols as $col => $sym ) {
34
				$value = $rowValue->$sym;
35
				$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $count ++, $row, $value );
36
			}
37
			$row ++;
38
		}
39
		return $row;
40
	}
41
	private static function printTitle($objPHPExcel, $title) {
42
		$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 0, 1, $title );
43
	}
44
	/**
45
	 *
46
	 * @param PHPExcel $objPHPExcel        	
47
	 */
48
	private static function printFooter($objPHPExcel, $cols, $row) {
49
		$objPHPExcel->getActiveSheet ()->getStyle ( 'A' . $row . ':AA' . ($row + 1) )->getFont ()->setBold ( true );
50
		// SUM
51
		$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 1, $row, "SUMA" );
52
		for($i = 2; $i < count ( $cols ); $i ++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
53
			$colLetter = chr ( 65 + $i );
54
			$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $i, $row, "=sum(" . $colLetter . "3" . ":" . $colLetter . ($row - 1) . ")" );
55
		}
56
		ExcelExport::cellColor ( $objPHPExcel, 'A' . $row . ':AA' . ($row + 1), 'DDDDDD' );
57
	}
58
	static function fillData($objPHPExcel) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
		$data = ExcelExportInfections::getData ();
60
		$cols = ExcelExportInfections::getColumns ();
61
		
62
		ExcelExportInfections::printTitle ( $objPHPExcel, "Raport" );
63
		ExcelExportInfections::printHeaders ( $objPHPExcel, $cols );
64
		$lastRow = ExcelExportInfections::printData ( $objPHPExcel, $data, $cols );
65
		ExcelExportInfections::printFooter ( $objPHPExcel, $cols, $lastRow );
66
	}
67
}