<commit_before><?php
/*
* (c) <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Usoft\IDealBundle\Driver;

use Mollie_API_Client;
use Mollie_API_Object_Method;
use Symfony\Component\Filesystem\Filesystem;
use Usoft\IDealBundle\Event\PaymentFailedEvent;
use Usoft\IDealBundle\Event\PaymentPlacedEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Usoft\IDealBundle\Event\PaymentSuccessEvent;
use Usoft\IDealBundle\Exceptions\RequestTokenNotFoundException;
use Usoft\IDealBundle\PaymentEvents;
use Usoft\IDealBundle\Model\Bank;
use Usoft\IDealBundle\Exceptions\BankLoaderException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Usoft\IDealBundle\Exceptions\IDealExecuteException;

/**
 * Class MollieDriver
 *
 * @author <NAME> <<EMAIL>>
 */
class MollieDriver implements IDealInterface
{
    /** @var Mollie_API_Client */
    private $mollie;

    /** @var RouterInterface */
    private $router;

    /** @var EventDispatcherInterface */
    private $eventDispatcher;

    /** @var Filesystem */
    private $filesystem;

    /** @var string */
    private $description;

    /**
     * @param Mollie_API_Client        $client
     * @param RouterInterface          $router
     * @param EventDispatcherInterface $eventDispatcher
     * @param Filesystem               $filesystem
     * @param string                   $key
     * @param string                   $description
     */
    public function __construct(
        Mollie_API_Client        $client,
        RouterInterface          $router,
        EventDispatcherInterface $eventDispatcher,
        Filesystem               $filesystem,
        $key,
        $description
    )
    {
        $this->description     = $description;
        $this->router          = $router;
        $this->eventDispatcher = $eventDispatcher;
        $this->filesystem      = $filesystem;

        $client->setApiKey($key);
        $this->mollie = $client;
    }

    /**
     * {@inheritdoc}
     */
    public function getBanks()
    {
        $bankList = [];
        try {
            $issuers = $this->mollie->issuers->all();
            foreach ($issuers as $issuer) {
                $bankList[] = new Bank($issuer->id, $issuer->name);
            }
            return $bankList;

        } catch (\Exception $exception) {
            throw new BankLoaderException('Cannot load issuers from mollie');
        }
    }

    /**
     * {@inheritdoc}
     */
    public function execute(Bank $bank, $amount, $routeName)
    {
        $token = md5(uniqid('<PASSWORD>_'));

        try {
            $redirectUrl = $this->router->generate($routeName, ['token' => $token], UrlGeneratorInterface::ABSOLUTE_URL);
        } catch (\Exception $exception) {
            throw new IDealExecuteException('Router not configured or does not exist');
        }

        try {
            $payment = $this->mollie->payments->create([
                'amount'      => $amount,
                'description' => $this->description,
                'redirectUrl' => $redirectUrl,
                'method'      => Mollie_API_Object_Method::IDEAL,
                'issuer'      => $bank->getId(),
            ]);

            $this->filesystem->dumpFile($this->getFilePath($token), $payment->id);
            $this->eventDispatcher->dispatch(PaymentEvents::PAYMENT_PLACED, new PaymentPlacedEvent($payment));

            return new RedirectResponse($payment->getPaymentUrl());

        } catch (\Exception $exception) {
            throw new IDealExecuteException($exception->getMessage());
        }
    }

