1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Veslo project <https://github.com/symfony-doge/veslo>. |
5
|
|
|
* |
6
|
|
|
* (C) 2019 Pavel Petrov <[email protected]>. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 GPL-3.0 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Veslo\SanityBundle\Vacancy\Tag\Group; |
17
|
|
|
|
18
|
|
|
use Veslo\AppBundle\Entity\Repository\BaseEntityRepository; |
19
|
|
|
use Veslo\SanityBundle\Dto\Vacancy\Tag\GroupDto; |
20
|
|
|
use Veslo\SanityBundle\Entity\Vacancy\Tag\Group; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Synchronizes sanity tags group data with external source |
24
|
|
|
*/ |
25
|
|
|
class Synchronizer |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Provides sanity tag groups which will be used by vacancy analysers |
29
|
|
|
* |
30
|
|
|
* @var ProviderInterface |
31
|
|
|
*/ |
32
|
|
|
private $groupProvider; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates and persists sanity tag groups in the local storage |
36
|
|
|
* |
37
|
|
|
* @var CreatorInterface |
38
|
|
|
*/ |
39
|
|
|
private $groupCreator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Repository where sanity tag groups are stored |
43
|
|
|
* |
44
|
|
|
* @var BaseEntityRepository |
45
|
|
|
*/ |
46
|
|
|
private $groupRepository; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Synchronizer constructor. |
50
|
|
|
* |
51
|
|
|
* @param ProviderInterface $groupProvider Provides sanity tag groups which will be used by vacancy analysers |
52
|
|
|
* @param CreatorInterface $groupCreator Creates and persists sanity tag groups in the local storage |
53
|
|
|
* @param BaseEntityRepository $groupRepository Repository where sanity tag groups are stored |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
ProviderInterface $groupProvider, |
57
|
|
|
CreatorInterface $groupCreator, |
58
|
|
|
BaseEntityRepository $groupRepository |
59
|
|
|
) { |
60
|
|
|
$this->groupProvider = $groupProvider; |
61
|
|
|
$this->groupCreator = $groupCreator; |
62
|
|
|
$this->groupRepository = $groupRepository; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Performs sanity tag groups sync and returns a set which are not exists yet in the local storage (diff set) |
67
|
|
|
* |
68
|
|
|
* @param bool $isPersistAndFlush Whenever groups which are not exists in the local storage should be saved |
69
|
|
|
* |
70
|
|
|
* @return iterable|GroupDto[] |
71
|
|
|
*/ |
72
|
|
|
public function synchronize(bool $isPersistAndFlush = true): iterable |
73
|
|
|
{ |
74
|
|
|
$groupsDataSynced = $this->groupProvider->getTagGroups(); // iterable |
75
|
|
|
|
76
|
|
|
/** @var Group[] $existentGroups */ |
77
|
|
|
$existentGroups = $this->groupRepository->findAll(); |
78
|
|
|
|
79
|
|
|
$diffSet = $this->calculateDiff($groupsDataSynced, $existentGroups); |
80
|
|
|
|
81
|
|
|
if ($isPersistAndFlush) { |
82
|
|
|
$this->persistAndFlush($diffSet); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $diffSet; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Calculates a diff set by specified synced groups data and existing ones |
90
|
|
|
* |
91
|
|
|
* @param iterable|GroupDto[] $groupsDataSynced Groups from third-party provider |
92
|
|
|
* @param Group[] $existentGroups Existing group entities in the local storage |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
private function calculateDiff(iterable $groupsDataSynced, array $existentGroups): array |
97
|
|
|
{ |
98
|
|
|
$syncedMap = []; |
99
|
|
|
foreach ($groupsDataSynced as $groupDataSynced) { |
100
|
|
|
$syncedGroupName = $groupDataSynced->getName(); |
101
|
|
|
$syncedMap[$syncedGroupName] = $groupDataSynced; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$existsMap = []; |
105
|
|
|
foreach ($existentGroups as $group) { |
106
|
|
|
$groupName = $group->getName(); |
107
|
|
|
$existsMap[$groupName] = $group; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$diffSet = []; |
111
|
|
|
foreach ($syncedMap as $groupName => $groupData) { |
112
|
|
|
if (!array_key_exists($groupName, $existsMap)) { |
113
|
|
|
$diffSet[] = $groupData; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $diffSet; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Calls the group creator to save groups from a diff set into the local storage |
122
|
|
|
* |
123
|
|
|
* @param GroupDto[] $diffSet Groups which are not exists in the local storage |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
private function persistAndFlush(array $diffSet): void |
128
|
|
|
{ |
129
|
|
|
foreach ($diffSet as $groupData) { |
130
|
|
|
$this->groupCreator->createByDto($groupData); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|