<?php
namespace App\Controller;
use App\Service\Features;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
class DebugController extends AbstractController
{
private KernelInterface $kernel;
private Application $application;
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
$this->application = new Application($this->kernel);
$this->application->setAutoExit(false);
}
#[Route(path: '/debug-features/', name: 'debug.task.debug_features')]
public function debugFeatures(Features $features): Response
{
$this->assertAvailable();
$features = [];
foreach ($_ENV as $k => $v) {
if(str_starts_with($k, 'FEATURE'))
$features[$k] = $v;
}
ksort($features);
return $this->json($features);
}
#[Route(path: '/task-charge-placed/', name: 'debug.task.charge_placements')]
public function taskChargePlacements(): Response
{
return $this->getResponse($this->runCommand('sales:profile-saloon:adboard:charge'));
}
#[Route(path: '/task-charge-hidden/', name: 'debug.task.charge_hidings')]
public function taskChargeHidings(): Response
{
return $this->getResponse($this->runCommand('sales:profile:hiding:charge'));
}
#[Route(path: '/task-profiles-rotate/', name: 'debug.task.profiles.rotate')]
public function taskProfilesRotate(): Response
{
$output = $this->runCommand('sales:profile:adboard:rotate');
return $this->getResponse($output);
}
#[Route(path: '/task-saloons-rotate/', name: 'debug.task.saloons.rotate')]
public function taskSaloonsRotate(): Response
{
$output = $this->runCommand('sales:saloon:adboard:rotate');
return $this->getResponse($output);
}
#[Route(path: '/task-rotate/', name: 'debug.task.rotate')]
public function taskRotate(): Response
{
$output = $this->runCommand('sales:profile:adboard:rotate');
$output .= $this->runCommand('sales:saloon:adboard:rotate');
return $this->getResponse($output);
}
#[Route(path: '/task-profiles-rotate-free/', name: 'debug.task.profiles.rotate_free')]
public function taskProfilesRotateFree(): Response
{
return $this->getResponse($this->runCommand('sales:profile:adboard:rotate-free'));
}
#[Route(path: '/task-profiles-align/', name: 'debug.task.profiles.align')]
public function taskProfilesAlign(): Response
{
$output = $this->runCommand('sales:profile:adboard:align-positions');
return $this->getResponse($output);
}
#[Route(path: '/task-profiles-delete-rejected-on-moderation/', name: 'debug.task.profiles.delete_rejected_on_moderation')]
public function taskProfilesDeleteRejectedOnModeration(): Response
{
return $this->getResponse($this->runCommand('update:profiles:delete-rejected-on-moderation', [], ['interval' => '5 MINUTE']));
}
#[Route(path: '/task-users-set-trusted/', name: 'debug.task.users.set_trusted')]
public function taskUsersSetTrusted(): Response
{
return $this->getResponse($this->runCommand('update:users:set-trusted'));
}
#[Route(path: '/task-users-low-balance-notification/', name: 'debug.task.users.low_balance_notification')]
public function taskUsersLowBalanceNotification(): Response
{
return $this->getResponse($this->runCommand('sales:user:low-balance:notify'));
}
#[Route(path: '/task-persist-ctr-values-permanent/', name: 'debug.task.profile_ctr.persist_permanent')]
public function taskPersistCtrValuesPermanent(): Response
{
return $this->getResponse($this->runCommand('maintenance:profile-ctr:persist-permanent'));
}
#[Route(path: '/task-clear-ctr-values-cache/', name: 'debug.task.profile_ctr.clear_cache')]
public function taskClearCtrValuesCache(): Response
{
return $this->getResponse($this->runCommand('maintenance:profile-ctr:clear-cache'));
}
#[Route(path: '/task-top-100-refresh/', name: 'debug.task.top_100.refresh')]
public function taskTop100Refresh(): Response
{
return $this->getResponse($this->runCommand('maintenance:top-100:refresh'));
}
#[Route(path: '/process-bulk-background-actions/', name: 'debug.task.background.bulk.process_actions')]
public function taskProcessBulkBackgroundAction(): Response
{
set_time_limit(300);
$this->runCommand('account:bulk-queue:process');
return $this->getResponse('OK');
}
#[Route(path: '/process-bulk-background-actions-via-consumer/', name: 'debug.task.background.bulk.process_actions_via_consumer')]
public function taskProcessBulkBackgroundActionViaConsumer(): Response
{
set_time_limit(600);
$this->runCommand('messenger:consume');
return $this->getResponse('OK');
}
#[Route(path: '/process-video-transcoding/', name: 'debug.task.background.process_video_transcoding')]
public function taskProcessVideoTranscoding(): Response
{
set_time_limit(600);
$this->runCommand('messenger:consume', ['receivers' => ['transcoding']], ['limit' => 1]);
return $this->getResponse('OK');
}
#[Route(path: '/process-test-data-dummy-profiles-create/', name: 'debug.task.test-data.dummy-profiles.create')]
public function taskCreateDummyProfiles(): Response
{
set_time_limit(600);
return $this->getResponse($this->runCommand('test-data:dummy-profiles:create', ['default_user' => true]));
}
#[Route(path: '/task-hide-free-unapproved-placements/', name: 'debug.task.hide_free_unapproved_placements')]
public function taskHideFreeUnapprovedPlacements(): Response
{
return $this->getResponse($this->runCommand('update:profiles:hide-free-unapproved-placements'));
}
#[Route(path: '/genereate-profiles-export/', name: 'debug.task.export.create')]
public function taskCreateProfilesExport(): Response
{
set_time_limit(600);
return $this->getResponse($this->runCommand('export:profiles', [], [
'city' => 'moscow',
'price_gte' => 15000,
]));
}
#[Route(path: '/task-stations-update/', name: 'debug.task.stations_update')]
public function taskStationsUpdate(): Response
{
return $this->getResponse($this->runCommand('stations:update'));
}
#[Route(path: '/task-set-seo-phone-numbers-to-profiles/', name: 'debug.task.set_seo_phone_numbers_to_profiles')]
public function taskSetSeoPhoneNumbersToProfiles(): Response
{
return $this->getResponse($this->runCommand('maintenance:seo:set-phones-to-profiles'));
}
#[Route(path: '/task-set-seo-phone-numbers-to-saloons/', name: 'debug.task.set_seo_phone_numbers_to_saloons')]
public function taskSetSeoPhoneNumbersToSaloons(): Response
{
return $this->getResponse($this->runCommand('maintenance:seo:set-phones-to-saloons'));
}
protected function assertAvailable()
{
if($this->kernel->getEnvironment() != 'dev'
&& $this->kernel->getEnvironment() != 'review')
throw new UnauthorizedHttpException('Not available');
}
protected function runCommand(string $command, array $arguments = [], array $options = []): string
{
$this->assertAvailable();
$arrayInput = [
'command' => $command,
];
foreach ($arguments as $k => $v)
$arrayInput[$k] = $v;
foreach ($options as $k => $v)
$arrayInput['--'.$k] = $v;
$input = new ArrayInput($arrayInput);
$output = new BufferedOutput();
$this->application->run($input, $output);
return $output->fetch();
}
protected function getResponse(string $output): Response
{
return new Response($output, headers: ['Content-Type' => 'text/plain; charset=utf-8']);
}
}