<commit_before><?php

declare(strict_types=1);

namespace App\Service\PaymentProcess;

use App\Entity\Payment;
use App\Entity\WayForPayLog;
use App\Exception\UnprocessedPaymentStatusException;
use App\Service\ReferralService;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Monolog\Logger;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Router;
use Symfony\Component\Translation\TranslatorInterface;

/**
 * AbstractPaymentProcessService.
 */
abstract class AbstractPaymentProcessService implements PaymentProcessInterface
{
    public const SESSION_PAYMENT_KEY = 'session_payment';

    public const TRANSACTION_APPROVED_AND_SET_PAID_STATUS = 'approved_and_set_paid';
    public const TRANSACTION_STATUS_PENDING = 'pending';
    public const TRANSACTION_STATUS_FAIL = 'fail';

    protected $transactionStatus = [];

    /** @var string */
    protected $transactionStatusKey = '';
    protected $appConfig;
    protected $translator;
    protected $request;
    protected $locale;
    protected $logger;
    protected $referralService;
    protected $em;
    protected $session;
    protected $router;

    /**
     * @param array               $appConfig
     * @param TranslatorInterface $translator
     * @param RequestStack        $requestStack
     * @param Router              $router
     * @param EntityManager       $em
     * @param Logger              $logger
     * @param ReferralService     $referralService
     * @param Session             $session
     */
    public function __construct($appConfig, TranslatorInterface $translator, RequestStack $requestStack, Router $router, EntityManager $em, Logger $logger, ReferralService $referralService, Session $session)
    {
        $this->appConfig = $appConfig;
        $this->request = $requestStack->getCurrentRequest();
        $this->translator = $translator;
        $this->locale = null !== $this->request ? $this->request->getLocale() : 'uk';
        $this->logger = $logger;
        $this->referralService = $referralService;
        $this->em = $em;
        $this->session = $session;
        $this->router = $router;
    }

    /**
     * @param array $data
     *
     * @return array|null
     */
    public function getResponseOnServiceUrl(array $data): ?array
    {
        return [];
    }

    /**
     * @return string
     */
    abstract public function getOrderNumberKey(): string;

    /**
     * @param array  $data
     * @param string $paymentIdKey
     * @param string $paymentGate
     *
     * @return string
     *
     * @throws UnprocessedPaymentStatusException
     * @throws ORMException
     * @throws OptimisticLockException
     */
    protected function processSystemData(array $data, string $paymentIdKey, string $paymentGate): string
    {
        /** @var Payment $payment */
        $payment = $this->em
            ->getRepository(Payment::class)
            ->find($data[$paymentIdKey])
        ;

        if (!$payment) {
            $this->logger->addCritical(\sprintf('%s interaction Fail! payment not found', $this->getSystemName()));
            $this->saveDataLog(null, $data, \sprintf('%s: payment not found', $this->getSystemName()));

            throw new BadRequestHttpException('payment not found');
        }

        if ($payment->isPending() && $this->checkPayment($payment, $data)) {
            $payment->setPaidWithGate($paymentGate);

            $this->em->flush();

            $this->session->set(self::SESSION_PAYMENT_KEY, $data[$paymentIdKey]);
            $this->processReferral($payment, $data);
            $this->saveDataLog($payment, $data, \sprintf('%s: set paid', $this->getSystemName()));

            return self::TRANSACTION_APPROVED_AND_SET_PAID_STATUS;
        }

        $status = $this->getStatusFromData($data);
        $this->saveDataLog(null, $data, \sprintf('%s status %s!', $this->getSystemName(), $status));

        return $status;
    }

    /**
     * @param Payment $payment
     * @param array   $data
     *
     * @return bool
     */
    abstract protected function checkPayment(Payment $payment, array $data): bool;

