Completed
Push — master ( a5a50f...1f5af2 )
by Laurent
01:49
created

CreateMonthBillCommandHandler::createBill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
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->getBillingType(),
98
            $command->getBillType(),
99
            $command->getBillingCondition(),
100
            $command->getModelDocument(),
101
            $command->getPublicNote(),
102
            $command->getPrivateNote(),
103
            $command->getYear(),
104
            $command->getMonth()
105
        );
106
107
        $this->receiverMonthBillCommandHandler->handle($receiverCommand);
108
    }
109
}