Car::getId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Madkom\RegistryApplication\Domain\CarManagement;
4
5
use Madkom\RegistryApplication\Domain\CarManagement\CarExceptions\DuplicatedVehicleInspectionException;
6
use Madkom\RegistryApplication\Domain\CarManagement\CarExceptions\InvalidDatesException;
7
use Madkom\RegistryApplication\Domain\CarManagement\CarExceptions\NonexistentInsuranceException;
8
use Madkom\RegistryApplication\Domain\CarManagement\CarExceptions\RemovingNonexistentElementException;
9
use Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\DuplicatedInsuranceException;
10
use Madkom\RegistryApplication\Domain\CarManagement\Insurances\Insurance;
11
use Madkom\RegistryApplication\Domain\CarManagement\Insurances\InsuranceDocument;
12
use Madkom\RegistryApplication\Domain\CarManagement\Insurances\InsuranceDuplicationChecker;
13
use Madkom\RegistryApplication\Domain\CarManagement\VehicleInspection\VehicleInspection;
14
use Madkom\RegistryApplication\Domain\CarManagement\VehicleInspection\VehicleInspectionDateChecker;
15
use Madkom\RegistryApplication\Domain\CarManagement\VehicleInspection\VehicleInspectionDuplicationChecker;
16
17
class Car
18
{
19
    /** @var string */
20
    private $id;
21
22
    /** @var string */
23
    private $responsiblePerson;
24
25
    /** @var string */
26
    private $model;
27
28
    /** @var string */
29
    private $brand;
30
31
    /** @var string */
32
    private $registrationNumber;
33
34
    /** @var \DateTime */
35
    private $productionDate;
36
37
    /** @var \DateTime */
38
    private $warrantyPeriod;
39
40
    /** @var string */
41
    private $fuelType;
0 ignored issues
show
Unused Code introduced by
The property $fuelType is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
42
43
    /** @var string */
44
    private $engineSize;
0 ignored issues
show
Unused Code introduced by
The property $engineSize is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
45
46
    /** @var string */
47
    private $gearBox;
0 ignored issues
show
Unused Code introduced by
The property $gearBox is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
48
49
    /** @var string */
50
    private $city;
51
52
    /** @var string */
53
    private $department;
54
55
    /** @var Insurance[] */
56
    private $insurances = [];
57
58
    /** @var CarDocument[] */
59
    private $carDocuments = [];
60
61
    /** @var VehicleInspection[] */
62
    private $vehicleInspection = [];
63
64
    /**
65
     * Car constructor.
66
     *
67
     * @param           $id
68
     * @param           $model
69
     * @param           $brand
70
     * @param           $registrationNumber
71
     * @param \DateTime $productionDate
72
     * @param \DateTime $warrantyPeriod
73
     * @param           $city
74
     * @param           $department
75
     */
76 View Code Duplication
    private function __construct(
0 ignored issues
show
Duplication introduced by
This method 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...
77
        $id,
78
        $responsiblePerson,
79
        $model,
80
        $brand,
81
        $registrationNumber,
82
        $productionDate,
83
        $warrantyPeriod,
84
        $city,
85
        $department
86
    ) {
87
        $this->id = $id;
88
        $this->responsiblePerson = $responsiblePerson;
89
        $this->model = $model;
90
        $this->brand = $brand;
91
        $this->registrationNumber = $registrationNumber;
92
        $this->productionDate = $productionDate;
93
        $this->warrantyPeriod = $warrantyPeriod;
94
        $this->city = $city;
95
        $this->department = $department;
96
    }
97
98
    /**
99
     * @param           $id
100
     * @param           $model
101
     * @param           $brand
102
     * @param           $registrationNumber
103
     * @param \DateTime $productionDate
104
     * @param \DateTime $warrantyPeriod
105
     * @param           $city
106
     * @param           $department
107
     *
108
     * @return \Madkom\RegistryApplication\Domain\CarManagement\Car
109
     */
110
    public static function createCustom(
111
        $id,
112
        $responsiblePerson,
113
        $model,
114
        $brand,
115
        $registrationNumber,
116
        $productionDate,
117
        $warrantyPeriod,
118
        $city,
119
        $department
120
    ) {
121
        return new self(
122
            $id,
123
            $responsiblePerson,
124
            $model,
125
            $brand,
126
            $registrationNumber,
127
            $productionDate,
128
            $warrantyPeriod,
129
            $city,
130
            $department
131
        );
132
    }
133
134
    /**
135
     * @param string $responsiblePerson
136
     */
137
    public function changeResponsiblePersonTo($responsiblePerson)
138
    {
139
        $this->responsiblePerson = $responsiblePerson;
140
    }
141
142
    public function addVehicleInspection(VehicleInspection $newVehicleInspection)
143
    {
144
        $duplicationChecker = new VehicleInspectionDuplicationChecker();
145
        $isDuplicated = $duplicationChecker->checkForDuplicates($this->vehicleInspection, $newVehicleInspection);
146
        if ($isDuplicated) {
147
            throw new DuplicatedVehicleInspectionException();
148
        }
149
150
        $dateChecker = new VehicleInspectionDateChecker();
151
        $isInvalidDates = $dateChecker->checkDates($newVehicleInspection);
152
        if ($isInvalidDates) {
153
            throw new InvalidDatesException();
154
        }
155
156
        $this->vehicleInspection[] = $newVehicleInspection;
157
    }
158
159
    /**
160
     * @param \Madkom\RegistryApplication\Domain\CarManagement\VehicleInspection\VehicleInspection $vehicleInspection
161
     *
162
     * @throws \Madkom\RegistryApplication\Domain\CarManagement\CarExceptions\RemovingNonexistentElementException
163
     */
164
    public function removeVehicleInspection(VehicleInspection $vehicleInspection)
165
    { // REFACTOR: przekazywać tylko ID
166
        if (($key = array_search($vehicleInspection, $this->vehicleInspection, true)) !== false) {
167
            unset($this->vehicleInspection[$key]);
168
        } else {
169
            throw new RemovingNonexistentElementException();
170
        }
171
    }
172
173
    public function getVehicleInspection()
174
    {
175
        return $this->vehicleInspection;
176
    }
177
178
    public function addInsurance(Insurance $newInsurance)
179
    {
180
        $duplicationChecker = new InsuranceDuplicationChecker();
181
        $isDuplicated = $duplicationChecker->checkForDuplicates($this->insurances, $newInsurance);
182
        if ($isDuplicated) {
183
            throw new DuplicatedInsuranceException();
184
        }
185
186
        $this->insurances[] = $newInsurance;
187
    }
188
189
    public function removeInsurance($selectedInsuranceId)
190
    {
191
        foreach ($this->insurances as $key => $insurance) {
192
            if ($insurance->getId() === $selectedInsuranceId) {
193
                unset($this->insurances[$key]);
194
195
                return 0;
196
            }
197
        }
198
199
        throw new \InvalidArgumentException();
200
    }
201
202
    public function addInsuranceDocument($insuranceId, InsuranceDocument $insuranceDocument)
203
    {
204
        foreach ($this->insurances as &$insurance) {
205
            if ($insurance->getId() === $insuranceId) {
206
                $insurance->addInsuranceDocument($insuranceDocument);
207
208
                return 0;
209
            }
210
        }
211
212
        throw new NonexistentInsuranceException('Nie odnaleziono wybranego ubezpieczenia.');
213
    }
214
215
    public function getInsuranceDocuments($insuranceId)
216
    {
217
        foreach ($this->insurances as $insurance) {
218
            if ($insurance->getId() === $insuranceId) {
219
                return $insurance->getInsuranceDocuments();
220
            }
221
        }
222
223
        throw new NonexistentInsuranceException('Nie odnaleziono wybranego ubezpieczenia.');
224
    }
225
226
    public function addCarDocument(CarDocument $carDocument)
227
    {
228
        $this->carDocuments[] = $carDocument;
229
    }
230
231
    public function removeCarDocument($carDocumentId)
232
    {
233
        foreach ($this->carDocuments as $key => $document) {
234
            if ($document->getId() === $carDocumentId) {
235
                unset($this->carDocuments[$key]);
236
237
                return 0;
238
            }
239
        }
240
241
        throw new \InvalidArgumentException();
242
    }
243
244
    public function getCarDocument()
245
    {
246
        return $this->carDocuments;
247
    }
248
249
    public function removeInsuranceDocument($insuranceId, $documentId)
250
    {
251
        foreach ($this->insurances as $insurance) {
252
            if ($insurance->getId() === $insuranceId) {
253
                $insurance->removeInsuranceDocument($documentId);
254
255
                return 0;
256
            }
257
        }
258
259
        throw new NonexistentInsuranceException('Nie odnaleziono wybranego ubezpieczenia.');
260
    }
261
262
    public function getInsurances()
263
    {
264
        return $this->insurances;
265
    }
266
267
    public function changeCity($city)
268
    {
269
        $this->city = $city;
270
    }
271
272
    public function changeDepartment($department)
273
    {
274
        $this->department = $department;
275
    }
276
277
    public function getId()
278
    {
279
        return $this->id;
280
    }
281
}
282