    /**
     * @param array        $data
     * @param Payment|null $payment
     *
     * @return array
     */
    abstract protected function getRequestDataToArr(array $data, ?Payment $payment): array;
<commit_msg>fix out of memory - opt.<commit_after><?php

declare(strict_types=1);

namespace App\Service\PaymentProcess;

use App\Entity\Payment;
use App\Entity\WayForPayLog;
use App\Exception\UnprocessedPaymentStatusException;
use App\Service\ReferralService;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Monolog\Logger;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Router;
use Symfony\Component\Translation\TranslatorInterface;

/**
 * AbstractPaymentProcessService.
 */
abstract class AbstractPaymentProcessService implements PaymentProcessInterface
{
    public const SESSION_PAYMENT_KEY = 'session_payment';

    public const TRANSACTION_APPROVED_AND_SET_PAID_STATUS = 'approved_and_set_paid';
    public const TRANSACTION_STATUS_PENDING = 'pending';
    public const TRANSACTION_STATUS_FAIL = 'fail';

    protected $transactionStatus = [];

    /** @var string */
    protected $transactionStatusKey = '';
    protected $appConfig;
    protected $translator;
    protected $request;
    protected $locale;
    protected $logger;
    protected $referralService;
    protected $em;
    protected $session;
    protected $router;

    /**
     * @param array               $appConfig
     * @param TranslatorInterface $translator
     * @param RequestStack        $requestStack
     * @param Router              $router
     * @param EntityManager       $em
     * @param Logger              $logger
     * @param ReferralService     $referralService
     * @param Session             $session
     */
    public function __construct($appConfig, TranslatorInterface $translator, RequestStack $requestStack, Router $router, EntityManager $em, Logger $logger, ReferralService $referralService, Session $session)
    {
        $this->appConfig = $appConfig;
        $this->request = $requestStack->getCurrentRequest();
        $this->translator = $translator;
        $this->locale = null !== $this->request ? $this->request->getLocale() : 'uk';
        $this->logger = $logger;
        $this->referralService = $referralService;
        $this->em = $em;
        $this->session = $session;
        $this->router = $router;
    }

    /**
     * @param array $data
     *
     * @return array|null
     */
    public function getResponseOnServiceUrl(array $data): ?array
    {
        return [];
    }

    /**
     * @return string
     */
    abstract public function getOrderNumberKey(): string;

    /**
     * @param array  $data
     * @param string $paymentIdKey
     * @param string $paymentGate
     *
     * @return string
     *
     * @throws UnprocessedPaymentStatusException
     * @throws ORMException
     * @throws OptimisticLockException
     */
    protected function processSystemData(array $data, string $paymentIdKey, string $paymentGate): string
    {
        /** @var Payment $payment */
        $payment = $this->em
            ->getRepository(Payment::class)
            ->find($data[$paymentIdKey])
        ;

        if (!$payment) {
            $this->logger->addCritical(\sprintf('%s interaction Fail! payment not found', $this->getSystemName()));
            $this->saveDataLog(null, $data, \sprintf('%s: payment not found', $this->getSystemName()));

            throw new BadRequestHttpException('payment not found');
        }

        if ($payment->isPending() && $this->checkPayment($payment, $data)) {
            $payment->setPaidWithGate($paymentGate);

            $this->em->flush();

            $this->session->set(self::SESSION_PAYMENT_KEY, $data[$paymentIdKey]);
            $this->processReferral($payment, $data);
            $this->saveDataLog($payment, $data, \sprintf('%s: set paid', $this->getSystemName()));

            return self::TRANSACTION_APPROVED_AND_SET_PAID_STATUS;
        }

        $status = $this->getStatusFromData($data);
        $this->saveDataLog(null, $data, \sprintf('%s status %s!', $this->getSystemName(), $status), $status);

        return $status;
    }

    /**
     * @param Payment $payment
     * @param array   $data
     *
     * @return bool
     */
    abstract protected function checkPayment(Payment $payment, array $data): bool;

    /**
     * @param array        $data
     * @param Payment|null $payment
     *
     * @return array
     */
    abstract protected function getRequestDataToArr(array $data, ?Payment $payment): array;

{'fraction_non_alphanumeric': 0.12182179969047092, 'fraction_numerical': 0.00022109219544550078, 'mean_word_length': 2.9147555170921677, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5013433', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2498, 'n_words': 547}