CreateCategoryCommandHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 51
ccs 13
cts 16
cp 0.8125
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 15 2
1
<?php
2
3
namespace App\CommandHandler;
4
5
use App\Command\CreateCategoryCommand;
6
use App\Entity\Category;
7
use App\Entity\User;
8
use App\Repository\CategoryRepository;
9
use App\Repository\UserRepository;
10
use Symfony\Component\Messenger\MessageBusInterface;
11
use App\CommandHandler\Exception\CategoryNotCreatedException;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\Security\Core\Security;
14
15
class CreateCategoryCommandHandler implements CommandHandlerInterface
16
{
17
    /**
18
     * @var CategoryRepository
19
     */
20
    private $repository;
21
    /**
22
     * @var MessageBusInterface
23
     */
24
    private $eventBus;
25
    /**
26
     * @var LoggerInterface
27
     */
28
    private $logger;
29
    /**
30
     * @var Security
31
     */
32
    private $security;
33
34
    /** 
35
     * @param MessageBusInterface $eventBus
36
     * @param CategoryRepository $repository
37
     * @param LoggerInterface $logger
38
     * @param Security $security
39
     */
40 1
    public function __construct(MessageBusInterface $eventBus, CategoryRepository $repository, LoggerInterface $logger, Security $security)
41
    {
42 1
        $this->eventBus = $eventBus;
43 1
        $this->repository = $repository;
44 1
        $this->logger = $logger;
45 1
        $this->security = $security;
46 1
    }
47
48
    /**
49
     * @param CreateCategoryCommand $command
50
     */
51 1
    public function __invoke(CreateCategoryCommand $command)
52
    {
53
        try {
54 1
            $user = $this->security->getUser();
55
56 1
            $category = new Category(
57 1
                $command->getId(),
58 1
                $command->getName(),
59 1
                $command->getDescription(),
60
                $user
61
                );
62 1
            $this->repository->save($category);
63
        } catch (\Exception $e) {
64
            $this->logger->error($e->getMessage());
65
            throw new CategoryNotCreatedException('Category was not created: '.$e->getMessage());
66
        }
67 1
    }
68
}
69