Completed
Push — feature/fixing_cost ( f58af6...414fbf )
by Laurent
01:41
created

GetDamageQueryRepository::query()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 7
nop 1
dl 0
loc 36
rs 8.7217
c 0
b 0
f 0
1
<?php
2
3
4
namespace FlightLog\Infrastructure\Damage\Query\Repository;
5
6
7
use FlightLog\Application\Damage\ViewModel\Damage;
8
9
final class GetDamageQueryRepository
10
{
11
12
    /**
13
     * @var \DoliDB
14
     */
15
    private $db;
16
17
    /**
18
     * @param \DoliDB $db
19
     */
20
    public function __construct(\DoliDB $db)
21
    {
22
        $this->db = $db;
23
    }
24
25
    private function element($objectType, $rowid){
26
        $element = null;
27
        $subelement = null;
28
29
        if ($objectType != 'supplier_proposal' && $objectType != 'order_supplier' && $objectType != 'invoice_supplier'
30
            && preg_match('/^([^_]+)_([^_]+)/i', $objectType, $regs))
31
        {
32
            $element = $regs[1];
33
            $subelement = $regs[2];
34
        }
35
36
        $classpath = $element.'/class';
37
38
        if ($objectType == 'facture') {
39
            $classpath = 'compta/facture/class';
40
        }
41
        elseif ($objectType == 'facturerec') {
42
            $classpath = 'compta/facture/class';
43
        }
44
        elseif ($objectType == 'propal') {
45
            $classpath = 'comm/propal/class';
46
        }
47
        elseif ($objectType == 'supplier_proposal') {
48
            $classpath = 'supplier_proposal/class';
49
        }
50
        elseif ($objectType == 'shipping') {
51
            $classpath = 'expedition/class'; $subelement = 'expedition';
52
        }
53
        elseif ($objectType == 'delivery') {
54
            $classpath = 'livraison/class'; $subelement = 'livraison';
55
        }
56
        elseif ($objectType == 'invoice_supplier' || $objectType == 'order_supplier') {
57
            $classpath = 'fourn/class';
58
        }
59
        elseif ($objectType == 'fichinter') {
60
            $classpath = 'fichinter/class'; $subelement = 'fichinter';
61
        }
62
        elseif ($objectType == 'subscription') {
63
            $classpath = 'adherents/class';
64
        }
65
66
        // Set classfile
67
        $classfile = strtolower($subelement); $classname = ucfirst($subelement);
68
69
        if ($objectType == 'order') {
70
            $classfile = 'commande'; $classname = 'Commande';
71
        }
72
        elseif ($objectType == 'invoice_supplier') {
73
            $classfile = 'fournisseur.facture'; $classname = 'FactureFournisseur';
74
        }
75
        elseif ($objectType == 'order_supplier') {
76
            $classfile = 'fournisseur.commande'; $classname = 'CommandeFournisseur';
77
        }
78
        elseif ($objectType == 'supplier_proposal') {
79
            $classfile = 'supplier_proposal'; $classname = 'SupplierProposal';
80
        }
81
        elseif ($objectType == 'facturerec') {
82
            $classfile = 'facture-rec'; $classname = 'FactureRec';
83
        }
84
        elseif ($objectType == 'subscription') {
85
            $classfile = 'subscription'; $classname = 'Subscription';
86
        }
87
88
        dol_include_once('/'.$classpath.'/'.$classfile.'.class.php');
89
        if (!class_exists($classname))
90
        {
91
            return null;
92
        }
93
94
        $object = new $classname($this->db);
95
        $object->fetch($rowid);
96
97
        return $object;
98
    }
99
100
    /**
101
     * @param int $damageId
102
     *
103
     * @return Damage
104
     *
105
     * @throws \Exception
106
     */
107
    public function query($damageId){
108
109
        $sql = 'SELECT damage.rowid as id, damage.amount, author.login as author_name, damage.billed as invoiced, damage.flight_id as flight_id';
110
        $sql.= ', element.rowid as element_id ,element.fk_target as element_fk_target, element.targettype as element_target_type';
111
        $sql.=' FROM '.MAIN_DB_PREFIX.'bbc_flight_damages as damage';
112
        $sql.=' INNER JOIN '.MAIN_DB_PREFIX.'user as author ON author.rowid = damage.author_id';
113
        $sql.=' LEFT JOIN '.MAIN_DB_PREFIX.'element_element as element ON (element.fk_source = damage.rowid AND element.sourcetype = "flightlog_damage")';
114
        $sql.=' WHERE damage.rowid = '.$damageId;
115
116
        $resql = $this->db->query($sql);
117
        if (!$resql) {
118
            throw new \Exception('damage not found');
119
        }
120
121
        $num = $this->db->num_rows($resql);
122
        if ($num == 0) {
123
            throw new \Exception('damage not found');
124
        }
125
126
        /** @var Damage $damage */
127
        $damage = null;
128
        for($i = 0; $i < $num ; $i++) {
129
            $properties = $this->db->fetch_array($resql);
130
            if(!$damage){
131
                $damage = Damage::fromArray($properties);
132
            }
133
134
            if(!isset($properties['element_id'])){
135
                continue;
136
            }
137
138
            $damage->addLink($properties['element_id'], $properties['element_target_type'], $this->element($properties['element_target_type'], $properties['element_fk_target']));
0 ignored issues
show
Documentation introduced by
$this->element($properti...s['element_fk_target']) is of type null|object, but the function expects a object<CommonObject>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
        }
140
141
        return $damage;
142
    }
143
}