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

GetPilotDamagesQueryRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A query() 23 23 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
use FlightLog\Application\Damage\Query\GetPilotDamagesQueryRepositoryInterface;
7
use FlightLog\Application\Damage\ViewModel\TotalDamage;
8
9 View Code Duplication
final class GetPilotDamagesQueryRepository implements GetPilotDamagesQueryRepositoryInterface
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...
10
{
11
12
    /**
13
     * @var \DoliDB
14
     */
15
    private $db;
16
17
    /**
18
     * @param $db
19
     */
20
    public function __construct($db)
21
    {
22
        $this->db = $db;
23
    }
24
25
    /**
26
     * @param int $year
27
     *
28
     * @return TotalDamage[]|array
29
     */
30
    public function query($year)
31
    {
32
        $sql = 'SELECT SUM(damage.amount) as total_amount, damage.author_id as author';
33
        $sql.=' FROM '.MAIN_DB_PREFIX.'bbc_flight_damages as damage';
34
        $sql.=' INNER JOIN '.MAIN_DB_PREFIX.'bbc_vols as flight ON flight.rowid = damage.flight_id';
35
        $sql.=' WHERE YEAR(flight.date) = '.$this->db->escape($year);
36
        $sql.=' GROUP BY damage.author_id';
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[] = TotalDamage::fromArray($properties);
47
                }
48
            }
49
        }
50
51
        return $damages;
52
    }
53
}