1 | <?php |
||||
2 | |||||
3 | namespace Slides\Connector\Auth\Sync; |
||||
4 | |||||
5 | use Illuminate\Support\Str; |
||||
6 | use Slides\Connector\Auth\Sync\Syncable as LocalUser; |
||||
7 | use Slides\Connector\Auth\Sync\User as RemoteUser; |
||||
8 | use Illuminate\Support\Facades\Auth; |
||||
9 | use Slides\Connector\Auth\AuthService; |
||||
10 | |||||
11 | /** |
||||
12 | * Trait HandlesActions |
||||
13 | * |
||||
14 | * @property AuthService $authService |
||||
15 | * |
||||
16 | * @package Slides\Connector\Auth\Sync |
||||
17 | */ |
||||
18 | trait HandlesActions |
||||
19 | { |
||||
20 | /** |
||||
21 | * Handle a user action. |
||||
22 | * |
||||
23 | * @param RemoteUser $remote |
||||
24 | * @param string $action |
||||
25 | * |
||||
26 | * @return void |
||||
27 | */ |
||||
28 | protected function handleAction(RemoteUser $remote, string $action) |
||||
29 | { |
||||
30 | $handler = 'action' . Str::studly($action); |
||||
31 | |||||
32 | if(!method_exists($this, $handler)) { |
||||
33 | throw new \Slides\Connector\Auth\Exceptions\SyncException("User action handler {$handler} cannot be found."); |
||||
34 | } |
||||
35 | |||||
36 | $this->{$handler}($remote); |
||||
37 | } |
||||
38 | |||||
39 | /** |
||||
40 | * Handle the "create" action. |
||||
41 | * |
||||
42 | * @param RemoteUser $remote |
||||
43 | * |
||||
44 | * @return void |
||||
45 | * |
||||
46 | * @throws |
||||
47 | */ |
||||
48 | protected function actionCreate(RemoteUser $remote) |
||||
49 | { |
||||
50 | // If a user with the same email was found, we need to skip the process |
||||
51 | if(Auth::getProvider()->retrieveByCredentials(['email' => strtolower($remote->getEmail())])) { |
||||
52 | return; |
||||
53 | } |
||||
54 | |||||
55 | $this->authService->handle(AuthService::HANDLER_USER_SYNC_CREATE, ['remote' => $remote]); |
||||
56 | |||||
57 | $this->incrementStats('created'); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
58 | } |
||||
59 | |||||
60 | /** |
||||
61 | * Handle the "update" action. |
||||
62 | * |
||||
63 | * @param RemoteUser $remote |
||||
64 | * |
||||
65 | * @return void |
||||
66 | * |
||||
67 | * @throws |
||||
68 | */ |
||||
69 | protected function actionUpdate(RemoteUser $remote) |
||||
70 | { |
||||
71 | // If a user with the same email cannot be found, we should skip the process |
||||
72 | if(!$local = Auth::getProvider()->retrieveByCredentials(['email' => $remote->getEmail()])) { |
||||
73 | return; |
||||
74 | } |
||||
75 | |||||
76 | // If a local user was updated later than remote one, we should skip the process |
||||
77 | // Since we have a latest one |
||||
78 | if($this->localNewerThanRemote($local, $remote)) { |
||||
79 | return; |
||||
80 | } |
||||
81 | |||||
82 | // If "password" mode is not enabled, we cannot update local passwords, so we simply resetting it |
||||
83 | if(!$this->hasMode(static::MODE_PASSWORDS)) { |
||||
0 ignored issues
–
show
It seems like
hasMode() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
84 | $remote->resetPassword(); |
||||
85 | } |
||||
86 | |||||
87 | $this->authService->handle(AuthService::HANDLER_USER_SYNC_UPDATE, [ |
||||
88 | 'remote' => $remote, |
||||
89 | 'local' => $local |
||||
90 | ]); |
||||
91 | |||||
92 | $this->incrementStats('updated'); |
||||
93 | } |
||||
94 | |||||
95 | /** |
||||
96 | * Handle the "delete" action. |
||||
97 | * |
||||
98 | * @param RemoteUser $remote |
||||
99 | * |
||||
100 | * @return void |
||||
101 | * |
||||
102 | * @throws |
||||
103 | */ |
||||
104 | protected function actionDelete(RemoteUser $remote) |
||||
105 | { |
||||
106 | if(!$local = Auth::getProvider()->retrieveByCredentials(['email' => $remote->getEmail()])) { |
||||
107 | return; |
||||
108 | } |
||||
109 | |||||
110 | $this->authService->handle(AuthService::HANDLER_USER_SYNC_DELETE, ['remote' => $remote, 'local' => $local]); |
||||
111 | |||||
112 | $this->incrementStats('deleted'); |
||||
113 | } |
||||
114 | |||||
115 | /** |
||||
116 | * Check whether remote user has updated earlier than local one. |
||||
117 | * |
||||
118 | * @param Syncable|\Illuminate\Contracts\Auth\Authenticatable $local |
||||
119 | * @param User $remote |
||||
120 | * |
||||
121 | * @return bool |
||||
122 | */ |
||||
123 | private function localNewerThanRemote(LocalUser $local, RemoteUser $remote) |
||||
124 | { |
||||
125 | if(!$local->retrieveRemoteId()) { |
||||
126 | return false; |
||||
127 | } |
||||
128 | |||||
129 | if(!$remoteUpdated = $remote->getUpdated()) { |
||||
130 | return false; |
||||
131 | } |
||||
132 | |||||
133 | if(!$localUpdate = $local->retrieveUpdatedAt()) { |
||||
134 | return false; |
||||
135 | } |
||||
136 | |||||
137 | return $remoteUpdated->lessThan($localUpdate); |
||||
138 | } |
||||
139 | } |