1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once __DIR__ . '/../query/MonthlyBillableQueryHandler.php'; |
4
|
|
|
require_once __DIR__ . '/../query/MonthlyBillableQuery.php'; |
5
|
|
|
require_once __DIR__ . '/CreateReceiverMonthBillCommandHandler.php'; |
6
|
|
|
require_once __DIR__ . '/CreateReceiverMonthBillCommand.php'; |
7
|
|
|
require_once __DIR__ . '/CreateMonthBillCommand.php'; |
8
|
|
|
require_once __DIR__ . '/CommandHandlerInterface.php'; |
9
|
|
|
require_once __DIR__ . '/CommandInterface.php'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Laurent De Coninck <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class CreateMonthBillCommandHandler implements CommandHandlerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DoliDB |
18
|
|
|
*/ |
19
|
|
|
private $db; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var stdClass |
23
|
|
|
*/ |
24
|
|
|
private $conf; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var User |
28
|
|
|
*/ |
29
|
|
|
private $user; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Translate |
33
|
|
|
*/ |
34
|
|
|
private $langs; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var MonthlyBillableQueryHandler |
38
|
|
|
*/ |
39
|
|
|
private $queryHandler; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var CreateReceiverMonthBillCommandHandler |
43
|
|
|
*/ |
44
|
|
|
private $receiverMonthBillCommandHandler; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param DoliDB $db |
48
|
|
|
* @param stdClass $conf |
49
|
|
|
* @param User $user |
50
|
|
|
* @param Translate $langs |
51
|
|
|
*/ |
52
|
|
|
public function __construct($db, $conf, $user, $langs) |
53
|
|
|
{ |
54
|
|
|
$this->db = $db; |
55
|
|
|
$this->conf = $conf; |
56
|
|
|
$this->user = $user; |
57
|
|
|
$this->langs = $langs; |
58
|
|
|
$this->queryHandler = new MonthlyBillableQueryHandler($db, $conf); |
59
|
|
|
$this->receiverMonthBillCommandHandler = new CreateReceiverMonthBillCommandHandler($db, $conf, $user, $langs); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param CommandInterface|CreateMonthBillCommand $command |
64
|
|
|
* |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
|
|
public function handle(CommandInterface $command) |
68
|
|
|
{ |
69
|
|
|
$queryResult = $this->queryHandler->__invoke(new MonthlyBillableQuery( |
70
|
|
|
$command->getMonth(), |
|
|
|
|
71
|
|
|
$command->getYear()) |
|
|
|
|
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
foreach ($queryResult->getFlights() as $currentReceiver) { |
75
|
|
|
$this->db->begin(); |
76
|
|
|
|
77
|
|
|
try { |
78
|
|
|
$this->createBill($command, $currentReceiver); |
79
|
|
|
$this->db->commit(); |
80
|
|
|
} catch (\Exception $e) { |
81
|
|
|
$this->db->rollback($e->getTraceAsString()); |
82
|
|
|
throw $e; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param CreateMonthBillCommand|CommandInterface $command |
91
|
|
|
* @param MonthlyFlightBill $currentReceiver |
92
|
|
|
*/ |
93
|
|
|
private function createBill($command, $currentReceiver) |
94
|
|
|
{ |
95
|
|
|
$receiverCommand = new CreateReceiverMonthBillCommand( |
96
|
|
|
$currentReceiver, |
97
|
|
|
$command->getBillType(), |
|
|
|
|
98
|
|
|
$command->getPublicNote(), |
|
|
|
|
99
|
|
|
$command->getPrivateNote(), |
|
|
|
|
100
|
|
|
$command->getYear(), |
|
|
|
|
101
|
|
|
$command->getMonth() |
|
|
|
|
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$this->receiverMonthBillCommandHandler->handle($receiverCommand); |
105
|
|
|
} |
106
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: