SchulIT /
idp
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace App\Service; |
||||||
| 4 | |||||||
| 5 | use App\Entity\ServiceAttribute; |
||||||
| 6 | use App\Entity\ServiceAttributeUserRoleValue; |
||||||
| 7 | use App\Entity\ServiceAttributeUserTypeValue; |
||||||
| 8 | use App\Entity\ServiceAttributeValue; |
||||||
| 9 | use App\Entity\User; |
||||||
| 10 | use App\Entity\UserRole; |
||||||
| 11 | use App\Entity\UserType; |
||||||
| 12 | use App\Repository\ServiceAttributeRepositoryInterface; |
||||||
| 13 | use App\Repository\ServiceAttributeValueRepositoryInterface; |
||||||
| 14 | use App\Repository\TransactionalRepositoryInterface; |
||||||
| 15 | use App\Traits\ArrayTrait; |
||||||
| 16 | use Closure; |
||||||
| 17 | |||||||
| 18 | /** |
||||||
| 19 | * Helper which persists attributes of a user in the database. |
||||||
| 20 | */ |
||||||
| 21 | class AttributePersister { |
||||||
| 22 | use ArrayTrait; |
||||||
| 23 | |||||||
| 24 | public function __construct(private ServiceAttributeRepositoryInterface $attributeRepository, private ServiceAttributeValueRepositoryInterface $attributeValueRepository) |
||||||
| 25 | { |
||||||
| 26 | } |
||||||
| 27 | |||||||
| 28 | public function persist(array $values, array $currentAttributes, Closure $factory): void { |
||||||
| 29 | /** @var ServiceAttribute[] $attributes */ |
||||||
| 30 | $attributes = $this->makeArrayWithKeys( |
||||||
| 31 | $this->attributeRepository->findAll(), |
||||||
| 32 | fn(ServiceAttribute $attribute) => $attribute->getName() |
||||||
| 33 | ); |
||||||
| 34 | |||||||
| 35 | if($this->attributeValueRepository instanceof TransactionalRepositoryInterface) { |
||||||
| 36 | $this->attributeValueRepository->beginTransaction(); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 37 | } |
||||||
| 38 | |||||||
| 39 | foreach($values as $name => $value) { |
||||||
| 40 | if(!array_key_exists($name, $attributes)) { |
||||||
| 41 | continue; // TODO: maybe throw Exception here?! |
||||||
| 42 | } |
||||||
| 43 | |||||||
| 44 | $currentAttributeValue = null; |
||||||
| 45 | |||||||
| 46 | if (array_key_exists($name, $currentAttributes)) { |
||||||
| 47 | $currentAttributeValue = $currentAttributes[$name]; |
||||||
| 48 | } |
||||||
| 49 | |||||||
| 50 | if($currentAttributeValue !== null && ($value === null || empty($value))) { |
||||||
| 51 | $this->attributeValueRepository->remove($currentAttributeValue); |
||||||
| 52 | } elseif ($value !== null && !empty($value)) { |
||||||
| 53 | if($currentAttributeValue === null) { |
||||||
| 54 | $currentAttributeValue = $factory(); |
||||||
| 55 | $currentAttributeValue->setAttribute($attributes[$name]); |
||||||
| 56 | } |
||||||
| 57 | |||||||
| 58 | $currentAttributeValue->setValue($value); |
||||||
| 59 | $this->attributeValueRepository->persist($currentAttributeValue); |
||||||
| 60 | } |
||||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | if($this->attributeValueRepository instanceof TransactionalRepositoryInterface) { |
||||||
| 64 | $this->attributeValueRepository->commit(); |
||||||
|
0 ignored issues
–
show
The method
commit() does not exist on App\Repository\ServiceAt...alueRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Repository\ServiceAt...alueRepositoryInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 65 | } |
||||||
| 66 | } |
||||||
| 67 | |||||||
| 68 | public function persistUserAttributes(array $values, User $user): void { |
||||||
| 69 | $factory = function() use ($user) { |
||||||
| 70 | $value = (new ServiceAttributeValue()) |
||||||
| 71 | ->setUser($user); |
||||||
| 72 | $user->getAttributes()->add($value); |
||||||
| 73 | |||||||
| 74 | return $value; |
||||||
| 75 | }; |
||||||
| 76 | |||||||
| 77 | /** @var ServiceAttributeValue[] $currentUserAttributes */ |
||||||
| 78 | $currentUserAttributes = $this->makeArrayWithKeys( |
||||||
| 79 | $user->getAttributes()->toArray(), |
||||||
| 80 | fn(ServiceAttributeValue $attributeValue) => $attributeValue->getAttribute()->getName() |
||||||
| 81 | ); |
||||||
| 82 | |||||||
| 83 | $this->persist($values, $currentUserAttributes, $factory); |
||||||
| 84 | } |
||||||
| 85 | |||||||
| 86 | public function persistUserRoleAttributes(array $values, UserRole $userRole): void { |
||||||
| 87 | $factory = function() use ($userRole) { |
||||||
| 88 | $value = (new ServiceAttributeUserRoleValue()) |
||||||
| 89 | ->setUserRole($userRole); |
||||||
| 90 | $userRole->getAttributes()->add($value); |
||||||
| 91 | |||||||
| 92 | return $value; |
||||||
| 93 | }; |
||||||
| 94 | |||||||
| 95 | /** @var ServiceAttributeUserRoleValue[] $currentUserAttributes */ |
||||||
| 96 | $currentUserAttributes = $this->makeArrayWithKeys( |
||||||
| 97 | $userRole->getAttributes()->toArray(), |
||||||
| 98 | fn(ServiceAttributeUserRoleValue $attributeValue) => $attributeValue->getAttribute()->getName() |
||||||
| 99 | ); |
||||||
| 100 | |||||||
| 101 | $this->persist($values, $currentUserAttributes, $factory); |
||||||
| 102 | } |
||||||
| 103 | |||||||
| 104 | public function persistUserTypeAttributes(array $values, UserType $userType): void { |
||||||
| 105 | $factory = function() use ($userType) { |
||||||
| 106 | $value = (new ServiceAttributeUserTypeValue()) |
||||||
| 107 | ->setUserType($userType); |
||||||
| 108 | $userType->getAttributes()->add($value); |
||||||
| 109 | |||||||
| 110 | return $value; |
||||||
| 111 | }; |
||||||
| 112 | |||||||
| 113 | /** @var ServiceAttributeUserRoleValue[] $currentUserAttributes */ |
||||||
| 114 | $currentUserAttributes = $this->makeArrayWithKeys( |
||||||
| 115 | $userType->getAttributes()->toArray(), |
||||||
| 116 | fn(ServiceAttributeUserTypeValue $attributeValue) => $attributeValue->getAttribute()->getName() |
||||||
| 117 | ); |
||||||
| 118 | |||||||
| 119 | $this->persist($values, $currentUserAttributes, $factory); |
||||||
| 120 | } |
||||||
| 121 | } |