Completed
Push — master ( 5594ea...e43a77 )
by Laurent
03:34 queued 01:39
created

GetPilotsWithMissionsQueryHandler::generateSql()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 */
5
6
namespace flightlog\query;
7
8
use DoliDB;
9
use QuarterPilotMissionCollection;
10
11
/**
12
 * Returns pilots that have a mission in the year and quarter.
13
 *
14
 * @author Laurent De Coninck <[email protected]>
15
 */
16
class GetPilotsWithMissionsQueryHandler
17
{
18
19
    /**
20
     * @var DoliDB
21
     */
22
    private $db;
23
24
    /**
25
     * @param DoliDB $db
26
     */
27
    public function __construct(DoliDB $db)
28
    {
29
        $this->db = $db;
30
    }
31
32
    /**
33
     * @param GetPilotsWithMissionsQuery $query
34
     *
35
     * @return QuarterPilotMissionCollection
36
     */
37
    public function __invoke(GetPilotsWithMissionsQuery $query)
38
    {
39
        $sql = $this->generateSql($query);
40
        $resql = $this->db->query($sql);
41
42
        $result = new QuarterPilotMissionCollection();
43
        if ($resql) {
44
            $num = $this->db->num_rows($resql);
45
            $i = 0;
46
            if ($num) {
47
                while ($i < $num) {
48
                    $obj = $this->db->fetch_object($resql); //vol
49
                    if ($obj) {
50
51
                        $pilotId = $obj->rowid;
52
                        $pilotLastname = $obj->lastname;
53
                        $pilotFirstname = $obj->firstname;
54
55
                        $result->addMission($obj->quartil, $pilotId, $pilotFirstname, $pilotLastname,
56
                            $obj->number_flights, $obj->total_kilometers);
57
58
                    }
59
                    $i++;
60
                }
61
            }
62
        }
63
64
        return $result;
65
    }
66
67
    /**
68
     * @param GetPilotsWithMissionsQuery $query
69
     *
70
     * @return string
71
     */
72
    private function generateSql(GetPilotsWithMissionsQuery $query)
73
    {
74
        $sql = "SELECT USR.rowid, USR.lastname, USR.firstname, QUARTER(VOL.date) as quartil ";
75
        $sql .= " , SUM(VOL.kilometers) as total_kilometers";
76
        $sql .= " , COUNT(VOL.idBBC_vols) as number_flights";
77
        $sql .= " FROM llx_bbc_vols as VOL";
78
        $sql .= " LEFT JOIN llx_user AS USR ON VOL.fk_pilot = USR.rowid";
79
        $sql .= " WHERE ";
80
        $sql .= " YEAR(VOL.date) = " . $query->getYear();
81
        $sql .= " AND ( VOL.fk_type = 1 OR VOL.fk_type = 2 ) ";
82
83
        if ($query->hasQuarter()) {
84
            $sql .= " AND QUARTER(VOL.date) = " . $query->getQuarter();
85
        }
86
87
        $sql .= " GROUP BY QUARTER(VOL.date), VOL.fk_pilot";
88
        $sql .= ' HAVING total_kilometers > 0 OR number_flights > 0 ';
89
        $sql .= " ORDER BY QUARTER(VOL.date), VOL.fk_pilot";
90
91
        return $this->db->escape($sql);
92
    }
93
}