BillableFlightQueryHandler::__invoke()   F
last analyzed

Complexity

Conditions 18
Paths 60

Size

Total Lines 102

Duplication

Lines 53
Ratio 51.96 %

Importance

Changes 0
Metric Value
cc 18
nc 60
nop 1
dl 53
loc 102
rs 3.8933
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
3
require_once(DOL_DOCUMENT_ROOT . '/flightlog/class/flight/Pilot.php');
4
require_once(DOL_DOCUMENT_ROOT . '/flightlog/class/flight/FlightTypeCount.php');
5
require_once(DOL_DOCUMENT_ROOT . '/flightlog/query/BillableFlightQuery.php');
6
7
/**
8
 * @author Laurent De Coninck <[email protected]>
9
 */
10
class BillableFlightQueryHandler
11
{
12
13
    /**
14
     * @var DoliDb $db
15
     */
16
    private $db;
17
18
    /**
19
     * @var stdClass
20
     */
21
    private $conf;
22
23
    /**
24
     * @param DoliDb   $db
25
     * @param stdClass $conf
26
     */
27
    public function __construct(DoliDb $db, stdClass $conf)
28
    {
29
        $this->db = $db;
30
        $this->conf = $conf;
31
    }
32
33
    /**
34
     * @param BillableFlightQuery $query
35
     *
36
     * @return array
37
     */
38
    public function __invoke(BillableFlightQuery $query)
39
    {
40
        $sql = "SELECT USR.lastname AS nom , USR.firstname AS prenom ,COUNT(`idBBC_vols`) AS nbr,fk_pilot as pilot, TT.numero as type,SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(heureA,heureD)))) AS time";
41
        $sql .= " FROM llx_bbc_vols, llx_user AS USR,llx_bbc_types AS TT ";
42
        $sql .= " WHERE `fk_pilot`= USR.rowid AND fk_type = TT.idType AND YEAR(llx_bbc_vols.date) = " . ($query->hasYear() ? "'" . $query->getFiscalYear() . "'" : 'YEAR(NOW())');
43
        $sql .= " GROUP BY fk_pilot,`fk_type`";
44
45
        $resql = $this->db->query($sql);
46
        $array = array();
47
        if ($resql) {
48
            $num = $this->db->num_rows($resql);
49
            $i = 0;
50
            if ($num) {
51
                while ($i < $num) {
52
                    $obj = $this->db->fetch_object($resql); //vol
53
                    if ($obj) {
54
                        if (!isset($array[$obj->pilot])) {
55
                            $name = $obj->prenom . ' ' . $obj->nom;
56
                            $pilot = Pilot::create($name, $obj->pilot);
57
                            $array[$obj->pilot] = $pilot;
58
                        }
59
60
                        $array[$obj->pilot] = $array[$obj->pilot]->addCount(
61
                            new FlightTypeCount(
62
                                $obj->type,
63
                                $obj->nbr,
64
                                $this->getFactorByType($obj->type)
65
                            )
66
                        );
67
                    }
68
                    $i++;
69
                }
70
            }
71
        }
72
73
        if (!$query->isIncludeTotal()) {
74
            return $array;
75
        }
76
77
        //total orga
78
        $sql = 'SELECT llx_user.lastname as name , llx_user.firstname,llx_user.rowid, count(idBBC_vols) as total FROM llx_bbc_vols LEFT JOIN llx_user ON rowid = fk_organisateur WHERE YEAR(date) = \'' . $query->getFiscalYear() . '\' AND fk_type IN (1,2) GROUP BY fk_organisateur';
79
        $resql = $this->db->query($sql);
80 View Code Duplication
        if ($resql) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
            $num = $this->db->num_rows($resql);
82
            $i = 0;
83
            if ($num) {
84
                while ($i < $num) {
85
                    $obj = $this->db->fetch_object($resql); //vol
86
87
                    if ($obj) {
88
89
                        if (!isset($array[$obj->rowid])) {
90
                            $name = $obj->firstname . ' ' . $obj->name;
91
                            $pilot = Pilot::create($name, $obj->rowid);
92
                            $array[$obj->rowid] = $pilot;
93
                        }
94
95
                        $array[$obj->rowid] = $array[$obj->rowid]->addCount(
96
                            new FlightTypeCount(
97
                                'orga',
98
                                $obj->total,
99
                                $this->getFactorByType('orga')
100
                            )
101
                        );
102
                    }
103
                    $i++;
104
                }
105
            }
106
        }
107
108
        //total orga T6 - instructeur
109
        $sql = 'SELECT llx_user.lastname as name , llx_user.firstname,llx_user.rowid, count(idBBC_vols) as total FROM llx_bbc_vols LEFT JOIN llx_user ON rowid = fk_organisateur WHERE YEAR(date) = \'' . $query->getFiscalYear() . '\' AND fk_type = 6 GROUP BY fk_organisateur';
110
        $resql = $this->db->query($sql);
111 View Code Duplication
        if ($resql) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
            $num = $this->db->num_rows($resql);
113
            $i = 0;
114
            if ($num) {
115
                while ($i < $num) {
116
                    $obj = $this->db->fetch_object($resql); //vol
117
118
                    if ($obj) {
119
                        if (!isset($array[$obj->rowid])) {
120
                            $name = $obj->firstname . ' ' . $obj->name;
121
                            $pilot = Pilot::create($name, $obj->rowid);
122
                            $array[$obj->rowid] = $pilot;
123
                        }
124
125
                        $array[$obj->rowid] = $array[$obj->rowid]->addCount(
126
                            new FlightTypeCount(
127
                                'orga_T6',
128
                                $obj->total,
129
                                $this->getFactorByType('orga_T6')
130
                            )
131
                        );
132
                    }
133
                    $i++;
134
                }
135
            }
136
        }
137
138
        return $array;
139
    }
140
141
    /**
142
     * Returns the number of points if set in the config, if not return the price of the service.
143
     *
144
     * @param string $type
145
     *
146
     * @return int
147
     */
148
    private function getFactorByType($type)
149
    {
150
        switch ($type) {
151
            case 'orga':
152
                return $this->conf->BBC_POINTS_BONUS_ORGANISATOR;
153
            case 'orga_T6':
154
                return $this->conf->BBC_POINTS_BONUS_INSTRUCTOR;
155
        }
156
157
        $constVariableName = 'BBC_POINTS_BONUS_' . $type;
158
        if (!isset($this->conf->$constVariableName) || empty($this->conf->$constVariableName) || $this->conf->$constVariableName < 0) {
159
            return $this->getFactorForService($type);
160
        }
161
162
        return (int) $this->conf->$constVariableName;
163
164
    }
165
166
    /**
167
     * @param string $type
168
     *
169
     * @return float
170
     */
171
    private function getFactorForService($type)
172
    {
173
        $service = new Bbctypes($this->db);
174
        $fetchResult = $service->fetch($type);
175
176
        if ($fetchResult <= 0) {
177
            throw new \InvalidArgumentException('Service not found');
178
        }
179
180
        return $service->getService()->price_ttc;
181
    }
182
183
184
}