<commit_before><?php

namespace Looptribe\Paytoshi\Logic;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionException;
use Looptribe\Paytoshi\Api\PaytoshiApiInterface;
use Looptribe\Paytoshi\Captcha\CaptchaProviderInterface;
use Looptribe\Paytoshi\Model\Payout;
use Looptribe\Paytoshi\Model\Recipient;
use Looptribe\Paytoshi\Model\RecipientRepository;

class RewardLogic
{
    /** @var Connection */
    private $connection;

    /** @var RecipientRepository */
    private $recipientRepository;

    /** @var RewardProviderInterface */
    private $rewardProvider;

    /** @var PaytoshiApiInterface */
    private $api;

    /** @var IntervalEnforcerInterface */
    private $intervalEnforcer;

    /** @var CaptchaProviderInterface */
    private $captchaProvider;

    public function __construct(
        Connection $connection,
        RecipientRepository $recipientRepository,
        RewardProviderInterface $rewardProvider,
        PaytoshiApiInterface $api,
        IntervalEnforcerInterface $intervalEnforcer,
        CaptchaProviderInterface $captchaProvider
    ) {
        $this->connection = $connection;
        $this->recipientRepository = $recipientRepository;
        $this->rewardProvider = $rewardProvider;
        $this->api = $api;
        $this->intervalEnforcer = $intervalEnforcer;
        $this->captchaProvider = $captchaProvider;
    }

    /**
     * @param string $address
     * @param string $ip
     * @param string $challenge
     * @param string $response
     * @param string|null $referralAddress
     * @return Payout
     * @throws ConnectionException
     * @throws \Exception
     */
    public function create($address, $ip, $challenge, $response, $referralAddress = null)
    {
        // TODO: Captcha Check
        //

        $this->connection->beginTransaction();
        try {

            $recipient = $this->recipientRepository->findOneByAddress($address);
            if (!$recipient) {
                $recipient = new Recipient();
                $recipient->setAddress($address);
            }

            // Waiting interval check
            $interval = $this->intervalEnforcer->check($ip, $recipient);
            if ($interval) {
                throw new \Exception(
                    sprintf('You can get a reward again in %s.', $interval->format('%Hh, %Im, %Ss'))
                );
            }

            // Reward generation
            $earning = $this->rewardProvider->getReward();

            // Payout creation
            $payout = new Payout();
            $payout->setIp($ip);
            $payout->setRecipientAddress($recipient->getAddress());
            $payout->setEarning($earning);

            // TODO: payment process

            //$this->connection->commit();
            return $payout;

        } catch (\Exception $e) {
            $this->connection->rollBack();
            throw $e;
        }
    }
}<commit_msg>Captcha check added to RewardLogic. refs #12.<commit_after><?php

namespace Looptribe\Paytoshi\Logic;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionException;
use Looptribe\Paytoshi\Api\PaytoshiApiInterface;
use Looptribe\Paytoshi\Captcha\CaptchaProviderException;
use Looptribe\Paytoshi\Captcha\CaptchaProviderInterface;
use Looptribe\Paytoshi\Model\Payout;
use Looptribe\Paytoshi\Model\Recipient;
use Looptribe\Paytoshi\Model\RecipientRepository;

class RewardLogic
{
    /** @var Connection */
    private $connection;

    /** @var RecipientRepository */
    private $recipientRepository;

    /** @var RewardProviderInterface */
    private $rewardProvider;

    /** @var PaytoshiApiInterface */
    private $api;

    /** @var IntervalEnforcerInterface */
    private $intervalEnforcer;

    /** @var CaptchaProviderInterface */
    private $captchaProvider;

    public function __construct(
        Connection $connection,
        RecipientRepository $recipientRepository,
        RewardProviderInterface $rewardProvider,
        PaytoshiApiInterface $api,
        IntervalEnforcerInterface $intervalEnforcer,
        CaptchaProviderInterface $captchaProvider
    ) {
        $this->connection = $connection;
        $this->recipientRepository = $recipientRepository;
        $this->rewardProvider = $rewardProvider;
        $this->api = $api;
        $this->intervalEnforcer = $intervalEnforcer;
        $this->captchaProvider = $captchaProvider;
    }

    /**
     * @param string $address
     * @param string $ip
     * @param string $challenge
     * @param string $response
     * @param string|null $referralAddress
     * @return Payout
     * @throws ConnectionException
     * @throws \Exception
     */
    public function create($address, $ip, $challenge, $response, $referralAddress = null)
    {
        try {
            $options = array (
                'challenge' => $challenge,
                'response' => $response,
                'ip' => $ip
            );
            $captchaResponse = $this->captchaProvider->checkAnswer($options);
        }
        catch (CaptchaProviderException $e) {
            throw new \Exception(
                sprintf('Captcha error: %s', $e->getMessage())
            );
        }

        if (!$captchaResponse->isSuccessful()) {
            throw new \Exception($captchaResponse->getMessage());
        }

        $this->connection->beginTransaction();
        try {

            $recipient = $this->recipientRepository->findOneByAddress($address);
            if (!$recipient) {
                $recipient = new Recipient();
                $recipient->setAddress($address);
            }

            // Waiting interval check
            $interval = $this->intervalEnforcer->check($ip, $recipient);
            if ($interval) {
                throw new \Exception(
                    sprintf('You can get a reward again in %s.', $interval->format('%Hh, %Im, %Ss'))
                );
            }

            // Reward generation
            $earning = $this->rewardProvider->getReward();

            // Payout creation
            $payout = new Payout();
            $payout->setIp($ip);
            $payout->setRecipientAddress($recipient->getAddress());
            $payout->setEarning($earning);

            // TODO: payment process

            //$this->connection->commit();
            return $payout;

        } catch (\Exception $e) {
            $this->connection->rollBack();
            throw $e;
        }
    }
}
{'fraction_non_alphanumeric': 0.11740009329808739, 'fraction_numerical': 0.00031099362463069506, 'mean_word_length': 2.2305374183827222, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, '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': '6429692', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1732, 'n_words': 347}