forked from kiloutyg/docauposte2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityHeritanceService.php
executable file
·198 lines (159 loc) · 7.24 KB
/
EntityHeritanceService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace App\Service;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use App\Repository\ZoneRepository;
use App\Repository\ProductLineRepository;
use App\Repository\CategoryRepository;
use App\Repository\ButtonRepository;
use App\Repository\UploadRepository;
use App\Repository\IncidentRepository;
use App\Repository\IncidentCategoryRepository;
use App\Repository\OldUploadRepository;
use App\Service\UploadService;
use App\Service\IncidentService;
use App\Service\CacheService;
// This class is responsible for the logic of getting the related entities of a given entity
class EntityHeritanceService extends AbstractController
{
private $em;
private $logger;
private $zoneRepository;
private $productLineRepository;
private $categoryRepository;
private $buttonRepository;
private $uploadRepository;
private $incidentRepository;
private $incidentCategoryRepository;
private $oldUploadRepository;
private $uploadService;
private $incidentService;
private $cacheService;
public function __construct(
EntityManagerInterface $em,
LoggerInterface $logger,
ZoneRepository $zoneRepository,
ProductLineRepository $productLineRepository,
CategoryRepository $categoryRepository,
ButtonRepository $buttonRepository,
UploadRepository $uploadRepository,
IncidentRepository $incidentRepository,
IncidentCategoryRepository $incidentCategoryRepository,
OldUploadRepository $oldUploadRepository,
UploadService $uploadService,
IncidentService $incidentService,
CacheService $cacheService
) {
$this->em = $em;
$this->logger = $logger;
$this->zoneRepository = $zoneRepository;
$this->productLineRepository = $productLineRepository;
$this->categoryRepository = $categoryRepository;
$this->buttonRepository = $buttonRepository;
$this->uploadRepository = $uploadRepository;
$this->incidentRepository = $incidentRepository;
$this->incidentCategoryRepository = $incidentCategoryRepository;
$this->oldUploadRepository = $oldUploadRepository;
$this->uploadService = $uploadService;
$this->incidentService = $incidentService;
$this->cacheService = $cacheService;
}
// This function returns an array of all the related uploads entities of a given entity
public function uploadsByParentEntity($parentEntityType, $parentId)
{
$uploads = [];
$entity = $this->cacheService->getEntityById($parentEntityType, $parentId);
if (!$entity) {
return [];
}
// Depending on the entity type, get the related entities
if ($parentEntityType === 'zone') {
$productLines = $this->cacheService->getEntitiesByParentId('productLine', $parentId);
foreach ($productLines as $productLine) {
$uploads = array_merge($uploads, $this->uploadsByParentEntity('productLine', $productLine->getId()));
}
} elseif ($parentEntityType === 'productLine') {
$categories = $this->cacheService->getEntitiesByParentId('category', $parentId);
foreach ($categories as $category) {
$uploads = array_merge($uploads, $this->uploadsByParentEntity('category', $category->getId()));
}
} elseif ($parentEntityType === 'category') {
$buttons = $this->cacheService->getEntitiesByParentId('button', $parentId);
foreach ($buttons as $button) {
$uploads = array_merge($uploads, $this->uploadsByParentEntity('button', $button->getId()));
}
} elseif ($parentEntityType === 'button') {
$uploadsArray = $this->cacheService->getEntitiesByParentId('upload', $parentId);
$uploadsArray = $uploadsArray->toArray();
foreach ($uploadsArray as $upload) {
$uploads[] = $upload;
}
}
return $uploads;
}
// This function returns an array of all the related incidents entities of a given entity
public function incidentsByParentEntity($parentEntityType, $parentId)
{
$incidents = [];
$entity = $this->cacheService->getEntityById($parentEntityType, $parentId);
if (!$entity) {
return [];
}
// Depending on the entity type, get the related entities
if ($parentEntityType === 'zone') {
$productLines = $this->cacheService->getEntitiesByParentId('productLine', $parentId);
foreach ($productLines as $productLine) {
$incidents = array_merge($incidents, $this->incidentsByParentEntity('productLine', $productLine->getId()));
}
} elseif ($parentEntityType === 'productLine') {
$incidentsArray = $this->cacheService->getEntitiesByParentId('incident', $parentId);
$incidentsArray = $incidentsArray->toArray();
foreach ($incidentsArray as $incident) {
$incidents[] = $incident;
}
} elseif ($parentEntityType === 'category') {
if ($productLineId = $entity->getProductLine()->getId()) {
$incidents = array_merge($incidents, $this->incidentsByParentEntity('productLine', $productLineId));
}
}
return $incidents;
}
// This function returns an array of all the related uploads entities of a given entity
public function validatedUploadsByParentEntity($parentEntityType, $parentId)
{
$uploads = [];
$entity = $this->cacheService->getEntityById($parentEntityType, $parentId);
if (!$entity) {
return [];
}
// Depending on the entity type, get the related entities
if ($parentEntityType === 'zone') {
$productLines = $this->cacheService->getEntitiesByParentId('productLine', $parentId);
foreach ($productLines as $productLine) {
$uploads = array_merge($uploads, $this->validatedUploadsByParentEntity('productLine', $productLine->getId()));
}
} elseif ($parentEntityType === 'productLine') {
$categories = $this->cacheService->getEntitiesByParentId('category', $parentId);
foreach ($categories as $category) {
$uploads = array_merge($uploads, $this->validatedUploadsByParentEntity('category', $category->getId()));
}
} elseif ($parentEntityType === 'category') {
$buttons = $this->cacheService->getEntitiesByParentId('button', $parentId);
foreach ($buttons as $button) {
$uploads = array_merge($uploads, $this->validatedUploadsByParentEntity('button', $button->getId()));
}
} elseif ($parentEntityType === 'button') {
$uploadsArray = $this->cacheService->getEntitiesByParentId('upload', $parentId);
$uploadsArray = $uploadsArray->toArray();
foreach ($uploadsArray as $upload) {
if ($upload->getValidation() != null) {
$uploads[] = $upload;
}
}
}
return $uploads;
}
}