Completed
Pull Request — master (#5)
by Laurent
02:35 queued 42s
created

CreateMonthBillCommandHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 4
dl 0
loc 9
rs 9.6666
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);
0 ignored issues
show
Compatibility introduced by
$command of type object<CommandInterface> is not a sub-type of object<CreateMonthBillCommand>. It seems like you assume a concrete implementation of the interface CommandInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
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 $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
}