Completed
Push — master ( 130e15...4fdaf0 )
by Rémi
02:49
created

updateUserThirdPartyAccount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 6
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 applyUserCreatedEvent(UserCreatedEvent $event)
49
    {
50
        $this->logger->info('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 applyThirdPartyAccountLinkedEvent(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 applyThirdPartyAccountReplacedEvent(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