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\Client\CompanyRole\Permission; |
||
9 | |||
10 | use ArrayObject; |
||
11 | use Generated\Shared\Transfer\CompanyRoleTransfer; |
||
12 | use Generated\Shared\Transfer\PermissionCollectionTransfer; |
||
13 | use Generated\Shared\Transfer\PermissionTransfer; |
||
14 | use Spryker\Client\CompanyRole\Dependency\Client\CompanyRoleToPermissionClientInterface; |
||
15 | use Spryker\Client\CompanyRole\Zed\CompanyRoleStubInterface; |
||
16 | |||
17 | class CompanyRolePermissionsHandler implements CompanyRolePermissionsHandlerInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected const PERMISSION_KEY_GLOSSARY_PREFIX = 'permission.name.'; |
||
23 | |||
24 | /** |
||
25 | * @var \Spryker\Client\CompanyRole\Dependency\Client\CompanyRoleToPermissionClientInterface |
||
26 | */ |
||
27 | protected $permissionClient; |
||
28 | |||
29 | /** |
||
30 | * @var \Spryker\Client\CompanyRole\Zed\CompanyRoleStubInterface |
||
31 | */ |
||
32 | protected $companyRoleStub; |
||
33 | |||
34 | /** |
||
35 | * @param \Spryker\Client\CompanyRole\Dependency\Client\CompanyRoleToPermissionClientInterface $permissionClient |
||
36 | * @param \Spryker\Client\CompanyRole\Zed\CompanyRoleStubInterface $companyRoleStub |
||
37 | */ |
||
38 | public function __construct( |
||
39 | CompanyRoleToPermissionClientInterface $permissionClient, |
||
40 | CompanyRoleStubInterface $companyRoleStub |
||
41 | ) { |
||
42 | $this->permissionClient = $permissionClient; |
||
43 | $this->companyRoleStub = $companyRoleStub; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param \Generated\Shared\Transfer\CompanyRoleTransfer $companyRoleTransfer |
||
48 | * |
||
49 | * @return \Generated\Shared\Transfer\PermissionCollectionTransfer |
||
50 | */ |
||
51 | public function findNonInfrastructuralCompanyRolePermissionsByIdCompanyRole( |
||
52 | CompanyRoleTransfer $companyRoleTransfer |
||
53 | ): PermissionCollectionTransfer { |
||
54 | $availablePermissions = $this->permissionClient->findMergedRegisteredNonInfrastructuralPermissions()->getPermissions(); |
||
55 | $companyRolePermissions = $this->companyRoleStub->findCompanyRolePermissions($companyRoleTransfer) |
||
56 | ->getPermissions(); |
||
57 | |||
58 | $availableCompanyRolePermissions = $this->getAvailableCompanyRolePermissions( |
||
59 | $availablePermissions, |
||
60 | $companyRolePermissions, |
||
61 | $companyRoleTransfer, |
||
62 | ); |
||
63 | |||
64 | $permissions = new ArrayObject(); |
||
65 | foreach ($availableCompanyRolePermissions as $permissionTransfer) { |
||
66 | $permissionData = $this->transformPermissionTransferToArray( |
||
67 | $availableCompanyRolePermissions[$permissionTransfer->getKey()], |
||
68 | ); |
||
69 | |||
70 | $permissions->append($permissionData); |
||
71 | } |
||
72 | |||
73 | return (new PermissionCollectionTransfer()) |
||
74 | ->setPermissions($permissions); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param \Generated\Shared\Transfer\PermissionTransfer $permissionTransfer |
||
79 | * |
||
80 | * @return array |
||
81 | */ |
||
82 | protected function transformPermissionTransferToArray( |
||
83 | PermissionTransfer $permissionTransfer |
||
84 | ): array { |
||
85 | $permissionData = $permissionTransfer->toArray(false, true); |
||
86 | |||
87 | $permissionGlossaryKeyName = static::PERMISSION_KEY_GLOSSARY_PREFIX . $permissionData[PermissionTransfer::KEY]; |
||
88 | $permissionData[PermissionTransfer::KEY] = $permissionGlossaryKeyName; |
||
89 | |||
90 | $idCompanyRole = $permissionTransfer->getIdCompanyRole(); |
||
91 | if ($permissionTransfer->getIdCompanyRole()) { |
||
92 | $permissionData[CompanyRoleTransfer::ID_COMPANY_ROLE] = $idCompanyRole; |
||
93 | } |
||
94 | |||
95 | return $permissionData; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param \ArrayObject<int, \Generated\Shared\Transfer\PermissionTransfer> $availablePermissions |
||
100 | * @param \ArrayObject<int, \Generated\Shared\Transfer\PermissionTransfer> $companyRolePermissions |
||
101 | * @param \Generated\Shared\Transfer\CompanyRoleTransfer $companyRoleTransfer |
||
102 | * |
||
103 | * @return array<\Generated\Shared\Transfer\PermissionTransfer> Keys are permission keys |
||
104 | */ |
||
105 | protected function getAvailableCompanyRolePermissions( |
||
106 | ArrayObject $availablePermissions, |
||
107 | ArrayObject $companyRolePermissions, |
||
108 | CompanyRoleTransfer $companyRoleTransfer |
||
109 | ): array { |
||
110 | $availableCompanyRolePermissions = []; |
||
111 | |||
112 | $companyRoleTransfer->requireIdCompanyRole(); |
||
113 | |||
114 | foreach ($availablePermissions as $availablePermission) { |
||
115 | $assignedCompanyRolePermission = $this->findAssignedCompanyRolePermission( |
||
0 ignored issues
–
show
|
|||
116 | $availablePermission, |
||
117 | $companyRolePermissions, |
||
118 | ); |
||
119 | |||
120 | if ($assignedCompanyRolePermission) { |
||
121 | $availablePermission->setIdCompanyRole($companyRoleTransfer->getIdCompanyRole()); |
||
122 | } |
||
123 | |||
124 | $availableCompanyRolePermissions[$availablePermission->getKey()] = $availablePermission; |
||
125 | } |
||
126 | |||
127 | return $availableCompanyRolePermissions; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param \Generated\Shared\Transfer\PermissionTransfer $availablePermission |
||
132 | * @param \ArrayObject<int, \Generated\Shared\Transfer\PermissionTransfer> $companyRolePermissions |
||
133 | * |
||
134 | * @return \Generated\Shared\Transfer\PermissionTransfer|null |
||
135 | */ |
||
136 | protected function findAssignedCompanyRolePermission( |
||
137 | PermissionTransfer $availablePermission, |
||
138 | ArrayObject $companyRolePermissions |
||
139 | ): ?PermissionTransfer { |
||
140 | foreach ($companyRolePermissions as $companyRolePermission) { |
||
141 | if ($companyRolePermission->getKey() === $availablePermission->getKey()) { |
||
142 | return $companyRolePermission; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | return null; |
||
147 | } |
||
148 | } |
||
149 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.