Completed
Push — master ( 505aad...bbab67 )
by Rémi
03:47
created

MessageAppCommandHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 4
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\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 \MessageApp\User\ThirdParty\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 \MessageApp\User\ThirdParty\AccountFactory $accountFactory
49
     * @param ErrorEventHandler                          $errorHandler
50
     */
51 9
    public function __construct(
52
        UserFactory $userBuilder,
53
        UserRepository $userManager,
54
        AccountFactory $accountFactory,
55
        ErrorEventHandler $errorHandler
56
    ) {
57 9
        $this->userBuilder = $userBuilder;
58 9
        $this->userManager = $userManager;
59 9
        $this->accountFactory = $accountFactory;
60 9
        $this->errorHandler = $errorHandler;
61 9
        $this->logger = new NullLogger();
62 9
    }
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