Current path: home/webdevt/cryptoimpot.fr/module/Application/src/Job/
?? Go up: /home/webdevt/cryptoimpot.fr/module/Application/src
<?php
namespace Application\Job;
use DoctrineModule\Persistence\ObjectManagerAwareInterface;
use DoctrineModule\Persistence\ProvidesObjectManager as ProvidesObjectManagerTrait;
use SlmQueue\Job\AbstractJob;
use Psr\Container\ContainerInterface;
use \Laminas\ServiceManager\ServiceLocatorInterface;
//use Application\Entity\Report;
//use Application\Log\LoggerAwareInterface;
//use Application\Log\LoggerAwareTrait;
//use Application\Service\ReportService;
class ImportAPI extends AbstractJob implements ObjectManagerAwareInterface {
//LoggerAwareInterface
//class ImportAPI extends AbstractJob {
//use LoggerAwareTrait;
use ProvidesObjectManagerTrait;
private $entityManager;
private $container;
public function getEntityManager() {
return $this->entityManager;
}
private $services;
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->services = $serviceLocator->getServiceLocator();
}
public function getServiceLocator() {
return $this->services;
}
/**
* @var
*/
protected $serviceExchange;
/**
* @var array
*/
protected $reportId = array();
public function setServiceExchange($serviceExchange) {
$this->serviceExchange = $serviceExchange;
}
public function getServiceExchange() {
return $this->serviceExchange;
}
public function __construct($entityManager) {
//// public function __construct()
$this->entityManager = $entityManager;
//parent::__construct();
//// $om = $this->getEntityManager();
//// $this->container = $container;
}
/**
* Execute the job
*
* @return void
*/
public function execute() {
$om = $this->getEntityManager();
// var_dump($om);
//clear object manager does not work
//$om = $this->getObjectManager();
//$om->clear();
file_put_contents('/Users/jimmy/Sites/ledgio2/data/logjob.txt', 'import', FILE_APPEND | LOCK_EX);
try {
//$container->get('doctrine.entitymanager.orm_default');
$content = $this->getContent();
$classNameService = $content['classNameService'];
$userMapper = $this->getEntityManager()->getRepository(\User\Entity\User::class);
$user = $userMapper->findOneById($content['userId']);
$mapperExchange = new \Application\Mapper\ExchangeMapper($this->getEntityManager());
$exchange = $mapperExchange->findById($content['exchangeId']);
$serviceExchange = new $classNameService($content['data'], $user, $exchange, $this->getEntityManager());
// var_dump($content);
$serviceExchange->importTradesHistory();
// add message queue
$mapperMessage = new \Application\Mapper\MessageMapper($this->getEntityManager());
$message = new \Application\Entity\Message(
array(
'user' => $user,
'messageHeader' => 'API ' . $exchange->getName(),
'messageBody' => 'Importation des trades via l\'API '.$exchange->getName().' terminée',
'messageFooter',
'messageType' => 'success'
));
$mapperMessage->insert($message);
$ledgioService = new \Application\Service\LedgioService($user, $this->getEntityManager());
$ledgioService->update();
//echo 'Hello World';
// if (is_array($this->reportId)) {
// foreach ($this->reportId as $reportId) {
// $this->updateRank($reportId);
// }
// unset($reportId);
// } else {
// $this->updateRank($this->reportId);
// }
} catch (\Exception $exception) {
$mapperMessage = new \Application\Mapper\MessageMapper($this->getEntityManager());
$message = new \Application\Entity\Message(
array(
'user' => $user,
'messageHeader' => 'API ' . $exchange->getName(),
'messageBody' => 'Importation des trades via l\'API '.$exchange->getName().' échouée',
'messageFooter',
'messageType' => 'danger'
));
$mapperMessage->insert($message);
}
// $content = $this->getContent();
// $this->setReportId($content['reportId']);
//
// if (!empty($this->reportId)) {
// try {
// if (is_array($this->reportId)) {
// foreach ($this->reportId as $reportId) {
// $this->updateRank($reportId);
// }
// unset($reportId);
// } else {
// $this->updateRank($this->reportId);
// }
// } catch (\Exception $exception) {
// echo "Exception message is {$exception->getMessage()} \n";
// }
// }
}
private function updateRank($reportId) {
/* @var $report Report */
// $report = $this->reportService->getReport($reportId);
// $this->logInfo(print_r($report, true)); // this always return older db values, the values before it start queue deamon
//
// if (!$report instanceof Report) {
// return;
// }
//
// if (empty($rankData)) {
// return;
// }
//
// //more codes, application related logics
//
// $this->reportService->updateReportEntity($report);
}
private function setReportId($reportId) {
if (is_numeric($reportId)) {
$this->reportId = array($reportId);
} elseif (is_array($reportId)) {
$this->reportId = $reportId;
} else {
throw new \Exception('Expects reportId as int or array');
}
}
}