<?php
/**
* Created by PhpStorm.
* User: antoinenedelec
* Date: 25/04/2018
* Time: 18:08
*/
namespace App\Controller;
use App\Entity\Presta;
use App\Kernel;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Lock\Factory;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\Store\SemaphoreStore;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class HomepageController extends AbstractController
{
/**
* @var Kernel
*/
private $kernel;
private $factory;
private $lockName = "!!!lock-dropbox-simultaneous-id-";
private $prestaLockName = "!!!lock-dropbox-presta-";
private $fileName = "PROCESS_WEB_APP.txt";
public function __construct(Kernel $kernel)
{
$this->kernel = $kernel;
}
/**
* @Route("/", name="homepage")
*/
public function homepage(TranslatorInterface $translator): Response
{
return $this->render('homepage/view.html.twig', [
'title' => $translator->trans('title.homepage'),
]);
}
/**
* @Route("/errorlog", name="errorlog")
*/
public function errorLogAction(LoggerInterface $logger, TranslatorInterface $translator): Response
{
$numberOfLog = 50;
$fileName = $this->kernel->getLogDir() . "/alert.log";
$logs = [];
try {
$handle = fopen($fileName, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$logs[] = substr(str_replace("app.ALERT: ", "", $line), 0, -6);
}
fclose($handle);
}
} catch (\Exception $e) {
$logger->error($e->getMessage());
}
return $this->render('settings/error_log.html.twig', [
'title' => $translator->trans('title.errorlog'),
'logs' => array_reverse(array_slice($logs, -$numberOfLog, $numberOfLog, true)),
]);
}
/**
* @Route("/faq", name="faq")
*/
public function faqAction(TranslatorInterface $translator): Response
{
return $this->render('faq/view.html.twig', [
'title' => $translator->trans('title.faq')
]);
}
/**
* @Route("/information/{id}", name="information")
*/
public function informationAction($id, TranslatorInterface $translator): Response
{
return $this->render('common/information.html.twig', [
'title' => $translator->trans('title.information'),
'information' => $translator->trans('information.' . $id)
]);
}
/**
* @Route("/admin/process", name="admin.process")
*/
public function userProcessAction(ManagerRegistry $doctrine): Response
{
// GET Number
$fileName = $this->kernel->getLogDir() . "/" . $this->fileName;
$last = -1;
try {
$handle = fopen($fileName, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$last = (int)$line;
}
fclose($handle);
}
} catch (\Exception $e) {
}
$store = new SemaphoreStore();
$this->factory = new LockFactory($store);
$prestas = $doctrine->getRepository(Presta::class)->findAllPrestaToSync();
$presta_locks = [];
foreach ($prestas as $presta) {
$presta_locks[$presta->getId()] = $this->testLock($this->prestaLockName . $presta->getId());
}
$process_lock = [];
$process_lock[] = $this->testLock($this->lockName . "0");
$process_lock[] = $this->testLock($this->lockName . "1");
$dropbox_lock = $this->testLock("lock_use_dropbox_" . "0");
return $this->render('admin/process.html.twig', [
'title' => 'Touts les process',
'presta_locks' => $presta_locks,
'process_lock' => $process_lock,
'dropbox_lock' => $dropbox_lock,
'last' => $last,
]);
}
private function testLock($lock_name): string
{
if ($this->factory instanceof Factory) {
$lock = $this->factory->createLock($lock_name, 3600);
}
if ($lock->acquire()) {
$lock->release();
return $lock_name . " libre";
} else {
return $lock_name . " occupé";
}
}
}