src/Contact/Serializer/Normalizer/Relationship/EmploymentNormalizer.php line 53

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2021 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2021 TECLA Consulting Group oü
  16.  */
  17. namespace App\Contact\Serializer\Normalizer\Relationship;
  18. use App\Contact\Model\Relationship\EmploymentInterface;
  19. use App\Contact\Security\Relationship\Employment\CanDeleteEmployeePermission;
  20. use App\Contact\Security\Relationship\Employment\CanDeleteEmployerPermission;
  21. use App\Contact\Security\Relationship\Employment\CanUpdateEmployeePermission;
  22. use App\Contact\Security\Relationship\Employment\CanUpdateEmployerPermission;
  23. use App\Core\Serializer\Normalizer\AttributeAwareNormalizerTrait;
  24. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  25. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  26. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  27. use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
  28. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  29. final class EmploymentNormalizer implements ContextAwareNormalizerInterfaceCacheableSupportsMethodInterface
  30. {
  31.     use AttributeAwareNormalizerTrait;
  32.     private NormalizerInterface $normalizer;
  33.     private UrlGeneratorInterface $urlGenerator;
  34.     private AuthorizationCheckerInterface $authorizationChecker;
  35.     public function __construct(
  36.         NormalizerInterface $normalizer,
  37.         UrlGeneratorInterface $urlGenerator,
  38.         AuthorizationCheckerInterface $authorizationChecker
  39.     ) {
  40.         $this->normalizer $normalizer;
  41.         $this->urlGenerator $urlGenerator;
  42.         $this->authorizationChecker $authorizationChecker;
  43.     }
  44.     public function normalize($objectstring $format null, array $context = []): array|string|int|float|bool|\ArrayObject|null
  45.     {
  46.         $data $this->normalizer->normalize($object$format$context);
  47.         if (true === $this->shouldSerializeAttribute($dataEmploymentInterface::ATTR__LINKS$context)) {
  48.             $this->normalize_Links($object$data);
  49.         }
  50.         return $data;
  51.     }
  52.     public function supportsNormalization($datastring $format null, array $context = []): bool
  53.     {
  54.         return $data instanceof EmploymentInterface;
  55.     }
  56.     public function hasCacheableSupportsMethod(): bool
  57.     {
  58.         return __CLASS__ === static::class;
  59.     }
  60.     private function normalize_Links(EmploymentInterface $relationship, array &$data): void
  61.     {
  62.         $ajaxLinks = [];
  63.         if (true === $this->authorizationChecker->isGranted(CanUpdateEmployeePermission::class, $relationship)) {
  64.             $ajaxLinks['edit'] = $this->urlGenerator->generate('app.contact.ajax.contact_relationship.employment.employee.edit', [
  65.                 'contactId' => $relationship->getEmployer()->getId(),
  66.                 'relationshipId' => $relationship->getId(),
  67.             ]);
  68.         }
  69.         if (true === $this->authorizationChecker->isGranted(CanUpdateEmployerPermission::class, $relationship)) {
  70.             $ajaxLinks['editEmployer'] = $this->urlGenerator->generate('app.contact.ajax.contact_relationship.employment.employer.edit', [
  71.                 'contactId' => $relationship->getEmployee()->getId(),
  72.                 'relationshipId' => $relationship->getId(),
  73.             ]);
  74.         }
  75.         if (true === $this->authorizationChecker->isGranted(CanDeleteEmployeePermission::class, $relationship)) {
  76.             $ajaxLinks['remove'] = $this->urlGenerator->generate('app.contact.ajax.contact_relationship.employment.employee.remove', [
  77.                 'contactId' => $relationship->getEmployer()->getId(),
  78.                 'relationshipId' => $relationship->getId(),
  79.             ]);
  80.         }
  81.         if (true === $this->authorizationChecker->isGranted(CanDeleteEmployerPermission::class, $relationship)) {
  82.             $ajaxLinks['removeEmployer'] = $this->urlGenerator->generate('app.contact.ajax.contact_relationship.employment.employer.remove', [
  83.                 'contactId' => $relationship->getEmployee()->getId(),
  84.                 'relationshipId' => $relationship->getId(),
  85.             ]);
  86.         }
  87.         $data[EmploymentInterface::ATTR__LINKS] = [
  88.             'ajax' => $ajaxLinks,
  89.         ];
  90.     }
  91. }