Completed
Push — master ( 9abca0...16ab07 )
by Rémi
04:24
created

MessageAppCommandHandler::createUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 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\ApplicationUser;
9
use MessageApp\User\ApplicationUserFactory;
10
use MessageApp\User\ApplicationUserId;
11
use MessageApp\User\Repository\ApplicationUserRepository;
12
use MessageApp\User\UndefinedApplicationUser;
13
use Psr\Log\LoggerAwareInterface;
14
use Psr\Log\LoggerAwareTrait;
15
use Psr\Log\NullLogger;
16
use RemiSan\Context\ContextContainer;
17
18
class MessageAppCommandHandler implements LoggerAwareInterface
19
{
20
    use LoggerAwareTrait;
21
22
    /**
23
     * @var ApplicationUserFactory
24
     */
25
    private $userBuilder;
26
27
    /**
28
     * @var ApplicationUserRepository
29
     */
30
    private $userManager;
31
32
    /**
33
     * @var ErrorEventHandler
34
     */
35
    private $errorHandler;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param ApplicationUserFactory    $userBuilder
41
     * @param ApplicationUserRepository $userManager
42
     * @param ErrorEventHandler         $errorHandler
43
     */
44 9
    public function __construct(
45
        ApplicationUserFactory $userBuilder,
46
        ApplicationUserRepository $userManager,
47
        ErrorEventHandler $errorHandler
48
    ) {
49 9
        $this->userBuilder = $userBuilder;
50 9
        $this->userManager = $userManager;
51 9
        $this->errorHandler = $errorHandler;
52 9
        $this->logger = new NullLogger();
53 9
    }
54
55
    /**
56
     * Handles a CreateUserCommand
57
     *
58
     * @param  CreateUserCommand $command
59
     * @return string
60
     */
61 6
    public function handleCreateUserCommand(CreateUserCommand $command)
62
    {
63 6
        $user = null;
64 6
        $originalUser = $command->getOriginalUser();
65
66 6
        ContextContainer::setContext($command->getContext());
67
68
        try {
69 6
            $user = $this->createUser(
70 6
                $command->getId(),
71 4
                $originalUser,
72 6
                $command->getPreferredLanguage()
73 4
            );
74 3
            $this->userManager->save($user);
75 3
            $this->logger->info('User Created');
76 5
        } catch (\Exception $e) {
77 3
            $this->logger->error('Error creating the user');
78 3
            $this->errorHandler->handle(
79 3
                new UnableToCreateUserEvent(
80 3
                    new UndefinedApplicationUser($originalUser)
81 2
                )
82 2
            );
83
        }
84
85 6
        ContextContainer::reset();
86 6
    }
87
88
    /**
89
     * Creates the player
90
     *
91
     * @param ApplicationUserId $userId
92
     * @param  object           $originalUser
93
     * @param  string           $language
94
     *
95
     * @return ApplicationUser
96
     */
97 6
    private function createUser(ApplicationUserId $userId, $originalUser, $language)
98
    {
99 6
        $this->logger->debug('Trying to create user');
100 6
        return $this->userBuilder->create($userId, $originalUser, $language);
101
    }
102
}
103