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

ApplicationUserListener   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
c 2
b 1
f 0
lcom 1
cbo 10
dl 0
loc 89
ccs 0
cts 34
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A updateUserThirdPartyAccount() 0 12 2
A handleUserCreatedEvent() 0 19 2
A handleThirdPartyAccountLinkedEvent() 0 6 1
A handleThirdPartyAccountReplacedEvent() 0 6 1
1
<?php
2
3
namespace MessageApp\Listener;
4
5
use League\Event\ListenerInterface;
6
use MessageApp\Event\ThirdPartyAccountLinkedEvent;
7
use MessageApp\Event\ThirdPartyAccountReplacedEvent;
8
use MessageApp\Event\UserCreatedEvent;
9
use MessageApp\User\ApplicationUserId;
10
use MessageApp\User\Finder\AppUserFinder;
11
use MessageApp\User\ThirdParty\Account;
12
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LoggerAwareTrait;
14
use Psr\Log\NullLogger;
15
16
class ApplicationUserListener implements ListenerInterface, LoggerAwareInterface
17
{
18
    use ListenerTrait, LoggerAwareTrait;
19
20
    /**
21
     * @var ApplicationUserFactory
22
     */
23
    private $appUserFactory;
24
25
    /**
26
     * @var AppUserFinder
27
     */
28
    private $finder;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param ApplicationUserFactory $appUserFactory
34
     * @param AppUserFinder          $finder
35
     */
36
    public function __construct(
37
        ApplicationUserFactory $appUserFactory,
38
        AppUserFinder $finder
39
    ) {
40
        $this->appUserFactory = $appUserFactory;
41
        $this->finder = $finder;
42
        $this->logger = new NullLogger();
43
    }
44
45
    /**
46
     * @param UserCreatedEvent $event
47
     */
48
    public function handleUserCreatedEvent(UserCreatedEvent $event)
49
    {
50
        $this->logger->info('Message read model received user event');
51
52
        $user = $this->finder->find($event->getUserId());
53
54
        if ($user) {
55
            $user->setName($event->getUsername());
56
            $user->setPreferredLanguage($event->getPreferredLanguage());
57
        } else {
58
            $user = $this->appUserFactory->create(
59
                $event->getUserId(),
60
                $event->getUsername(),
61
                $event->getPreferredLanguage()
62
            );
63
        }
64
65
        $this->finder->save($user);
66
    }
67
68
    /**
69
     * @param ThirdPartyAccountLinkedEvent $event
70
     */
71
    public function handleThirdPartyAccountLinkedEvent(ThirdPartyAccountLinkedEvent $event)
72
    {
73
        $this->logger->info('Third party account linked event');
74
75
        $this->updateUserThirdPartyAccount($event->getUserId(), $event->getThirdPartyAccount());
76
    }
77
78
    /**
79
     * @param ThirdPartyAccountReplacedEvent $event
80
     */
81
    public function handleThirdPartyAccountReplacedEvent(ThirdPartyAccountReplacedEvent $event)
82
    {
83
        $this->logger->info('Third party account replaced event');
84
85
        $this->updateUserThirdPartyAccount($event->getUserId(), $event->getThirdPartyAccount());
86
    }
87
88
    /**
89
     * @param ApplicationUserId $userId
90
     * @param Account           $account
91
     */
92
    private function updateUserThirdPartyAccount(ApplicationUserId $userId, Account $account)
93
    {
94
        $user = $this->finder->find((string) $userId);
95
96
        if (!$user) {
97
            return;
98
        }
99
100
        $user->setThirdPartyAccount($account);
101
102
        $this->finder->save($user);
103
    }
104
}
105