<?php declare(strict_types=1);
namespace App\Bundle\Croatia\EventSubscriber;
use App\Contracts\HttpFoundation\RequestInfoInterface;
use App\Event\UserPasswordResetedEvent;
use App\Event\UserRegisteredEvent;
use Psr\Log\LoggerInterface;
use GuzzleHttp\Client;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Helper\UserHelper;
class UserEventsSubscriber implements EventSubscriberInterface
{
/**
* @var Client
*/
private $neturalClient;
/** @var RequestInfoInterface */
private $requestInfo;
/** @var UserHelper */
protected $userHelper;
public function __construct(Client $neturalClient, RequestInfoInterface $requestInfo, UserHelper $userHelper)
{
$this->neturalClient = $neturalClient;
$this->requestInfo = $requestInfo;
$this->userHelper = $userHelper;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
UserRegisteredEvent::class => 'onUserRegistered',
UserPasswordResetedEvent::class => 'onUserPasswordReseted',
];
}
public function onUserPasswordReseted(UserPasswordResetedEvent $event)
{
$user = $event->getUser();
$this->neturalClient->post('/restV/sendform/passwordReset', [
'body' => json_encode([
'link' => sprintf(
'%s/user/passwort-aendern/?memberid=%s&hash=%s',
getenv('MAIL_PASSWORD_ACTIVATION_URL'),
$user->getMemberId(),
$event->getResetCode()
),
'forename' => $user->getFirstName(),
'surname' => $user->getLastName(),
'nickname' => $user->getUsername(),
'to' => $user->getEmail(),
'locale' => $this->requestInfo->getMasterRequest()->getLocale(),
'country' => getenv('APP_COUNTRY')
])
]);
}
public function onUserRegistered(UserRegisteredEvent $event)
{
$user = $event->getUser();
$activationLink = sprintf(
'%s/user/register/finish/?memberid=%s&code=%s&userId=%s',
getenv('MAIL_PASSWORD_ACTIVATION_URL'),
$user->getMemberId(),
$event->getActivationCode(),
$user->getMemberId()
);
if ($user->getAppleUserId()) {
// automatically activate user, if apple id is given
$this->userHelper->activateAppleUser($user->getMemberId());
// $this->neturalClient->request('GET', $activationLink);
} else {
$this->neturalClient->post('/restV/sendform/registration', [
'body' => json_encode([
'link' => $activationLink,
'refId' => $user->getMemberId(),
'forename' => $user->getFirstName(),
'surname' => $user->getLastName(),
'nickname' => $user->getUsername(),
'to' => $user->getEmail(),
'locale' => $this->requestInfo->getMasterRequest()->getLocale(),
'country' => getenv('APP_COUNTRY')
])
]);
}
}
}