Completed
Pull Request — master (#5)
by Laurent
02:35 queued 42s
created

MonthlyBillableQueryHandler::__invoke()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 53
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 42
nc 3
nop 1
dl 0
loc 53
rs 8.7155
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
require_once __DIR__ . '/../class/billing/monthly/MonthBillCollection.php';
3
require_once __DIR__ . '/../class/billing/monthly/MonthlyFlightBill.php';
4
require_once __DIR__ . '/../class/billing/monthly/MoneyReceiver.php';
5
require_once __DIR__ . '/../class/bbcvols.class.php';
6
require_once __DIR__ . '/MonthlyBillableQuery.php';
7
8
/**
9
 * @author Laurent De Coninck <[email protected]>
10
 */
11
class MonthlyBillableQueryHandler
12
{
13
14
    /**
15
     * @var DoliDB
16
     */
17
    private $db;
18
19
    /**
20
     * @var stdClass
21
     */
22
    private $config;
23
24
    /**
25
     * @param DoliDB   $db
26
     * @param stdClass $config
27
     */
28
    public function __construct($db, $config)
29
    {
30
        $this->db = $db;
31
        $this->config = $config;
32
    }
33
34
    /**
35
     * @param MonthlyBillableQuery $query
36
     *
37
     * @return MonthBillCollection
38
     */
39
    public function __invoke(MonthlyBillableQuery $query)
40
    {
41
        $sql = 'SELECT flights.*, usr.rowid as usrRowId, usr.lastname as lastname,  usr.firstname as firstname FROM llx_bbc_vols as flights INNER JOIN llx_user as usr ON flights.fk_receiver = usr.rowid';
42
        $sql .= ' WHERE ';
43
        $sql .= ' flights.fk_type = 2 ';
44
        $sql .= ' AND flights.is_facture = 0 ';
45
        $sql .= sprintf(' AND YEAR(flights.date) = %s ', $query->getYear());
46
        $sql .= sprintf(' AND MONTH(flights.date) = %s', $query->getMonth());
47
48
        $resql = $this->db->query($sql);
49
        $array = new MonthBillCollection();
50
51
        if ($resql) {
52
            $num = $this->db->num_rows($resql);
53
            $i = 0;
54
            if ($num) {
55
                while ($i < $num) {
56
                    $obj = $this->db->fetch_object($resql);
57
                    if ($obj) {
58
59
                        $flight = new Bbcvols($this->db);
60
                        $flight->id = $obj->idBBC_vols;
61
                        $flight->idBBC_vols = $obj->idBBC_vols;
62
                        $flight->date = $this->db->jdate($obj->date);
63
                        $flight->lieuD = $obj->lieuD;
64
                        $flight->lieuA = $obj->lieuA;
65
                        $flight->heureD = $obj->heureD;
66
                        $flight->heureA = $obj->heureA;
67
                        $flight->BBC_ballons_idBBC_ballons = $obj->BBC_ballons_idBBC_ballons;
68
                        $flight->nbrPax = $obj->nbrPax;
69
                        $flight->remarque = $obj->remarque;
70
                        $flight->incidents = $obj->incidents;
71
                        $flight->fk_type = $obj->fk_type;
72
                        $flight->fk_pilot = $obj->fk_pilot;
73
                        $flight->fk_organisateur = $obj->fk_organisateur;
74
                        $flight->is_facture = $obj->is_facture;
75
                        $flight->kilometers = $obj->kilometers;
76
                        $flight->cost = $obj->cost;
77
                        $flight->fk_receiver = $obj->fk_receiver;
78
                        $flight->justif_kilometers = $obj->justif_kilometers;
79
                        $flight->date_creation = $obj->date_creation;
80
                        $flight->date_update = $obj->date_update;
81
82
                        $moneyReceiver = new MoneyReceiver($obj->firstname, $obj->lastname, $obj->usrRowId);
83
                        $array->addFlight($moneyReceiver, $flight);
84
                    }
85
                    $i++;
86
                }
87
            }
88
        }
89
90
        return $array;
91
    }
92
93
}