Viewing File: /usr/local/cpanel/whostmgr/docroot/cgi/ncssl/source/src/Service/Message/EventCoreNotifier.php

<?php

namespace App\Service\Message;

use App\Repository\CertificateRepository;
use App\Service\NcGatewayApi\NcGatewayApi;
use App\Service\State\StateUser;
use App\Traits\RequestBodyCreatorTrait;
use Exception;
use Psr\Log\LoggerInterface;

class EventCoreNotifier implements EventCoreNotifierInterface
{
    use RequestBodyCreatorTrait;

    private const GW_METHOD_NAME = 'web/handler';
    private const CORE_METHOD_NAME = 'addCertificateAdditionalInfo';

    /**
     * @param NcGatewayApi $ncGatewayApi
     * @param StateUser $stateUser
     * @param LoggerInterface $eventCoreNotifierLogger
     * @param CertificateRepository $certificateRepository
     */
    public function __construct(
        private readonly NcGatewayApi $ncGatewayApi,
        private readonly StateUser $stateUser,
        private readonly LoggerInterface $eventCoreNotifierLogger,
        private readonly CertificateRepository $certificateRepository
    ) { }

    /**
     * @param int $eventCode
     * @param int $certificateId
     * @param string|null $domain
     *
     * @return EventCoreNotifierInterface
     * @throws Exception
     */
    public function sendEvent(int $eventCode, int $certificateId, ?string $domain = null): EventCoreNotifierInterface
    {
        $domain = $domain ?? $this->findDomainName($certificateId);
        $eventResponse = $this->ncGatewayApi->makeCall(
            static::EVENT_CORE_NOTIFICATION_METHOD,
            $this->generateParameters($eventCode, $certificateId, $domain)
        );

        $this->eventCoreNotifierLogger->info('Call core event', [
            'core parameters' => $this->getCoreParameters($eventCode, $certificateId, $domain),
            'response' => $eventResponse,
        ]);

        return $this;
    }

    /**
     * @param int $eventCode
     * @param int $certificateId
     * @param string|null $domain
     *
     * @return array
     */
    private function generateParameters(int $eventCode, int $certificateId, ?string $domain): array
    {
        return [
            'coreEndpoint' => static::GW_METHOD_NAME,
            'coreData' => $this->createRequest(static::CORE_METHOD_NAME, $this->getCoreParameters($eventCode, $certificateId, $domain)),
        ];
    }

    /**
     * @param int $eventCode
     * @param int $certificateId
     * @param string|null $domain
     *
     * @return array
     */
    private function getCoreParameters(int $eventCode, int $certificateId, ?string $domain): array
    {
        return [
            'certificateId' => $certificateId,
            'eventCode' => $eventCode,
            'cPanelUserName' => $this->stateUser->getUser()->getName(),
            'cPanelHost' => gethostname(),
            'ncUserName' => $this->stateUser->getUser()->getNcLogin(),
            'domain' => $domain,
        ];
    }

    /**
     * @param int $ncCertificateId
     *
     * @return string|null
     */
    private function findDomainName(int $ncCertificateId): ?string
    {
        $certificate = $this->certificateRepository->findOneBy(['ncId' => $ncCertificateId]);

        return $certificate?->getCommonName();
    }
}
Back to Directory File Manager