1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation\Identity; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* User Synchronization. |
18
|
|
|
*/ |
19
|
|
|
class UserSync extends Base |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Synchronize user profile. |
23
|
|
|
* |
24
|
|
|
* @param UserProviderInterface $user |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function synchronize(UserProviderInterface $user) |
29
|
|
|
{ |
30
|
|
|
$profile = $this->userModel->getByExternalId($user->getExternalIdColumn(), $user->getExternalId()); |
|
|
|
|
31
|
|
|
$properties = UserProperty::getProperties($user); |
32
|
|
|
|
33
|
|
|
if (!empty($profile)) { |
34
|
|
|
$profile = $this->updateUser($profile, $properties); |
35
|
|
|
} elseif ($user->isUserCreationAllowed()) { |
36
|
|
|
$profile = $this->createUser($user, $properties); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $profile; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Update user profile. |
44
|
|
|
* |
45
|
|
|
* @param array $profile |
46
|
|
|
* @param array $properties |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
private function updateUser(array $profile, array $properties) |
51
|
|
|
{ |
52
|
|
|
$values = UserProperty::filterProperties($profile, $properties); |
53
|
|
|
|
54
|
|
|
if (!empty($values)) { |
55
|
|
|
$values['id'] = $profile['id']; |
56
|
|
|
$result = $this->userModel->update($values); |
|
|
|
|
57
|
|
|
|
58
|
|
|
return $result ? array_merge($profile, $properties) : $profile; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $profile; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create user. |
66
|
|
|
* |
67
|
|
|
* @param UserProviderInterface $user |
68
|
|
|
* @param array $properties |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
private function createUser(UserProviderInterface $user, array $properties) |
73
|
|
|
{ |
74
|
|
|
$userId = $this->userModel->create($properties); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if ($userId === false) { |
77
|
|
|
$this->logger->error('Unable to create user profile: '.$user->getExternalId()); |
|
|
|
|
78
|
|
|
|
79
|
|
|
return []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->userModel->getById($userId); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.