Completed
Push — master ( 84cfc6...1601f8 )
by Laurent
01:38
created

Pilot::addCount()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 6
nop 1
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
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(), $currentType->getFactor());
89
        }
90
91
        if (!$found) {
92
            $types[] = new FlightTypeCount($flightTypeCount->getType(), $flightTypeCount->getCount(), $flightTypeCount->getFactor());
93
        }
94
95
        return new Pilot($this->name, $this->id, $types);
96
    }
97
98
    /**
99
     * @param string $type
100
     *
101
     * @return FlightTypeCount
102
     */
103
    public function getCountForType($type)
104
    {
105
        foreach ($this->flightTypeCounts as $flightTypeCount) {
106
            if ($flightTypeCount->getType() === $type) {
107
                return $flightTypeCount;
108
            }
109
        }
110
111
        return new FlightTypeCount($type);
112
    }
113
114
    /**
115
     * @return FlightBonus
116
     */
117
    public function getFlightBonus()
118
    {
119
        $bonus = FlightBonus::zero();
120
121
        $bonus = $bonus->addPoints($this->getFlightPoints('1'));
122
        $bonus = $bonus->addPoints($this->getFlightPoints('2'));
123
        $bonus = $bonus->addPoints($this->getFlightPoints('orga'));
124
        return $bonus->addPoints($this->getFlightPoints('orga_T6'));
125
    }
126
127
    /**
128
     * Get the total of cost for the pilot
129
     */
130
    public function getFlightsCost()
131
    {
132
        $flightsCost = FlightCost::zero();
133
134
        $flightsCost = $flightsCost->addCost($this->getFlightCost('3'));
135
        $flightsCost = $flightsCost->addCost($this->getFlightCost('4'));
136
        $flightsCost = $flightsCost->addCost($this->getFlightCost('6'));
137
        $flightsCost = $flightsCost->addCost($this->getFlightCost('7'));
138
139
        return $flightsCost;
140
    }
141
142
    /**
143
     * @return FlightCost
144
     */
145
    public function getTotalBill()
146
    {
147
148
        $totalBill = $this->getFlightsCost()->minBonus($this->getFlightBonus());
149
        if ($totalBill->getValue() < 0) {
150
            return FlightCost::zero();
151
        }
152
153
        return $totalBill;
154
    }
155
156
    /**
157
     * @return boolean
158
     */
159
    public function isBillable(){
160
        return $this->getTotalBill()->getValue() > 0;
161
    }
162
163
    /**
164
     * @param string $type
165
     *
166
     * @return FlightPoints
167
     */
168
    private function getFlightPoints($type)
169
    {
170
        return FlightPoints::create($this->getCountForType($type)->getCost()->getValue());
171
    }
172
173
    /**
174
     * Get the flight cost for a type
175
     *
176
     * @param string $type
177
     *
178
     * @return FlightCost
179
     */
180
    private function getFlightCost($type)
181
    {
182
        return $this->getCountForType($type)->getCost();
183
    }
184
185
186
}