DeleteLoanCommandHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 38
ccs 11
cts 11
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 8 2
A __construct() 0 5 1
1
<?php
2
3
namespace App\CommandHandler;
4
5
use App\Command\DeleteLoanCommand;
6
use App\Repository\LoanRepository;
7
use Symfony\Component\Messenger\MessageBusInterface;
8
use App\CommandHandler\Exception\LoanNotDeletedException;
9
use Psr\Log\LoggerInterface;
10
11
class DeleteLoanCommandHandler implements CommandHandlerInterface
12
{
13
    /**
14
     * @var LoanRepository
15
     */
16
    private $repository;    
17
    /**
18
     * @var MessageBusInterface
19
     */
20
    private $eventBus;
21
    /**
22
     * @var LoggerInterface
23
     */
24
    private $logger;
25
26
    /**
27
     * @param MessageBusInterface $eventBus
28
     * @param LoanRepository $repository
29
     * @param LoggerInterface $logger
30
     */
31 2
    public function __construct(MessageBusInterface $eventBus, LoanRepository $repository, LoggerInterface $logger)
32
    {
33 2
        $this->eventBus = $eventBus;
34 2
        $this->repository = $repository;
35 2
        $this->logger = $logger;
36 2
    }
37
38
    /**
39
     * @param DeleteLoanCommand $command
40
     */
41 2
    public function __invoke(DeleteLoanCommand $command)
42
    {
43
        try {
44 2
            $loan = $this->repository->getLoan($command->getId());
45 1
            $this->repository->delete($loan);
46 1
        } catch (\Exception $e) {
47 1
            $this->logger->error($e->getMessage());
48 1
            throw new LoanNotDeletedException('Loan was not deleted: '.$e->getMessage());
49
        }
50 1
    }
51
}
52