Completed
Push — master ( df6d12...eab9e2 )
by Patrick
01:41 queued 10s
created

SimplePDF::createPDFBody()   B

Complexity

Conditions 8
Paths 36

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 36
nop 0
dl 0
loc 52
rs 7.8028
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Schedules;
3
4
class SimplePDF extends \PDF\PDF
5
{
6
    use ShiftSchedule;
7
8
    protected $department;
9
    protected $shifts;
10
11
    public function __construct($department, $shifts)
12
    {
13
        parent::__construct();
14
        $this->department = $department;
15
        $this->shifts = $shifts;
16
        $this->createPDFBody();
17
    }
18
19
    protected function createPDFBody()
20
    {
21
        $html = '<body>';
22
        $html .= '<style type="text/css">table {border-collapse: collapse;} table, th, td {border: 1px solid black;}</style>';
23
        $html .= '<h1 style="text-align: center;">'.$this->department['departmentName'].' Shift Schedule</h1>';
24
        //Group shifts by day...
25
        $days = array();
26
        $shifts = $this->shifts;
27
        $count = count($shifts);
28
        for($i = 0; $i < $count; $i++)
29
        {
30
            $start = new \DateTime($shifts[$i]['startTime']);
31
            $end = new \DateTime($shifts[$i]['endTime']);
32
            $shifts[$i]['startTime'] = $start;
33
            $shifts[$i]['endTime'] = $end;
34
            $dateStr = $start->format('l (n/j/Y)');
35
            $timeStr = $start->format('g:i A').' till '.$end->format('g:i A');
36
            if(strlen($shifts[$i]['name']) > 0)
37
            {
38
                $timeStr .=' - <i>'.$shifts[$i]['name'].'</i>';
39
            }
40
            if(!isset($days[$dateStr]))
41
            {
42
                $days[$dateStr] = array();
43
            }
44
            if(!isset($days[$dateStr][$timeStr]))
45
            {
46
                $days[$dateStr][$timeStr] = array();
47
            }
48
            array_push($days[$dateStr][$timeStr], $shifts[$i]);
49
        }
50
        ksort($days);
51
        foreach($days as $dateStr=>$day)
52
        {
53
            $html .='<h2>'.$dateStr.'</h2>';
54
            uksort($day, array($this, 'groupSort'));
55
            foreach($day as $shiftStr=>$shifts)
56
            {
57
                usort($shifts, array($this, 'shiftSort'));
58
                $html .='<h3>'.$shiftStr.'</h3>';
59
                $html .='<table width="100%"><tr><th style="width: 20%">Role</th><th>Volunteer Name</th><th>Volunteer Camp</th></tr>';
60
                foreach($shifts as $shift)
61
                {
62
                    //TODO Volunteer info for shift...
63
                    $html .='<tr><td>'.$this->getRoleNameFromID($shift['roleID']).'</td><td></td><td></td></tr>';
64
                }
65
                $html .='</table>';
66
            }
67
        }
68
        $html .='</body>';
69
        $this->setPDFFromHTML($html);
70
    }
71
} 
72