129 lines
4.1 KiB
PHP
129 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\BusProNet;
|
|
|
|
use App\BusProNet\Model\CrmAttributesResponse;
|
|
use App\BusProNet\Model\ProfileResponse;
|
|
use App\Entity\Embeddable\Address;
|
|
use App\Entity\Embeddable\Communication;
|
|
use App\Entity\Teamer;
|
|
use App\Entity\User;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class UserDataHandler
|
|
{
|
|
public function __construct(
|
|
private readonly EntityManagerInterface $entityManager,
|
|
private readonly LoggerInterface $logger
|
|
) {
|
|
}
|
|
|
|
public function collectRoles(CrmAttributesResponse $crmAttributes, ?string $preferredRole): array
|
|
{
|
|
// Collect user's roles from CRM attributes
|
|
$roles = [];
|
|
|
|
if ($crmAttributes->isAdmin() && (null === $preferredRole || 'admin' === $preferredRole)) {
|
|
$roles[] = 'ROLE_ADMIN';
|
|
} elseif ($crmAttributes->isManager() && (null === $preferredRole || 'manager' === $preferredRole)) {
|
|
$roles[] = 'ROLE_MANAGER';
|
|
} elseif ($crmAttributes->isHouseManager() && (null === $preferredRole || 'house_manager' === $preferredRole)) {
|
|
$roles[] = 'ROLE_HOUSE_MANAGER';
|
|
}
|
|
|
|
if ($crmAttributes->isTeamer()) {
|
|
$roles[] = 'ROLE_TEAMER';
|
|
}
|
|
|
|
return $roles;
|
|
}
|
|
|
|
public function findLocalUser(ProfileResponse $profileResponse): ?User
|
|
{
|
|
// Check if user is already present in local database
|
|
$repository = $this->entityManager->getRepository(User::class);
|
|
|
|
return $repository->findOneBy([
|
|
'busProAddressId' => $profileResponse->getAddressId(),
|
|
'busProPersonId' => $profileResponse->getPersonId(),
|
|
]);
|
|
}
|
|
|
|
public function createLocalUser(
|
|
ProfileResponse $profileResponse,
|
|
array $roles,
|
|
bool $isTeamer = false,
|
|
array $crmSelections = [],
|
|
array $hotelCodes = []
|
|
): User {
|
|
$user = new User();
|
|
$user
|
|
->setFirstName($profileResponse->getFirstName())
|
|
->setLastName($profileResponse->getName())
|
|
->setEmail($profileResponse->getCommunication()->getEmail())
|
|
->setBusProPersonId($profileResponse->getPersonId())
|
|
->setBusProAddressId($profileResponse->getAddressId())
|
|
->setHotelCodes($hotelCodes)
|
|
->setRoles($roles)
|
|
;
|
|
|
|
if (true === $isTeamer) {
|
|
$teamer = Teamer::fromApiResponse($profileResponse);
|
|
$teamer->setCrmSelections($crmSelections);
|
|
$user->setTeamer($teamer);
|
|
$this->logger->info('Create teamer', [
|
|
'teamer_id' => $teamer->getId(),
|
|
'teamer_name' => (string) $teamer,
|
|
]);
|
|
}
|
|
|
|
$this->entityManager->persist($user);
|
|
$this->entityManager->flush();
|
|
|
|
$this->logger->info('Create user', [
|
|
'user_id' => $user->getId(),
|
|
'user_email' => $user->getEmail(),
|
|
]);
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function updateLocalUser(
|
|
User $user,
|
|
ProfileResponse $profileResponse,
|
|
array $roles,
|
|
bool $isTeamer = false,
|
|
array $crmSelections = [],
|
|
array $hotelCodes = []
|
|
): void {
|
|
$user
|
|
->setEmail($profileResponse->getCommunication()->getEmail())
|
|
->setRoles($roles)
|
|
->setHotelCodes($hotelCodes)
|
|
;
|
|
|
|
if (true === $isTeamer) {
|
|
$address = Address::fromApiResponse($profileResponse);
|
|
$communication = Communication::fromApiResponse($profileResponse);
|
|
if (null === $user->getTeamer()) {
|
|
$teamer = Teamer::fromApiResponse($profileResponse);
|
|
$this->entityManager->persist($teamer);
|
|
} else {
|
|
$teamer = $user->getTeamer();
|
|
}
|
|
$teamer
|
|
->setAddress($address)
|
|
->setCommunication($communication)
|
|
->setCrmSelections($crmSelections)
|
|
;
|
|
|
|
$this->logger->info('Update teamer', [
|
|
'teamer_id' => $teamer->getId(),
|
|
'teamer_name' => (string) $teamer,
|
|
]);
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
}
|
|
} |