Completed
Push — feature/fixing_cost ( ef981e )
by Laurent
01:53
created

GetDamagesForFlightQueryRepository::__invoke()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 22
loc 22
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
4
namespace FlightLog\Infrastructure\Damage\Query\Repository;
5
6
7
use FlightLog\Application\Damage\Query\GetDamagesForFlightQueryRepositoryInterface;
8
use FlightLog\Application\Damage\ViewModel\Damage;
9
10 View Code Duplication
final class GetDamagesForFlightQueryRepository implements GetDamagesForFlightQueryRepositoryInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
11
{
12
13
    /**
14
     * @var \DoliDB
15
     */
16
    private $db;
17
18
    /**
19
     * @param $db
20
     */
21
    public function __construct($db)
22
    {
23
        $this->db = $db;
24
    }
25
26
    /**
27
     * @param int $flightId
28
     *
29
     * @return Damage[]|array
30
     */
31
    public function __invoke($flightId)
32
    {
33
        $sql = 'SELECT damage.amount, author.login as author_name';
34
        $sql.=' FROM '.MAIN_DB_PREFIX.'bbc_flight_damages as damage';
35
        $sql.=' INNER JOIN '.MAIN_DB_PREFIX.'user as author ON author.rowid = damage.author_id';
36
        $sql.=' WHERE damage.flight_id = '.$this->db->escape($flightId);
37
38
        $damages = [];
39
40
        $resql = $this->db->query($sql);
41
        if ($resql) {
42
            $num = $this->db->num_rows($resql);
43
            if ($num) {
44
                for($i = 0; $i < $num ; $i++) {
45
                    $properties = $this->db->fetch_array($resql);
46
                    $damages[] = Damage::fromArray($properties);
47
                }
48
            }
49
        }
50
51
        return $damages;
52
    }
53
}