Completed
Push — feature/multi_order ( eb7707 )
by Laurent
01:37
created

GetPilotsWithMissionsQueryHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 33 6
A generateSql() 0 26 3
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
                        if($query->isPilotsOnly()){
56
                            $result->addPilot($pilotId, $pilotFirstname, $pilotLastname);
57
                        }else{
58
                            $result->addMission($obj->quartil, $pilotId, $pilotFirstname, $pilotLastname,
59
                                $obj->number_flights, $obj->total_kilometers);
60
                        }
61
62
                    }
63
                    $i++;
64
                }
65
            }
66
        }
67
68
        return $result;
69
    }
70
71
    /**
72
     * @param GetPilotsWithMissionsQuery $query
73
     *
74
     * @return string
75
     */
76
    private function generateSql(GetPilotsWithMissionsQuery $query)
77
    {
78
        $sql = "SELECT USR.rowid, USR.lastname, USR.firstname ";
79
        $sql .= " , SUM(VOL.kilometers) as total_kilometers ";
80
        $sql .= " , COUNT(VOL.idBBC_vols) as number_flights";
81
82
        if(!$query->isPilotsOnly()){
83
            $sql .= " , QUARTER(VOL.date) as quartil ";
84
        }
85
86
        $sql .= " FROM llx_bbc_vols as VOL";
87
        $sql .= " LEFT JOIN llx_user AS USR ON VOL.fk_pilot = USR.rowid";
88
        $sql .= " WHERE ";
89
        $sql .= " YEAR(VOL.date) = " . $query->getYear();
90
        $sql .= " AND ( VOL.fk_type = 1 OR VOL.fk_type = 2 ) ";
91
92
        if ($query->hasQuarter()) {
93
            $sql .= " AND QUARTER(VOL.date) = " . $query->getQuarter();
94
        }
95
96
        $sql .= " GROUP BY QUARTER(VOL.date), VOL.fk_pilot";
97
        $sql .= ' HAVING total_kilometers > 0 OR number_flights > 0 ';
98
        $sql .= " ORDER BY QUARTER(VOL.date), VOL.fk_pilot";
99
100
        return $this->db->escape($sql);
101
    }
102
}