Pilot   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 177
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 4

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 4 1
A getName() 0 4 1
A getId() 0 4 1
A addCount() 0 24 4
A getCountForType() 0 10 3
A getFlightBonus() 0 9 1
A getFlightsCost() 0 11 1
A getTotalBill() 0 10 2
A isBillable() 0 4 1
A getFlightPoints() 0 4 1
A getFlightCost() 0 4 1
1
<?php
2
3
require_once(DOL_DOCUMENT_ROOT . '/flightlog/class/flight/FlightBonus.php');
4
require_once(DOL_DOCUMENT_ROOT . '/flightlog/class/flight/FlightPoints.php');
5
require_once(DOL_DOCUMENT_ROOT . '/flightlog/class/flight/FlightTypeCount.php');
6
require_once(DOL_DOCUMENT_ROOT . '/flightlog/class/billing/FlightCost.php');
7
8
/**
9
 * Pilot class
10
 *
11
 * @author Laurent De Coninck <[email protected]>
12
 */
13
class Pilot
14
{
15
16
    /**
17
     * @var string
18
     */
19
    private $name;
20
21
    /**
22
     * @var int
23
     */
24
    private $id;
25
26
    /**
27
     * @var array|FlightTypeCount[]
28
     */
29
    private $flightTypeCounts;
30
31
    /**
32
     * @param string $name
33
     * @param int    $id
34
     * @param array  $flightTypeCounts
35
     */
36
    private function __construct($name, $id, $flightTypeCounts)
37
    {
38
        $this->name = $name;
39
        $this->id = $id;
40
        $this->flightTypeCounts = $flightTypeCounts;
41
    }
42
43
    /**
44
     * @param string $name
45
     * @param int    $id
46
     *
47
     * @return Pilot
48
     */
49
    public static function create($name, $id)
50
    {
51
        return new Pilot($name, $id, []);
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getName()
58
    {
59
        return $this->name;
60
    }
61
62
    /**
63
     * @return int
64
     */
65
    public function getId()
66
    {
67
        return $this->id;
68
    }
69
70
    /**
71
     * @param FlightTypeCount $flightTypeCount
72
     *
73
     * @return Pilot
74
     */
75
    public function addCount(FlightTypeCount $flightTypeCount)
76
    {
77
        $types = [];
78
79
        $found = false;
80
        /** @var FlightTypeCount $currentType */
81
        foreach ($this->flightTypeCounts as $currentType) {
82
            if ($currentType->getType() === $flightTypeCount->getType()) {
83
                $found = true;
84
                $types[] = $currentType->add($flightTypeCount);
85
                break;
86
            }
87
88
            $types[] = new FlightTypeCount($currentType->getType(), $currentType->getCount(),
89
                $currentType->getFactor());
90
        }
91
92
        if (!$found) {
93
            $types[] = new FlightTypeCount($flightTypeCount->getType(), $flightTypeCount->getCount(),
94
                $flightTypeCount->getFactor());
95
        }
96
97
        return new Pilot($this->name, $this->id, $types);
98
    }
99
100
    /**
101
     * @param string $type
102
     *
103
     * @return FlightTypeCount
104
     */
105
    public function getCountForType($type)
106
    {
107
        foreach ($this->flightTypeCounts as $flightTypeCount) {
108
            if ($flightTypeCount->getType() === $type) {
109
                return $flightTypeCount;
110
            }
111
        }
112
113
        return new FlightTypeCount($type);
114
    }
115
116
    /**
117
     * @return FlightBonus
118
     */
119
    public function getFlightBonus()
120
    {
121
        $bonus = FlightBonus::zero();
122
123
        $bonus = $bonus->addPoints($this->getFlightPoints('1'));
124
        $bonus = $bonus->addPoints($this->getFlightPoints('2'));
125
        $bonus = $bonus->addPoints($this->getFlightPoints('orga'));
126
        return $bonus->addPoints($this->getFlightPoints('orga_T6'));
127
    }
128
129
    /**
130
     * Get the total of cost for the pilot
131
     */
132
    public function getFlightsCost()
133
    {
134
        $flightsCost = FlightCost::zero();
135
136
        $flightsCost = $flightsCost->addCost($this->getFlightCost('3'));
137
        $flightsCost = $flightsCost->addCost($this->getFlightCost('4'));
138
        $flightsCost = $flightsCost->addCost($this->getFlightCost('6'));
139
        $flightsCost = $flightsCost->addCost($this->getFlightCost('7'));
140
141
        return $flightsCost;
142
    }
143
144
    /**
145
     * @return FlightCost
146
     */
147
    public function getTotalBill()
148
    {
149
150
        $totalBill = $this->getFlightsCost()->minBonus($this->getFlightBonus());
151
        if ($totalBill->getValue() < 0) {
152
            return FlightCost::zero();
153
        }
154
155
        return $totalBill;
156
    }
157
158
    /**
159
     * @return boolean
160
     */
161
    public function isBillable()
162
    {
163
        return $this->getTotalBill()->getValue() > 0;
164
    }
165
166
    /**
167
     * @param string $type
168
     *
169
     * @return FlightPoints
170
     */
171
    private function getFlightPoints($type)
172
    {
173
        return FlightPoints::create($this->getCountForType($type)->getCost()->getValue());
174
    }
175
176
    /**
177
     * Get the flight cost for a type
178
     *
179
     * @param string $type
180
     *
181
     * @return FlightCost
182
     */
183
    private function getFlightCost($type)
184
    {
185
        return $this->getCountForType($type)->getCost();
186
    }
187
188
189
}