<?php
// src/AppBundle/EventListener/CommandListener.php
namespace App\EventListener;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use AppBundle\Service\FlexiBee\Utils;
use Symfony\Component\HttpKernel\KernelInterface;
use App\Entity\User;
use App\Entity\UserConnector;
class CommandListener
{
private $container;
private $utils;
protected $doctrine;
protected $appKernel;
public function __construct(KernelInterface $appKernel,
ManagerRegistry $doctrine,
$defaultLocale = 'cs')
{
$this->appKernel = $appKernel;
$this->doctrine = $doctrine;
$this->defaultLocale = $defaultLocale;
}
//public function onConsoleCommand(ConsoleCommandEvent $event) {
/**
* @throws \ReflectionException
*/
//public function __invoke(RequestEvent $event): void {
public function onConsoleCommand(ConsoleCommandEvent $event) {
// get the output instance
$output = $event->getOutput();
// get the output instance
$input = $event->getInput();
$args = $input->getArguments();
// print("\n\n qqw onConsoleCommand getInput: ".$args['connectorCode']);
// \Doctrine\Common\Util\Debug::dump($args);
// get the command to be executed
$command = $event->getCommand();
// write something about the command
if(!empty($args['connectorCode']) && $args['connectorCode'] != '') {
$output->writeln(sprintf("\n ================= Before running command <info>%s</info> =================", $command->getName()));
$output->writeln(sprintf("\n User Connector Code: <info>%s</info>\n", $args['connectorCode']));
//$this->container->setParameter('locale', 'de');
//$request = $this->container->get('request_stack')->getCurrentRequest();
//$request = $event->getRequest();
//$request->setLocale('de');
$em = $this->doctrine->getManager();
/* we get user connector */
$userConnector = $em->getRepository(UserConnector::class)->getUserConnectorByCode($args['connectorCode']);
$userId = $userConnector->getUserId();
$user = $em->getRepository(User::class)->getUser($userId);
//test - setting current locale
//$this->container->get('gedmo.listener.translatable')->setTranslatableLocale('cs');
//$this->container->get('gedmo.listener.translatable')->setDefaultLocale('cs');
/* we setup entity manager for user erp database*/
// $connection = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'dynamic_conn'));
//$connection->setLocale('en');
$connection = $this->appKernel->getContainer()->get(sprintf('doctrine.dbal.%s_connection', 'dynamic_conn'));
$connection->close();
$refConn = new \ReflectionObject($connection);
$refParams = $refConn->getProperty('params');
$refParams->setAccessible('public'); //we have to change it for a moment
$params = $refParams->getValue($connection);
$params['dbname'] = $user->getDatabaseName();
$params['user'] = $user->getDatabaseUser();
$params['password'] = $user->getDatabasePassword();
$refParams->setAccessible('private');
$refParams->setValue($connection, $params);
$this->doctrine->resetManager('dynamic_em');
/* we setup entity manager for target e-shop */
// $connectionTarget = $this->container->get(sprintf('doctrine.dbal.%s_connection', 'eshop_conn'));
$connectionTarget = $this->appKernel->getContainer()->get(sprintf('doctrine.dbal.%s_connection', 'eshop_conn'));
$connectionTarget->close();
$refConn = new \ReflectionObject($connectionTarget);
$refParams = $refConn->getProperty('params');
$refParams->setAccessible('public'); //we have to change it for a moment
$params = $refParams->getValue($connectionTarget);
$params['host'] = $userConnector->getTargetDatabaseHost();
$params['dbname'] = $userConnector->getTargetDatabaseName();
$params['user'] = $userConnector->getTargetDatabaseUser();
$params['password'] = $userConnector->getTargetDatabasePassword();
$refParams->setAccessible('private');
$refParams->setValue($connectionTarget, $params);
$this->doctrine->resetManager('eshop_em');
}
}
}