MessageAppCommandHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 11
dl 0
loc 98
ccs 34
cts 34
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B handleCreateUserCommand() 0 32 2
A createUser() 0 5 1
1
<?php
2
3
namespace MessageApp\Handler;
4
5
use MessageApp\Command\CreateUserCommand;
6
use MessageApp\Error\ErrorEventHandler;
7
use MessageApp\Event\UnableToCreateUserEvent;
8
use MessageApp\User\ApplicationUserId;
9
use MessageApp\User\Entity\SourcedUser;
10
use MessageApp\User\Repository\UserRepository;
11
use MessageApp\User\ThirdParty\AccountFactory;
12
use MessageApp\User\UndefinedApplicationUser;
13
use MessageApp\User\UserFactory;
14
use Psr\Log\LoggerAwareInterface;
15
use Psr\Log\LoggerAwareTrait;
16
use Psr\Log\NullLogger;
17
use RemiSan\Context\ContextContainer;
18
19
class MessageAppCommandHandler implements LoggerAwareInterface
20
{
21
    use LoggerAwareTrait;
22
23
    /**
24
     * @var UserFactory
25
     */
26
    private $userBuilder;
27
28
    /**
29
     * @var UserRepository
30
     */
31
    private $userManager;
32
33
    /**
34
     * @var AccountFactory
35
     */
36
    private $accountFactory;
37
38
    /**
39
     * @var ErrorEventHandler
40
     */
41
    private $errorHandler;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param UserFactory       $userBuilder
47
     * @param UserRepository    $userManager
48
     * @param AccountFactory    $accountFactory
49
     * @param ErrorEventHandler $errorHandler
50
     */
51 6
    public function __construct(
52
        UserFactory $userBuilder,
53
        UserRepository $userManager,
54
        AccountFactory $accountFactory,
55
        ErrorEventHandler $errorHandler
56
    ) {
57 6
        $this->userBuilder = $userBuilder;
58 6
        $this->userManager = $userManager;
59 6
        $this->accountFactory = $accountFactory;
60 6
        $this->errorHandler = $errorHandler;
61 6
        $this->logger = new NullLogger();
62 6
    }
63
64
    /**
65
     * Handles a CreateUserCommand
66
     *
67
     * @param  CreateUserCommand $command
68
     */
69 6
    public function handleCreateUserCommand(CreateUserCommand $command)
70
    {
71 6
        $user = null;
72 6
        $originalUser = $command->getOriginalUser();
73
74 6
        ContextContainer::setContext($command->getContext());
75
76
        try {
77 6
            $user = $this->createUser(
78 6
                $command->getId(),
79 4
                $originalUser,
80 6
                $command->getPreferredLanguage()
81 4
            );
82
83 3
            $this->userManager->save($user);
84
85 3
            $this->logger->info('User Created');
86 5
        } catch (\Exception $e) {
87 3
            $this->logger->error('Error creating the user');
88 3
            $this->errorHandler->handle(
89 3
                new UnableToCreateUserEvent(
90 3
                    $command->getId(),
91 3
                    new UndefinedApplicationUser(
92 3
                        $this->accountFactory->build($originalUser)
93 2
                    )
94 2
                ),
95 3
                $command->getContext()
96 2
            );
97
        }
98
99 6
        ContextContainer::reset();
100 6
    }
101
102
    /**
103
     * Creates the player
104
     *
105
     * @param ApplicationUserId $userId
106
     * @param  object           $originalUser
107
     * @param  string           $language
108
     *
109
     * @return SourcedUser
110
     */
111 6
    private function createUser(ApplicationUserId $userId, $originalUser, $language)
112
    {
113 6
        $this->logger->debug('Trying to create user');
114 6
        return $this->userBuilder->create($userId, $originalUser, $language);
115
    }
116
}
117