handleThirdPartyAccountLinkedEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 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\Store\ApplicationUserStore;
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 ApplicationUserStore
27
     */
28
    private $store;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param ApplicationUserFactory $appUserFactory
34
     * @param ApplicationUserStore   $finder
35
     */
36 18
    public function __construct(
37
        ApplicationUserFactory $appUserFactory,
38
        ApplicationUserStore $finder
39
    ) {
40 18
        $this->appUserFactory = $appUserFactory;
41 18
        $this->store = $finder;
42 18
        $this->logger = new NullLogger();
43 18
    }
44
45
    /**
46
     * @param UserCreatedEvent $event
47
     */
48 6
    public function handleUserCreatedEvent(UserCreatedEvent $event)
49
    {
50 6
        $this->logger->info('Message read model received user event');
51
52 6
        $user = $this->store->find((string) $event->getUserId());
53
54 6
        if ($user) {
55 3
            $user->setName($event->getUsername());
56 3
            $user->setPreferredLanguage($event->getPreferredLanguage());
57 2
        } else {
58 3
            $user = $this->appUserFactory->create(
59 3
                $event->getUserId(),
60 3
                $event->getUsername(),
61 3
                $event->getPreferredLanguage()
62 2
            );
63
        }
64
65 6
        $this->store->save($user);
66 6
    }
67
68
    /**
69
     * @param ThirdPartyAccountLinkedEvent $event
70
     */
71 6
    public function handleThirdPartyAccountLinkedEvent(ThirdPartyAccountLinkedEvent $event)
72
    {
73 6
        $this->logger->info('Third party account linked event');
74
75 6
        $this->updateUserThirdPartyAccount($event->getUserId(), $event->getThirdPartyAccount());
76 6
    }
77
78
    /**
79
     * @param ThirdPartyAccountReplacedEvent $event
80
     */
81 6
    public function handleThirdPartyAccountReplacedEvent(ThirdPartyAccountReplacedEvent $event)
82
    {
83 6
        $this->logger->info('Third party account replaced event');
84
85 6
        $this->updateUserThirdPartyAccount($event->getUserId(), $event->getThirdPartyAccount());
86 6
    }
87
88
    /**
89
     * @param ApplicationUserId $userId
90
     * @param Account           $account
91
     */
92 12
    private function updateUserThirdPartyAccount(ApplicationUserId $userId, Account $account)
93
    {
94 12
        $user = $this->store->find((string) $userId);
95
96 12
        if (!$user) {
97 6
            return;
98
        }
99
100 6
        $user->setThirdPartyAccount($account);
101
102 6
        $this->store->save($user);
103 6
    }
104
}
105