Completed
Push — master ( ade58c...3e282b )
by Rémi
03:35
created

SourcedUser::isThirdPartyAccountAlreadyLinked()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace MessageApp\User\Entity;
4
5
use Broadway\EventSourcing\EventSourcedAggregateRoot;
6
use MessageApp\Event\ThirdPartyAccountLinkedEvent;
7
use MessageApp\Event\ThirdPartyAccountReplacedEvent;
8
use MessageApp\Event\UserCreatedEvent;
9
use MessageApp\User\ApplicationUserId;
10
use MessageApp\User\ThirdParty\Account as ThirdPartyAccount;
11
use MessageApp\User\ThirdParty\Source;
12
13
class SourcedUser extends EventSourcedAggregateRoot
14
{
15
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
17
    /////////////////////////////////////////////   PROPERTIES   ///////////////////////////////////////////////////
18
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
19
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
21
    /**
22
     * @var ApplicationUserId
23
     */
24
    private $id;
25
26
    /**
27
     * @var string
28
     */
29
    private $name;
30
31
    /**
32
     * @var string
33
     */
34
    private $preferredLanguage;
35
36
    /**
37
     * @var ThirdPartyAccount[]
38
     */
39
    private $thirdPartyAccounts;
40
41
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
42
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
43
    /////////////////////////////////////////   PRIVATE CONSTRUCTOR   //////////////////////////////////////////////
44
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
45
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
46
47
    /**
48
     * Constructor
49
     */
50 12
    private function __construct()
51
    {
52 12
        $this->thirdPartyAccounts = [];
53 12
    }
54
55
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
56
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
57
    ////////////////////////////////////////////   DOMAIN METHODS   ////////////////////////////////////////////////
58
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
59
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
60
61
    /**
62
     * @param ThirdPartyAccount $thirdPartyUser
63
     */
64 6
    public function linkToThirdPartyAccount(ThirdPartyAccount $thirdPartyUser)
65
    {
66 6
        if ($this->isThirdPartyAccountAlreadyLinked($thirdPartyUser->getSource())) {
67 3
            $this->apply(new ThirdPartyAccountReplacedEvent($this->id, $thirdPartyUser));
68 2
        }
69
70 6
        $this->apply(new ThirdPartyAccountLinkedEvent($this->id, $thirdPartyUser));
71 6
    }
72
73
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
74
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
75
    //////////////////////////////////////////////   ACCESSORS   ///////////////////////////////////////////////////
76
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
78
79
    /**
80
     * Returns the user id
81
     *
82
     * @return ApplicationUserId
83
     */
84 3
    public function getId()
85
    {
86 3
        return $this->id;
87
    }
88
89
    /**
90
     * @return string
91
     */
92 9
    public function getAggregateRootId()
93
    {
94 9
        return $this->id;
95
    }
96
97
    /**
98
     * Returns the user name
99
     *
100
     * @return string
101
     */
102 3
    public function getName()
103
    {
104 3
        return $this->name;
105
    }
106
107
    /**
108
     * Returns the preferred language
109
     *
110
     * @return string
111
     */
112 3
    public function getPreferredLanguage()
113
    {
114 3
        return $this->preferredLanguage;
115
    }
116
117
    /**
118
     * @return ThirdPartyAccount[]
119
     */
120 6
    public function getThirdPartyAccounts()
121
    {
122 6
        return array_values($this->thirdPartyAccounts);
123
    }
124
125
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
126
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127
    //////////////////////////////////////////   PRIVATE METHODS   /////////////////////////////////////////////////
128
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
130
131
    /**
132
     * Initialize the game
133
     *
134
     * @param ApplicationUserId $id
135
     * @param string            $name
136
     * @param string            $language
137
     */
138 9
    private function initialize(ApplicationUserId $id, $name, $language)
139
    {
140 9
        $this->apply(new UserCreatedEvent($id, $name, $language));
141 9
    }
142
143
    /**
144
     * @param Source $source
145
     *
146
     * @return bool
147
     */
148 6
    private function isThirdPartyAccountAlreadyLinked(Source $source)
149
    {
150 6
        return array_key_exists((string) $source, $this->thirdPartyAccounts);
151
    }
152
153
    /**
154
     * @param ThirdPartyAccount $account
155
     */
156 6
    private function setThirdPartyUser(ThirdPartyAccount $account)
157
    {
158 6
        $this->thirdPartyAccounts[(string) $account->getSource()] = $account;
159 6
    }
160
161
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
163
    ////////////////////////////////////////////   APPLY EVENTS   //////////////////////////////////////////////////
164
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
165
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
166
167
    /**
168
     * Apply the user created event
169
     *
170
     * @param \MessageApp\Event\UserCreatedEvent $event
171
     */
172 9
    protected function applyUserCreatedEvent(UserCreatedEvent $event)
173
    {
174 9
        $this->id = $event->getUserId();
175 9
        $this->name = $event->getUsername();
176 9
        $this->preferredLanguage = $event->getPreferredLanguage();
177 9
        $this->thirdPartyAccounts = [];
178 9
    }
179
180
    /**
181
     * Apply the third party account linked event
182
     *
183
     * @param ThirdPartyAccountLinkedEvent $event
184
     */
185 6
    protected function applyThirdPartyAccountLinkedEvent(ThirdPartyAccountLinkedEvent $event)
186
    {
187 6
        $this->setThirdPartyUser($event->getThirdPartyAccount());
188 6
    }
189
190
    /**
191
     * Apply the third party account replaced event
192
     *
193
     * @param ThirdPartyAccountReplacedEvent $event
194
     */
195 3
    protected function applyThirdPartyAccountReplacedEvent(ThirdPartyAccountReplacedEvent $event)
196
    {
197 3
        $this->setThirdPartyUser($event->getThirdPartyAccount());
198 3
    }
199
200
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
201
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
202
    /////////////////////////////////////////   STATIC CONSTRUCTOR   ///////////////////////////////////////////////
203
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
204
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
205
206
    /**
207
     * Create a new instance
208
     *
209
     * @param  ApplicationUserId $id
210
     * @param  string            $name
211
     * @param  string            $language
212
     *
213
     * @return SourcedUser
214
     */
215 9
    public static function createUser(ApplicationUserId $id, $name, $language)
216
    {
217 9
        $user = new self();
218 9
        $user->initialize(
219 6
            $id,
220 6
            $name,
221
            $language
222 6
        );
223
224 9
        return $user;
225
    }
226
227
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
228
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
229
    ///////////////////////////////////////////   RECONSTITUTION   /////////////////////////////////////////////////
230
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
231
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
232
233
    /**
234
     * Static construction method for reconstitution
235
     *
236
     * @return SourcedUser
237
     */
238 3
    public static function instantiateForReconstitution()
239
    {
240 3
        return new self();
241
    }
242
}
243