<?php
// src/EventSubscriber/ExceptionSubscriber.php
namespace App\EventSubscriber;
use App\Entity\Location;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events as CoreWorkflowEvents;
use App\Event as MyCoreWorkflowEvents;
use Webkul\UVDesk\AutomationBundle\Workflow\Event;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpClient\CurlHttpClient;
//use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
class AgentEventSubscriber implements EventSubscriberInterface
{
//protected string $baseUrl;
//protected $entityManager;
public function __construct(EntityManagerInterface $entityManager, string $baseUrl) {
$this->entityManager = $entityManager;
$this->baseUrl = $baseUrl;
}
public static function getSubscribedEvents(): array
{
// return the subscribed events, their methods and priorities
return [
MyCoreWorkflowEvents\Agent\Create::getId() => [
['onAgentCreate', 0],
],
MyCoreWorkflowEvents\Agent\Update::getId() => [
['onAgentUpdate', 0],
],
CoreWorkflowEvents\Agent\Delete::getId() => [
['onAgentDelete', 0],
],
/*'uvdesk.automation.workflow.execute' => [
['onAgentUpdate', -1],
]*/
];
}
public function OnAgentCreate(Event $event)
{
$user = $event->getUser();
if (!empty($user) && $user instanceof \Webkul\UVDesk\CoreFrameworkBundle\Entity\User) {
//$data = ['email' => $user->getEmail(), 'password' => $event->getPassword()];
$userInfos = $event->getUserInfos();
$locationDetails = Location::decompose($userInfos->getCommune());
$roles = [];
foreach($user->getAgentInstance()->getSupportPrivileges() as $supportPrivileges) {
//foreach($supportPrivileges->getPrivileges() as $privileges) {
if(!in_array($supportPrivileges, $roles)) {
$roles[] = [$supportPrivileges->getName() => $supportPrivileges->getPrivileges()];
}
//}
}
$data = array_merge($locationDetails, ['email' => $user->getEmail(), 'name' => $user->getFullName(),
'password' => "@D'O25!dsGd", 'roles' => $roles]) ;
$httpClient = new CurlHttpClient(["verify_peer"=>false,"verify_host"=>false]);
$url = $this->baseUrl.'/api/assign-roles'; //'/api/register';
$response = $httpClient->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
],
// these values are automatically encoded before including them in the URL
'body' => $data,
]);
$rep = json_decode($response->getContent(), true);
//dd($rep);
/*if($rep['success']) {
$userInfos->setAccessToken($rep['token']);
//on doit persister �a en BD
$this->entityManager->persist($userInfos);
$this->entityManager->flush();*/
/*//on lui assigne les privileges
if(!empty($roles)) {
$url = $this->baseUrl.'/api/assign-roles';
$data = ['email' => $user->getEmail(), 'roles' => $roles];
$response = $httpClient->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
],
// these values are automatically encoded before including them in the URL
'body' => $data,
]);
}*/
/*} else {
//dd($rep);
/* to do flash message * /
}*/
}
}
public function OnAgentUpdate(Event $event)
{
$user = $event->getUser();
if (!empty($user) && $user instanceof \Webkul\UVDesk\CoreFrameworkBundle\Entity\User) {
//$data = ['email' => $user->getEmail(), 'password' => $event->getPassword()];
$userInfos = $event->getUserInfos();
$locationDetails = Location::decompose($userInfos->getCommune());
$roles = [];
foreach($user->getAgentInstance()->getSupportPrivileges() as $supportPrivileges) {
//foreach($supportPrivileges->getPrivileges() as $privileges) {
if(!in_array($supportPrivileges, $roles)) {
$roles[] = [$supportPrivileges->getName() => $supportPrivileges->getPrivileges()];
}
//}
}
$password = $event->getPassword() ? $event->getPassword() : null;
$httpClient = new CurlHttpClient(["verify_peer"=>false,"verify_host"=>false]);
$url = $this->baseUrl.'/api/assign-roles';
$data = array_merge(['email' => $user->getEmail(), 'roles' => $roles, 'name' => $user->getFullName(), 'password' => $password], $locationDetails);
//dd($data);
$response = $httpClient->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
],
// these values are automatically encoded before including them in the URL
'body' => $data,
]);
$rep = json_decode($response->getContent(), true);
//dd($rep);
if($rep['success']) {
$userInfos->setAccessToken($rep['ResponseData']['token']);
//on doit persister �a en BD
$this->entityManager->persist($userInfos);
$this->entityManager->flush();
//dd($rep);
} else {
/* to do flash message */
//dd($rep);
}
//dd($rep);
/*if(!$rep['success'] && strtolower($rep['message']) == "not found") {
$data = array_merge($locationDetails, ['email' => $user->getEmail(), 'name' => $user->getFullName(),
'password' => "@D'O25!dsGd", 'password_confirmation' => "@D'O25!dsGd"]) ;
$url = $this->baseUrl.'/api/register';
$response = $httpClient->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
],
// these values are automatically encoded before including them in the URL
'body' => $data,
]);
$rep = json_decode($response->getContent(), true);
//dd($rep);
if($rep['success']) {
$userInfos->setAccessToken($rep['ResponseData']['token']);
//on doit persister �a en BD
$this->entityManager->persist($userInfos);
$this->entityManager->flush();
//on lui assigne les privileges
if(!empty($roles)) {
$url = $this->baseUrl.'/api/assign-permission';
$data = ['email' => $user->getEmail(), 'permissions' => $roles];
$response = $httpClient->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
],
// these values are automatically encoded before including them in the URL
'body' => $data,
]);
}
}
}*/
}
}
}