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

GetDamagesForFlightQueryRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 44
loc 44
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A __invoke() 22 22 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}