Completed
Branch feature/create_monthly_billing (30fb74)
by Laurent
02:14
created

CreateMonthBillCommandHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A handle() 0 21 3
A createBill() 0 16 1
1
<?php
2
3
require_once __DIR__ . '/../query/MonthlyBillableQueryHandler.php';
4
require_once __DIR__ . '/../query/MonthlyBillableQuery.php';
5
require_once __DIR__ . '/CreatePilotMonthBillCommandHandler.php';
6
require_once __DIR__ . '/CreatePilotMonthBillCommand.php';
7
require_once __DIR__ . '/CreateMonthBillCommand.php';
8
9
/**
10
 * @author Laurent De Coninck <[email protected]>
11
 */
12
class CreateMonthBillCommandHandler
13
{
14
    /**
15
     * @var DoliDB
16
     */
17
    private $db;
18
19
    /**
20
     * @var stdClass
21
     */
22
    private $conf;
23
24
    /**
25
     * @var User
26
     */
27
    private $user;
28
29
    /**
30
     * @var Translate
31
     */
32
    private $langs;
33
34
    /**
35
     * @var MonthlyBillableQueryHandler
36
     */
37
    private $queryHandler;
38
39
    /**
40
     * @var CreatePilotMonthBillCommandHandler
41
     */
42
    private $receiverMonthBillCommandHandler;
43
44
    /**
45
     * @param DoliDB    $db
46
     * @param stdClass  $conf
47
     * @param User      $user
48
     * @param Translate $langs
49
     */
50
    public function __construct($db, $conf, $user, $langs)
51
    {
52
        $this->db = $db;
53
        $this->conf = $conf;
54
        $this->user = $user;
55
        $this->langs = $langs;
56
        $this->queryHandler = new MonthlyBillableQueryHandler($db, $conf);
57
        $this->receiverMonthBillCommandHandler = new CreatePilotMonthBillCommandHandler($db, $conf, $user, $langs);
58
    }
59
60
    /**
61
     * @param CreateMonthBillCommand $command
62
     *
63
     * @throws Exception
64
     */
65
    public function handle($command)
66
    {
67
        $queryResult = $this->queryHandler->__invoke(new MonthlyBillableQuery(
68
                $command->getMonth(),
69
                $command->getYear())
70
        );
71
72
        foreach ($queryResult->getFlights() as $currentReceiver) {
73
            $this->db->begin();
74
75
            try {
76
                $this->createBill($command, $currentReceiver);
77
                $this->db->commit();
78
            } catch (\Exception $e) {
79
                $this->db->rollback($e->getTraceAsString());
80
                throw $e;
81
            }
82
83
        }
84
85
    }
86
87
    /**
88
     * @param CreateMonthBillCommand $command
89
     * @param MonthlyFlightBill      $currentReceiver
90
     */
91
    private function createBill($command, $currentReceiver)
92
    {
93
        $receiverCommand = new CreatePilotMonthBillCommand(
94
            $currentReceiver,
95
            $command->getBillingType(),
96
            $command->getBillType(),
97
            $command->getBillingCondition(),
98
            $command->getModelDocument(),
99
            $command->getPublicNote(),
100
            $command->getPrivateNote(),
101
            $command->getYear(),
102
            $command->getMonth()
103
        );
104
105
        $this->receiverMonthBillCommandHandler->handle($receiverCommand);
106
    }
107
}