|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Madkom\RegistryApplication\Domain\CarManagement\Insurances; |
|
4
|
|
|
|
|
5
|
|
|
use Madkom\RegistryApplication\Domain\CarManagement\CarExceptions\InvalidDatesException; |
|
6
|
|
|
use Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\EmptyInsuranceDateException; |
|
7
|
|
|
use Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\UnknownInsuranceTypeException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class InsuranceFactory. |
|
11
|
|
|
*/ |
|
12
|
|
|
class InsuranceFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param string $insuranceType |
|
16
|
|
|
* @param \DateTime $dateFrom |
|
17
|
|
|
* @param \DateTime $dateTo |
|
18
|
|
|
* @param string $insuranceId |
|
19
|
|
|
* @param string $insurerId |
|
20
|
|
|
* |
|
21
|
|
|
* @throws \Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\EmptyInsuranceDateException |
|
22
|
|
|
* @throws \Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\UnknownInsuranceTypeException |
|
23
|
|
|
* @throws \InvalidArgumentException |
|
24
|
|
|
* @throws \Madkom\RegistryApplication\Domain\CarManagement\CarExceptions\InvalidDatesException |
|
25
|
|
|
* |
|
26
|
|
|
* @return Insurance |
|
27
|
|
|
*/ |
|
28
|
|
|
public function create($insuranceId, $insuranceType, $dateFrom, $dateTo, $insurerId) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->areEmpty($dateFrom, $dateTo); |
|
31
|
|
|
$this->hasValidDateFormat($dateFrom, $dateTo); |
|
32
|
|
|
$this->isEqualToOneYear($dateFrom, $dateTo); |
|
33
|
|
|
|
|
34
|
|
|
switch ($insuranceType) { |
|
35
|
|
|
case CarInsurance::INSURANCE_TYPE: |
|
36
|
|
|
$insurance = new CarInsurance($insuranceId, $dateFrom, $dateTo, $insurerId); |
|
37
|
|
|
break; |
|
38
|
|
|
case AccidentInsurance::INSURANCE_TYPE: |
|
39
|
|
|
$insurance = new AccidentInsurance($insuranceId, $dateFrom, $dateTo, $insurerId); |
|
40
|
|
|
break; |
|
41
|
|
|
case AssistanceInsurance::INSURANCE_TYPE: |
|
42
|
|
|
$insurance = new AssistanceInsurance($insuranceId, $dateFrom, $dateTo, $insurerId); |
|
43
|
|
|
break; |
|
44
|
|
|
case LiabilityInsurance::INSURANCE_TYPE: |
|
45
|
|
|
$insurance = new LiabilityInsurance($insuranceId, $dateFrom, $dateTo, $insurerId); |
|
46
|
|
|
break; |
|
47
|
|
|
default: |
|
48
|
|
|
throw new UnknownInsuranceTypeException('Unknown insurance type: '.$insuranceType); |
|
49
|
|
|
break; |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $insurance; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param \DateTime $dateFrom |
|
57
|
|
|
* @param \DateTime $dateTo |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \Madkom\RegistryApplication\Domain\CarManagement\Insurances\Exceptions\EmptyInsuranceDateException |
|
60
|
|
|
*/ |
|
61
|
|
|
private function areEmpty($dateFrom, $dateTo) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($dateFrom === '' or $dateTo === '') { |
|
|
|
|
|
|
64
|
|
|
throw new EmptyInsuranceDateException('The \'date from\' or \'date to\' is empty'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param \DateTime $dateFrom |
|
70
|
|
|
* @param \DateTime $dateTo |
|
71
|
|
|
* |
|
72
|
|
|
* @throws \InvalidArgumentException |
|
73
|
|
|
*/ |
|
74
|
|
|
private function hasValidDateFormat($dateFrom, $dateTo) |
|
75
|
|
|
{ |
|
76
|
|
|
if (!($dateFrom instanceof \DateTime) or !($dateTo instanceof \DateTime)) { |
|
|
|
|
|
|
77
|
|
|
throw new \InvalidArgumentException('The \'date from\' or \'date to\' has not be instance of DateTime class.' |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param \DateTime $dateFrom |
|
84
|
|
|
* @param \DateTime $dateTo |
|
85
|
|
|
*/ |
|
86
|
|
|
private function isEqualToOneYear($dateFrom, $dateTo) |
|
87
|
|
|
{ |
|
88
|
|
|
$interval = $dateTo->diff($dateFrom); |
|
89
|
|
|
|
|
90
|
|
|
if ($interval->days < 365 or $interval->days > 366) { |
|
|
|
|
|
|
91
|
|
|
throw new InvalidDatesException('Umowa z ubezpieczeniem może być tylko na rok.'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.