|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\CommandHandler; |
|
4
|
|
|
|
|
5
|
|
|
use App\Command\UpdateLoanCommand; |
|
6
|
|
|
use App\Repository\LoanRepository; |
|
7
|
|
|
use App\Repository\ItemRepository; |
|
8
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
9
|
|
|
use App\CommandHandler\Exception\LoanNotUpdatedException; |
|
10
|
|
|
use Psr\Log\LoggerInterface; |
|
11
|
|
|
|
|
12
|
|
|
class UpdateLoanCommandHandler implements CommandHandlerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var LoanRepository |
|
16
|
|
|
*/ |
|
17
|
|
|
private $repository; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var MessageBusInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $eventBus; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var LoggerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $logger; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var ItemRepository |
|
28
|
|
|
*/ |
|
29
|
|
|
private $itemRepository; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param MessageBusInterface $eventBus |
|
33
|
|
|
* @param LoanRepository $repository |
|
34
|
|
|
* @param LoggerInterface $logger |
|
35
|
|
|
* @param ItemRepository $itemRepository |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function __construct( |
|
38
|
|
|
MessageBusInterface $eventBus, |
|
39
|
|
|
LoanRepository $repository, |
|
40
|
|
|
LoggerInterface $logger, |
|
41
|
|
|
ItemRepository $itemRepository |
|
42
|
|
|
) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$this->eventBus = $eventBus; |
|
45
|
1 |
|
$this->repository = $repository; |
|
46
|
1 |
|
$this->logger = $logger; |
|
47
|
1 |
|
$this->itemRepository = $itemRepository; |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param UpdateLoanCommand $command |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function __invoke(UpdateLoanCommand $command) |
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
1 |
|
$item = $this->itemRepository->getItem($command->getItemId()); |
|
57
|
1 |
|
$loan = $this->repository->getLoan($command->getId()); |
|
58
|
1 |
|
$loan->setItem($item); |
|
59
|
1 |
|
$loan->setLoaner($command->getLoaner()); |
|
60
|
1 |
|
$loan->setLoanDate($command->getLoanDate()); |
|
61
|
1 |
|
$loan->setReturnDate($command->getReturnDate()); |
|
62
|
1 |
|
$this->repository->save($loan); |
|
63
|
|
|
} catch (\Exception $e) { |
|
64
|
|
|
$this->logger->error($e->getMessage()); |
|
65
|
|
|
throw new LoanNotUpdatedException('Loan was not updated: '.$e->getMessage()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|