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

<?php

namespace App\Command;

use App\Exception\ActivateException;
use App\Exception\CertificateException;
use App\Service\Certificate\Activate;
use App\Service\Certificate\Certificate;
use App\Service\Certificate\CertificateTransfer;
use App\Service\CpanelApi\Adapter\CliAdapter;
use App\Service\CpanelApi\CpanelApi;
use Doctrine\DBAL\Driver\Exception;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
    name: 'app:activate',
    description: 'Activate certificate command',
)]
class ActivateCommand extends Command
{
    public function __construct(
        private readonly CpanelApi $api,
        private readonly Activate $activate,
        private readonly Certificate $certificate,
        private readonly LoggerInterface $logger,
    ){
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->addArgument('domainName', InputArgument::REQUIRED, 'Domain name')
            ->addArgument('isAsync', InputArgument::OPTIONAL, 'Is Async Flow', false)
        ;
    }


    /**
     * @throws ActivateException
     * @throws CertificateException
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $adapter = $this->api->getAdapter();
        if ($adapter instanceof CliAdapter) {
            $adapter->setUsername($_SERVER['USER']);
        }

        $certificateTransfer = new CertificateTransfer([
            'userName' => $_SERVER['USER'],
            'domainName' => $input->getArgument('domainName'),
        ]);

        $certificateTransfer = $this->certificate->getCertificate($certificateTransfer);

        try {
            $this->activate->activate($certificateTransfer, $input->getArgument('isAsync'));
        } catch (Exception $exception) {
            $this->logger->error(sprintf('Error occurred activate request: %s', $exception->getMessage()));

            return Command::FAILURE;
        }

        return Command::SUCCESS;
    }
}
Back to Directory File Manager