1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slides\Connector\Auth\Services\Assessment; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Slides\Connector\Auth\Repositories\UserRepository; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AssessmentService |
10
|
|
|
* |
11
|
|
|
* @package Slides\Connector\Auth\Services\Assessment |
12
|
|
|
*/ |
13
|
|
|
class AssessmentService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var UserRepository |
17
|
|
|
*/ |
18
|
|
|
protected $users; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AssessmentService constructor. |
22
|
|
|
* |
23
|
|
|
* @param UserRepository $users |
24
|
|
|
*/ |
25
|
|
|
public function __construct(UserRepository $users) |
26
|
|
|
{ |
27
|
|
|
$this->users = $users; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Differentiate users with the passed ones. |
32
|
|
|
* |
33
|
|
|
* @param array $keys The remote user keys. |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function differentiateUsers(array $keys): array |
38
|
|
|
{ |
39
|
|
|
$localKeys = $this->users->findRemoteIds(); |
40
|
|
|
|
41
|
|
|
return [ |
42
|
|
|
'uniqueTenantUsers' => $this->retrieveUniqueLocals($keys, $localKeys), |
43
|
|
|
'uniqueServiceUserKeys' => $this->retrieveUniqueRemotes($keys, $localKeys) |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retrieve remote users that not exist locally. |
49
|
|
|
* |
50
|
|
|
* @param array $remoteKeys |
51
|
|
|
* @param Collection $localKeys |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
protected function retrieveUniqueRemotes(array $remoteKeys, Collection $localKeys): array |
56
|
|
|
{ |
57
|
|
|
return array_diff($remoteKeys, $localKeys->toArray()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Retrieve local users that not exist remotely. |
62
|
|
|
* |
63
|
|
|
* @param array $remoteKeys |
64
|
|
|
* @param Collection $localKeys |
65
|
|
|
* |
66
|
|
|
* @return Collection |
67
|
|
|
*/ |
68
|
|
|
protected function retrieveUniqueLocals(array $remoteKeys, Collection $localKeys): Collection |
69
|
|
|
{ |
70
|
|
|
$uniqueKeys = $localKeys->diff($remoteKeys); |
71
|
|
|
|
72
|
|
|
return $this->users->many($uniqueKeys->keys()->all()); |
73
|
|
|
} |
74
|
|
|
} |