1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Zed\CompanyRole\Business\Model; |
||
9 | |||
10 | use Generated\Shared\Transfer\CompanyResponseTransfer; |
||
11 | use Generated\Shared\Transfer\CompanyRoleCriteriaFilterTransfer; |
||
12 | use Generated\Shared\Transfer\CompanyRoleResponseTransfer; |
||
13 | use Generated\Shared\Transfer\CompanyRoleTransfer; |
||
14 | use Generated\Shared\Transfer\CompanyTransfer; |
||
15 | use Generated\Shared\Transfer\CompanyUserTransfer; |
||
16 | use Generated\Shared\Transfer\PermissionCollectionTransfer; |
||
17 | use Generated\Shared\Transfer\ResponseMessageTransfer; |
||
18 | use Spryker\Zed\CompanyRole\CompanyRoleConfig; |
||
19 | use Spryker\Zed\CompanyRole\Dependency\Facade\CompanyRoleToPermissionFacadeInterface; |
||
20 | use Spryker\Zed\CompanyRole\Persistence\CompanyRoleEntityManagerInterface; |
||
21 | use Spryker\Zed\CompanyRole\Persistence\CompanyRoleRepositoryInterface; |
||
22 | use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait; |
||
23 | |||
24 | class CompanyRole implements CompanyRoleInterface |
||
25 | { |
||
26 | use TransactionTrait; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected const ERROR_MESSAGE_HAS_RELATED_USERS = 'company.company_role.delete.error.has_users'; |
||
32 | |||
33 | /** |
||
34 | * @var \Spryker\Zed\CompanyRole\Persistence\CompanyRoleRepositoryInterface |
||
35 | */ |
||
36 | protected $repository; |
||
37 | |||
38 | /** |
||
39 | * @var \Spryker\Zed\CompanyRole\Persistence\CompanyRoleEntityManagerInterface |
||
40 | */ |
||
41 | protected $entityManager; |
||
42 | |||
43 | /** |
||
44 | * @var \Spryker\Zed\CompanyRole\Business\Model\CompanyRolePermissionWriterInterface |
||
45 | */ |
||
46 | protected $permissionWriter; |
||
47 | |||
48 | /** |
||
49 | * @var \Spryker\Zed\CompanyRole\CompanyRoleConfig |
||
50 | */ |
||
51 | protected $companyRoleConfig; |
||
52 | |||
53 | /** |
||
54 | * @var \Spryker\Zed\CompanyRole\Dependency\Facade\CompanyRoleToPermissionFacadeInterface |
||
55 | */ |
||
56 | protected $permissionFacade; |
||
57 | |||
58 | /** |
||
59 | * @param \Spryker\Zed\CompanyRole\Persistence\CompanyRoleRepositoryInterface $repository |
||
60 | * @param \Spryker\Zed\CompanyRole\Persistence\CompanyRoleEntityManagerInterface $entityManager |
||
61 | * @param \Spryker\Zed\CompanyRole\Business\Model\CompanyRolePermissionWriterInterface $permissionWriter |
||
62 | * @param \Spryker\Zed\CompanyRole\CompanyRoleConfig $companyRoleConfig |
||
63 | * @param \Spryker\Zed\CompanyRole\Dependency\Facade\CompanyRoleToPermissionFacadeInterface $permissionFacade |
||
64 | */ |
||
65 | public function __construct( |
||
66 | CompanyRoleRepositoryInterface $repository, |
||
67 | CompanyRoleEntityManagerInterface $entityManager, |
||
68 | CompanyRolePermissionWriterInterface $permissionWriter, |
||
69 | CompanyRoleConfig $companyRoleConfig, |
||
70 | CompanyRoleToPermissionFacadeInterface $permissionFacade |
||
71 | ) { |
||
72 | $this->repository = $repository; |
||
73 | $this->entityManager = $entityManager; |
||
74 | $this->permissionWriter = $permissionWriter; |
||
75 | $this->companyRoleConfig = $companyRoleConfig; |
||
76 | $this->permissionFacade = $permissionFacade; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param \Generated\Shared\Transfer\CompanyRoleTransfer $companyRoleTransfer |
||
81 | * |
||
82 | * @return \Generated\Shared\Transfer\CompanyRoleResponseTransfer |
||
83 | */ |
||
84 | public function create(CompanyRoleTransfer $companyRoleTransfer): CompanyRoleResponseTransfer |
||
85 | { |
||
86 | return $this->getTransactionHandler()->handleTransaction(function () use ($companyRoleTransfer) { |
||
87 | return $this->executeCompanyRoleSaveTransaction($companyRoleTransfer); |
||
88 | }); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param \Generated\Shared\Transfer\CompanyResponseTransfer $companyResponseTransfer |
||
93 | * |
||
94 | * @return \Generated\Shared\Transfer\CompanyResponseTransfer |
||
95 | */ |
||
96 | public function createByCompany(CompanyResponseTransfer $companyResponseTransfer): CompanyResponseTransfer |
||
97 | { |
||
98 | $companyRoles = $this->companyRoleConfig->getPredefinedCompanyRoles(); |
||
99 | |||
100 | if ($companyRoles) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
101 | return $this->createCompanyRoles($companyResponseTransfer, $companyRoles); |
||
102 | } |
||
103 | |||
104 | return $companyResponseTransfer; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param \Generated\Shared\Transfer\CompanyResponseTransfer $companyResponseTransfer |
||
109 | * @param array<\Generated\Shared\Transfer\CompanyRoleTransfer> $companyRoles |
||
110 | * |
||
111 | * @return \Generated\Shared\Transfer\CompanyResponseTransfer |
||
112 | */ |
||
113 | protected function createCompanyRoles( |
||
114 | CompanyResponseTransfer $companyResponseTransfer, |
||
115 | array $companyRoles |
||
116 | ): CompanyResponseTransfer { |
||
117 | $companyResponseTransfer->requireCompanyTransfer(); |
||
118 | $companyTransfer = $companyResponseTransfer->getCompanyTransfer(); |
||
119 | |||
120 | $availablePermissions = $this->permissionFacade->findMergedRegisteredNonInfrastructuralPermissions(); |
||
121 | |||
122 | foreach ($companyRoles as $companyRoleTransfer) { |
||
123 | $companyRoleResponseTransfer = $this->createCompanyRoleWithAssignedPermissions( |
||
124 | $companyRoleTransfer, |
||
125 | $companyTransfer, |
||
126 | $availablePermissions, |
||
127 | ); |
||
128 | |||
129 | $companyResponseTransfer = $this->addCompanyRoleMessagesToCompanyResponseTransfer( |
||
130 | $companyRoleResponseTransfer, |
||
131 | $companyResponseTransfer, |
||
132 | ); |
||
133 | } |
||
134 | |||
135 | return $companyResponseTransfer; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param \Generated\Shared\Transfer\CompanyRoleResponseTransfer $companyRoleResponseTransfer |
||
140 | * @param \Generated\Shared\Transfer\CompanyResponseTransfer $companyResponseTransfer |
||
141 | * |
||
142 | * @return \Generated\Shared\Transfer\CompanyResponseTransfer |
||
143 | */ |
||
144 | protected function addCompanyRoleMessagesToCompanyResponseTransfer( |
||
145 | CompanyRoleResponseTransfer $companyRoleResponseTransfer, |
||
146 | CompanyResponseTransfer $companyResponseTransfer |
||
147 | ): CompanyResponseTransfer { |
||
148 | foreach ($companyRoleResponseTransfer->getMessages() as $messageTransfer) { |
||
149 | $companyResponseTransfer->addMessage($messageTransfer); |
||
150 | } |
||
151 | |||
152 | return $companyResponseTransfer; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param \Generated\Shared\Transfer\CompanyRoleTransfer $companyRoleTransfer |
||
157 | * @param \Generated\Shared\Transfer\CompanyTransfer $companyTransfer |
||
158 | * @param \Generated\Shared\Transfer\PermissionCollectionTransfer $availablePermissions |
||
159 | * |
||
160 | * @return \Generated\Shared\Transfer\CompanyRoleResponseTransfer |
||
161 | */ |
||
162 | protected function createCompanyRoleWithAssignedPermissions( |
||
163 | CompanyRoleTransfer $companyRoleTransfer, |
||
164 | CompanyTransfer $companyTransfer, |
||
165 | PermissionCollectionTransfer $availablePermissions |
||
166 | ): CompanyRoleResponseTransfer { |
||
167 | $companyRoleTransfer->setFkCompany($companyTransfer->getIdCompany()); |
||
168 | |||
169 | $preparedPermissionCollection = $this->findAssignedCompanyRolePermissions( |
||
170 | $companyRoleTransfer->getPermissionCollection(), |
||
171 | $availablePermissions, |
||
172 | ); |
||
173 | |||
174 | $companyRoleTransfer->setPermissionCollection($preparedPermissionCollection); |
||
175 | |||
176 | return $this->create($companyRoleTransfer); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param \Generated\Shared\Transfer\PermissionCollectionTransfer $companyRolePermissions |
||
181 | * @param \Generated\Shared\Transfer\PermissionCollectionTransfer $availablePermissions |
||
182 | * |
||
183 | * @return \Generated\Shared\Transfer\PermissionCollectionTransfer |
||
184 | */ |
||
185 | protected function findAssignedCompanyRolePermissions( |
||
186 | PermissionCollectionTransfer $companyRolePermissions, |
||
187 | PermissionCollectionTransfer $availablePermissions |
||
188 | ): PermissionCollectionTransfer { |
||
189 | $availableCompanyRolePermissions = new PermissionCollectionTransfer(); |
||
190 | |||
191 | foreach ($companyRolePermissions->getPermissions() as $assignedCompanyRolePermissionTransfer) { |
||
192 | foreach ($availablePermissions->getPermissions() as $availablePermissionTransfer) { |
||
193 | if ($assignedCompanyRolePermissionTransfer->getKey() === $availablePermissionTransfer->getKey()) { |
||
194 | $availableCompanyRolePermissions->addPermission($availablePermissionTransfer); |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | return $availableCompanyRolePermissions; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param \Generated\Shared\Transfer\CompanyRoleTransfer $companyRoleTransfer |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | public function update(CompanyRoleTransfer $companyRoleTransfer): void |
||
208 | { |
||
209 | $this->getTransactionHandler()->handleTransaction(function () use ($companyRoleTransfer) { |
||
210 | $this->executeCompanyRoleSaveTransaction($companyRoleTransfer); |
||
211 | }); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param \Generated\Shared\Transfer\CompanyRoleTransfer $companyRoleTransfer |
||
216 | * |
||
217 | * @return \Generated\Shared\Transfer\CompanyRoleResponseTransfer |
||
218 | */ |
||
219 | public function delete(CompanyRoleTransfer $companyRoleTransfer): CompanyRoleResponseTransfer |
||
220 | { |
||
221 | $companyRoleResponseTransfer = (new CompanyRoleResponseTransfer()) |
||
222 | ->setCompanyRoleTransfer($companyRoleTransfer) |
||
223 | ->setIsSuccessful(true); |
||
224 | |||
225 | return $this->getTransactionHandler()->handleTransaction(function () use ($companyRoleResponseTransfer) { |
||
226 | return $this->executeDeleteTransaction($companyRoleResponseTransfer); |
||
227 | }); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param \Generated\Shared\Transfer\CompanyUserTransfer $companyUserTransfer |
||
232 | * |
||
233 | * @return void |
||
234 | */ |
||
235 | public function saveCompanyUser(CompanyUserTransfer $companyUserTransfer): void |
||
236 | { |
||
237 | $this->getTransactionHandler()->handleTransaction(function () use ($companyUserTransfer) { |
||
238 | $this->entityManager->saveCompanyUser($companyUserTransfer); |
||
239 | }); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @param \Generated\Shared\Transfer\CompanyUserTransfer $companyUserTransfer |
||
244 | * |
||
245 | * @return \Generated\Shared\Transfer\CompanyUserTransfer |
||
246 | */ |
||
247 | public function hydrateCompanyUser(CompanyUserTransfer $companyUserTransfer): CompanyUserTransfer |
||
248 | { |
||
249 | $companyUserTransfer->requireIdCompanyUser(); |
||
250 | |||
251 | $criteriaFilterTransfer = new CompanyRoleCriteriaFilterTransfer(); |
||
252 | $criteriaFilterTransfer->setIdCompanyUser($companyUserTransfer->getIdCompanyUser()); |
||
253 | $companyRoleCollection = $this->repository->getCompanyRoleCollection($criteriaFilterTransfer); |
||
254 | |||
255 | return $companyUserTransfer->setCompanyRoleCollection($companyRoleCollection); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * @param \Generated\Shared\Transfer\CompanyRoleResponseTransfer $companyRoleResponseTransfer |
||
260 | * |
||
261 | * @return \Generated\Shared\Transfer\CompanyRoleResponseTransfer |
||
262 | */ |
||
263 | protected function executeDeleteTransaction(CompanyRoleResponseTransfer $companyRoleResponseTransfer): CompanyRoleResponseTransfer |
||
264 | { |
||
265 | $companyRoleResponseTransfer |
||
266 | ->getCompanyRoleTransfer() |
||
267 | ->requireIdCompanyRole(); |
||
268 | |||
269 | $companyRoleResponseTransfer = $this->checkOnRelatedUsers($companyRoleResponseTransfer); |
||
270 | |||
271 | if (!$companyRoleResponseTransfer->getIsSuccessful()) { |
||
272 | return $companyRoleResponseTransfer; |
||
273 | } |
||
274 | |||
275 | $this->entityManager->deleteCompanyRoleById( |
||
276 | $companyRoleResponseTransfer |
||
277 | ->getCompanyRoleTransfer() |
||
278 | ->getIdCompanyRole(), |
||
279 | ); |
||
280 | |||
281 | return $companyRoleResponseTransfer; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param \Generated\Shared\Transfer\CompanyRoleResponseTransfer $companyRoleResponseTransfer |
||
286 | * |
||
287 | * @return \Generated\Shared\Transfer\CompanyRoleResponseTransfer |
||
288 | */ |
||
289 | protected function checkOnRelatedUsers(CompanyRoleResponseTransfer $companyRoleResponseTransfer): CompanyRoleResponseTransfer |
||
290 | { |
||
291 | $hasUsers = $this->repository->hasUsers( |
||
292 | $companyRoleResponseTransfer |
||
293 | ->getCompanyRoleTransfer() |
||
294 | ->getIdCompanyRole(), |
||
295 | ); |
||
296 | |||
297 | if ($hasUsers) { |
||
298 | $companyRoleResponseTransfer |
||
299 | ->setIsSuccessful(false) |
||
300 | ->addMessage( |
||
301 | (new ResponseMessageTransfer()) |
||
302 | ->setText(static::ERROR_MESSAGE_HAS_RELATED_USERS), |
||
303 | ); |
||
304 | |||
305 | return $companyRoleResponseTransfer; |
||
306 | } |
||
307 | |||
308 | return $companyRoleResponseTransfer; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @param \Generated\Shared\Transfer\CompanyRoleTransfer $companyRoleTransfer |
||
313 | * |
||
314 | * @return \Generated\Shared\Transfer\CompanyRoleResponseTransfer |
||
315 | */ |
||
316 | protected function executeCompanyRoleSaveTransaction(CompanyRoleTransfer $companyRoleTransfer): CompanyRoleResponseTransfer |
||
317 | { |
||
318 | $permissionCollection = $companyRoleTransfer->getPermissionCollection(); |
||
319 | $companyRoleTransfer = $this->entityManager->saveCompanyRole($companyRoleTransfer); |
||
320 | $companyRoleTransfer->setPermissionCollection($permissionCollection); |
||
321 | |||
322 | $this->permissionWriter->saveCompanyRolePermissions($companyRoleTransfer); |
||
323 | |||
324 | return (new CompanyRoleResponseTransfer()) |
||
325 | ->setIsSuccessful(true) |
||
326 | ->setCompanyRoleTransfer($companyRoleTransfer); |
||
327 | } |
||
328 | } |
||
329 |