Viewing File: /usr/local/cpanel/whostmgr/docroot/cgi/ncssl/source/src/Command/ToggleHttpRedirectCommand.php

<?php

namespace App\Command;

use App\Service\Manager\HttpsRedirectManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'app:toggle-http-redirect', description: 'Command for toggle http redirect')]
class ToggleHttpRedirectCommand extends Command
{
    private const PROTOCOL_HTTP = 'http';
    private const PROTOCOL_HTTPS = 'https';
    public function __construct(
        private readonly HttpsRedirectManager $httpsRedirectManager,
        private readonly LoggerInterface $logger,
    ){
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->addArgument(
                'domain',
                InputArgument::REQUIRED,
                'On which domain do you want to change the http status?'
            )
            ->addArgument(
                'protocol',
                InputArgument::REQUIRED,
                'On which protocol do you want to change the status? `http` or `https`'
            );
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $protocolArgument = $input->getArgument('protocol');
        if (!in_array($protocolArgument, [self::PROTOCOL_HTTP, self::PROTOCOL_HTTPS], true)) {
            throw new InvalidArgumentException('Protocol should be `http` or `https`');
        }
        $domainArgument = $input->getArgument('domain');
        $isHttps = $protocolArgument === self::PROTOCOL_HTTPS;

        try {
            $this->httpsRedirectManager->toggleHttpRedirect($domainArgument, $isHttps);

            return Command::SUCCESS;
        } catch (\Exception $e) {
            $this->logger->error(sprintf('Error occurred while toggle http redirect: %s', $e->getMessage()));
            return Command::FAILURE;
        }
    }
}
Back to Directory File Manager