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

<?php

namespace App\Command;

use App\Entity\User;
use App\Service\Certificate\SyncCertificate;
use App\Service\State\StateUser;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
    name: 'app:sync-certs',
    description: 'Synchronize certificates with an RCC',
)]
class SyncCertsCommand extends Command
{
    public const MSG_TYPE_INFO = 1;
    public const MSG_TYPE_COMMENT = 2;
    public const MSG_TYPE_ERROR = 3;

    public function __construct(
        private readonly LoggerInterface $syncLogger,
        private readonly EntityManagerInterface $entityManager,
        private readonly StateUser $stateUser,
        private readonly SyncCertificate $syncCertificate,
    ){
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setHelp('Usage: <info>/usr/local/cpanel/3rdparty/bin/php /usr/local/cpanel/whostmgr/cgi/ncssl/source/bin/console app:sync-certs</info>');
    }

    /**
     * @throws Exception
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->write($this->prepareMessage('Syncing certificates with Namecheap started...', self::MSG_TYPE_INFO));

        /** @var UserRepository $userRepository */
        $userRepository = $this->entityManager->getRepository(User::class);
        $users = $userRepository->findUsersForSync();

        foreach ($users as $user) {
            $this->stateUser->setUser($user);

            $this->syncLogger->debug('Sync for cPanelUser started', [
                'userName' => $user->getName(),
                'ncLogin' => $user->getNcLogin(),
            ]);

            try {
                $this->syncCertificate->synchronizeAndInstall();
            } catch (\Throwable $throwable) {
                $this->syncLogger->error($throwable->getMessage(), $throwable->getTrace());
            }

            $this->syncLogger->debug('Sync for cPanelUser finished', [
                'userName' => $user->getName(),
                'ncLogin' => $user->getNcLogin(),
            ]);
        }

        $output->write($this->prepareMessage('Syncing certificates with Namecheap was end...', self::MSG_TYPE_INFO));

        return Command::SUCCESS;
    }
    protected function prepareMessage(string $msg, int $type): string
    {
        return match ($type) {
            self::MSG_TYPE_INFO => sprintf("<info>%s</info>\n", $msg),
            self::MSG_TYPE_COMMENT => sprintf("<comment>%s</comment>\n", $msg),
            self::MSG_TYPE_ERROR => sprintf("<error>%s</error>\n", $msg),
            default => '',
        };

    }
}
Back to Directory File Manager