EntityUtils.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Utils;
  4. use App\Attribute\BillingSettingDefaultValue;
  5. use App\Attribute\OrganizationDefaultValue;
  6. use App\Entity\Access\Access;
  7. /**
  8. * Class EntityUtils : Gestion des valeurs par défauts devant être présentes dans les entités.
  9. */
  10. class EntityUtils
  11. {
  12. /**
  13. * @throws \ReflectionException
  14. */
  15. public function defaultValueSettersByAccess(mixed $entity, Access $access): void
  16. {
  17. $this->organizationDefaultValue($entity, $access);
  18. $this->billingSettingDefaultValueDefaultValue($entity, $access);
  19. }
  20. /**
  21. * @throws \ReflectionException
  22. */
  23. protected function organizationDefaultValue(mixed $entity, Access $access): void
  24. {
  25. $reflection = new \ReflectionClass($entity::class);
  26. $organizationFaultValue = $reflection->getAttributes(OrganizationDefaultValue::class)[0] ?? null;
  27. $fieldName = $organizationFaultValue?->getArguments()['fieldName'] ?? null;
  28. if ($fieldName) {
  29. $entity->{sprintf('set%s', ucfirst($fieldName))}(...[$access->getOrganization()]);
  30. }
  31. }
  32. /**
  33. * @throws \ReflectionException
  34. */
  35. protected function billingSettingDefaultValueDefaultValue(mixed $entity, Access $access): void
  36. {
  37. $reflection = new \ReflectionClass($entity::class);
  38. $billingSettingDefaultValueDefault = $reflection->getAttributes(BillingSettingDefaultValue::class)[0] ?? null;
  39. $fieldName = $billingSettingDefaultValueDefault?->getArguments()['fieldName'] ?? null;
  40. if ($fieldName) {
  41. $entity->{sprintf('set%s', ucfirst($fieldName))}(...[$access->getOrganization()?->getBillingSetting()]);
  42. }
  43. }
  44. }