<commit_before><?php

namespace Ekyna\Component\Commerce\Bridge\Doctrine\ORM\Repository;

use Ekyna\Component\Commerce\Common\Model\CurrencyInterface;
use Ekyna\Component\Commerce\Payment\Entity\PaymentMessage;
use Ekyna\Component\Commerce\Payment\Model\PaymentStates;
use Ekyna\Component\Commerce\Payment\Repository\PaymentMethodRepositoryInterface;
use Ekyna\Component\Resource\Doctrine\ORM\TranslatableResourceRepository;

/**
 * Class PaymentMethodRepository
 * @package Ekyna\Component\Commerce\Bridge\Doctrine\ORM\Repository
 * @author  <NAME> <<EMAIL>>
 */
class PaymentMethodRepository extends TranslatableResourceRepository implements PaymentMethodRepositoryInterface
{
    /**
     * {@inheritdoc}
     */
    public function createNew()
    {
        /** @var \Ekyna\Component\Commerce\Payment\Model\PaymentMethodInterface $method */
        $method = parent::createNew();

        foreach (PaymentStates::getNotifiableStates() as $state) {
            $message = new PaymentMessage();
            $method->addMessage($message->setState($state));
        }

        return $method;
    }

    /**
     * @inheritDoc
     */
    public function findAvailable(CurrencyInterface $currency = null)
    {
        $qb = $this->createQueryBuilder('m');
        $ex = $qb->expr();

        $parameters = [
            'enabled'   => true,
            'available' => true,
        ];

        $qb
            ->andWhere($ex->eq('m.enabled', ':enabled'))
            ->andWhere($ex->eq('m.available', ':available'))
            ->orderBy('m.position', 'ASC');

        if ($currency) {
            $parameters['currency'] = $currency;
            $qb->andWhere($ex->orX(
                $qb->expr()->isMemberOf(':currency', 'm.currencies'),
                'm.currencies IS EMPTY'
            ));
        }

        return $qb
            ->getQuery()
            ->setParameters($parameters)
            ->getResult();
    }

    /**
     * @inheritDoc
     */
    public function findEnabled(CurrencyInterface $currency = null)
    {
        $qb = $this->createQueryBuilder('m');
        $ex = $qb->expr();

        $parameters = [
            'enabled'   => true,
        ];

        $qb
            ->andWhere($ex->eq('m.enabled', ':enabled'))
            ->orderBy('m.position', 'ASC');

        if ($currency) {
            $parameters['currency'] = $currency;
            $qb->andWhere($ex->orX(
                $qb->expr()->isMemberOf(':currency', 'm.currencies'),
                'm.currencies IS EMPTY'
            ));
        }

        return $qb
            ->getQuery()
            ->setParameters([
                'enabled' => true,
            ])
            ->getResult();
    }
}<commit_msg>[Commerce] Fixed payment method repository.<commit_after><?php

namespace Ekyna\Component\Commerce\Bridge\Doctrine\ORM\Repository;

use Ekyna\Component\Commerce\Common\Model\CurrencyInterface;
use Ekyna\Component\Commerce\Payment\Entity\PaymentMessage;
use Ekyna\Component\Commerce\Payment\Model\PaymentStates;
use Ekyna\Component\Commerce\Payment\Repository\PaymentMethodRepositoryInterface;
use Ekyna\Component\Resource\Doctrine\ORM\TranslatableResourceRepository;

/**
 * Class PaymentMethodRepository
 * @package Ekyna\Component\Commerce\Bridge\Doctrine\ORM\Repository
 * @author  <NAME> <<EMAIL>>
 */
class PaymentMethodRepository extends TranslatableResourceRepository implements PaymentMethodRepositoryInterface
{
    /**
     * {@inheritdoc}
     */
    public function createNew()
    {
        /** @var \Ekyna\Component\Commerce\Payment\Model\PaymentMethodInterface $method */
        $method = parent::createNew();

        foreach (PaymentStates::getNotifiableStates() as $state) {
            $message = new PaymentMessage();
            $method->addMessage($message->setState($state));
        }

        return $method;
    }

    /**
     * @inheritDoc
     */
    public function findAvailable(CurrencyInterface $currency = null)
    {
        $qb = $this->createQueryBuilder('m');
        $ex = $qb->expr();

        $parameters = [
            'enabled'   => true,
            'available' => true,
        ];

        $qb
            ->andWhere($ex->eq('m.enabled', ':enabled'))
            ->andWhere($ex->eq('m.available', ':available'))
            ->orderBy('m.position', 'ASC');

        if ($currency) {
            $parameters['currency'] = $currency;
            $qb->andWhere($ex->orX(
                $qb->expr()->isMemberOf(':currency', 'm.currencies'),
                'm.currencies IS EMPTY'
            ));
        }

        return $qb
            ->getQuery()
            ->setParameters($parameters)
            ->getResult();
    }

    /**
     * @inheritDoc
     */
    public function findEnabled(CurrencyInterface $currency = null)
    {
        $qb = $this->createQueryBuilder('m');
        $ex = $qb->expr();

        $parameters = [
            'enabled'   => true,
        ];

        $qb
            ->andWhere($ex->eq('m.enabled', ':enabled'))
            ->orderBy('m.position', 'ASC');

        if ($currency) {
            $parameters['currency'] = $currency;
            $qb->andWhere($ex->orX(
                $qb->expr()->isMemberOf(':currency', 'm.currencies'),
                'm.currencies IS EMPTY'
            ));
        }

        return $qb
            ->getQuery()
            ->setParameters($parameters)
            ->getResult();
    }
}
{'fraction_non_alphanumeric': 0.15093293922039536, 'fraction_numerical': 0.0, 'mean_word_length': 2.104357798165138, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 72, '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': '4227383', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1488, 'n_words': 236}