    /**
     * {@inheritdoc}
     */
    public function confirm(Request $request)
    {
        $token = $request->get('token');

        if ($token == null) {
            throw new RequestTokenNotFoundException('The token is not passed in the url');
        }

        if (false === $this->filesystem->exists($this->getFilePath($token))) {
            throw new RequestTokenNotFoundException('The token file cannot be found');
        }

        try {
            $key = file_get_contents($this->getFilePath($token));
            $payment = $this->mollie->payments->get($key);
<commit_msg>Use strict type checking on null<commit_after><?php
/*
* (c) <NAME> <<EMAIL>>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Usoft\IDealBundle\Driver;

use Mollie_API_Client;
use Mollie_API_Object_Method;
use Symfony\Component\Filesystem\Filesystem;
use Usoft\IDealBundle\Event\PaymentFailedEvent;
use Usoft\IDealBundle\Event\PaymentPlacedEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Usoft\IDealBundle\Event\PaymentSuccessEvent;
use Usoft\IDealBundle\Exceptions\RequestTokenNotFoundException;
use Usoft\IDealBundle\PaymentEvents;
use Usoft\IDealBundle\Model\Bank;
use Usoft\IDealBundle\Exceptions\BankLoaderException;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Usoft\IDealBundle\Exceptions\IDealExecuteException;

/**
 * Class MollieDriver
 *
 * @author <NAME> <<EMAIL>>
 */
class MollieDriver implements IDealInterface
{
    /** @var Mollie_API_Client */
    private $mollie;

    /** @var RouterInterface */
    private $router;

    /** @var EventDispatcherInterface */
    private $eventDispatcher;

    /** @var Filesystem */
    private $filesystem;

    /** @var string */
    private $description;

    /**
     * @param Mollie_API_Client        $client
     * @param RouterInterface          $router
     * @param EventDispatcherInterface $eventDispatcher
     * @param Filesystem               $filesystem
     * @param string                   $key
     * @param string                   $description
     */
    public function __construct(
        Mollie_API_Client        $client,
        RouterInterface          $router,
        EventDispatcherInterface $eventDispatcher,
        Filesystem               $filesystem,
        $key,
        $description
    )
    {
        $this->description     = $description;
        $this->router          = $router;
        $this->eventDispatcher = $eventDispatcher;
        $this->filesystem      = $filesystem;

        $client->setApiKey($key);
        $this->mollie = $client;
    }

    /**
     * {@inheritdoc}
     */
    public function getBanks()
    {
        $bankList = [];
        try {
            $issuers = $this->mollie->issuers->all();
            foreach ($issuers as $issuer) {
                $bankList[] = new Bank($issuer->id, $issuer->name);
            }
            return $bankList;

        } catch (\Exception $exception) {
            throw new BankLoaderException('Cannot load issuers from mollie');
        }
    }

    /**
     * {@inheritdoc}
     */
    public function execute(Bank $bank, $amount, $routeName)
    {
        $token = md5(uniqid('<PASSWORD>_'));

        try {
            $redirectUrl = $this->router->generate($routeName, ['token' => $token], UrlGeneratorInterface::ABSOLUTE_URL);
        } catch (\Exception $exception) {
            throw new IDealExecuteException('Router not configured or does not exist');
        }

        try {
            $payment = $this->mollie->payments->create([
                'amount'      => $amount,
                'description' => $this->description,
                'redirectUrl' => $redirectUrl,
                'method'      => Mollie_API_Object_Method::IDEAL,
                'issuer'      => $bank->getId(),
            ]);

            $this->filesystem->dumpFile($this->getFilePath($token), $payment->id);
            $this->eventDispatcher->dispatch(PaymentEvents::PAYMENT_PLACED, new PaymentPlacedEvent($payment));

            return new RedirectResponse($payment->getPaymentUrl());

        } catch (\Exception $exception) {
            throw new IDealExecuteException($exception->getMessage());
        }
    }

    /**
     * {@inheritdoc}
     */
    public function confirm(Request $request)
    {
        $token = $request->get('token');

        if ($token === null) {
            throw new RequestTokenNotFoundException('The token is not passed in the url');
        }

        if (false === $this->filesystem->exists($this->getFilePath($token))) {
            throw new RequestTokenNotFoundException('The token file cannot be found');
        }

        try {
            $key = file_get_contents($this->getFilePath($token));
            $payment = $this->mollie->payments->get($key);

{'fraction_non_alphanumeric': 0.12099684258006314, 'fraction_numerical': 0.0002255299954894001, 'mean_word_length': 2.4164098613251155, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 97, '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': '2606552', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2433, 'n_words': 499}