<commit_before><?php
namespace HtUserRegistration\Service;

use HtUserRegistration\Mapper\UserRegistrationMapperInterface;
use Zend\EventManager\EventInterface;
use ZfcUser\Entity\UserInterface;
use ZfcBase\EventManager\EventProvider;
use DateTime;
use HtUserRegistration\Entity\UserRegistrationInterface;
use Zend\Crypt\Password\Bcrypt;

class UserRegistrationService extends EventProvider implements UserRegistrationServiceInterface
{
    /**
     * @var UserRegistrationMapperInterface
     */
    protected $userRegistrationMapper;

    /**
     * @var \HtUserRegistration\Options\ModuleOptions
     */
    protected $moduleOptions;

    /**
     * @var \ZfcUser\Options\ModuleOptions
     */
    protected $zfcUserOptions;

    /**
     * @var \ZfcUser\Mapper\UserInterface
     */
    protected $userMapper;

    use \Zend\ServiceManager\ServiceLocatorAwareTrait;

    /**
     * {@inheritDoc}
     */
    public function onUserRegistration(EventInterface $e)
    {
        $user = $e->getParam('user');
        if ($this->getOptions()->getSendVerificationEmail() && $this->getZfcUserOptions()->getEnableRegistration()) {
            $this->sendVerificationEmail($user);
        } elseif ($this->getOptions()->getSendPasswordRequestEmail() && !$this->getZfcUserOptions()->getEnableRegistration()) {
            $this->sendPasswordRequestEmail($user);
        }

        // do nothing
    }

    /**
     * {@inheritDoc}
     */
    public function sendVerificationEmail(UserInterface $user)
    {
        $registrationRecord = $this->createRegistrationRecord($user);
        $mailer = $this->getServiceLocator()->get('HtUserRegistration\Mailer\Mailer');
        $mailer->sendVerificationEmail($registrationRecord);

    }

    /**
     * {@inheritDoc}
     */
    public function sendPasswordRequestEmail(UserInterface $user)
    {
        $registrationRecord = $this->createRegistrationRecord($user);
        $mailer = $this->getServiceLocator()->get('HtUserRegistration\Mailer\Mailer');
        $mailer->sendPasswordRequestEmail($registrationRecord);
    }

    /**
     * Stored user registration record to database
     *
     * @return UserInterface             $user
     * @return UserRegistrationInterface
     */
    protected function createRegistrationRecord(UserInterface $user)
    {
        $entityClass = $this->getOptions()->getRegistrationEntityClass();
        $entity = new $entityClass($user);
        $this->getEventManager()->trigger(__METHOD__, $this, array('user' => $user, 'record' => $entity));
        $entity->generateToken();
        $this->getUserRegistrationMapper()->insert($entity);
        $this->getEventManager()->trigger(__METHOD__ . '.post', $this, array('user' => $user, 'record' => $entity));

        return $entity;
    }

    /**
     * {@inheritDoc}
     */
    public function verifyEmail(UserInterface $user, $token)
    {
        $record = $this->getUserRegistrationMapper()->findByUser($user);
        $this->getEventManager()->trigger(__METHOD__, $this, array('user' => $user, 'token' => $token, 'record' => $record));

        if (!$record || !$this->isTokenValid($user, $token, $record)) {
            return false;
        }
        if (!$record->isResponded()) {
            $record->setResponded(UserRegistrationInterface::EMAIL_RESPONDED);
            $this->getUserRegistrationMapper()->update($record);
        }

        $this->getEventManager()->trigger(__METHOD__ . '.post', $this, array('user' => $user, 'token' => $token, 'record' => $record));

        return true;
    }

    /**
     * {@inheritDoc}
     */
    public function isTokenValid(UserInterface $user, $token, UserRegistrationInterface $record)
    {
        if ($record->getToken() !== $token) {
            $this->getEventManager()->trigger('tokenInvalid', $this, array('user' => $user, 'token' => $token, 'record' => $record));

            return false;
        } elseif ($this->getOptions()->getEnableRequestExpiry() && $this->isTokenExpired($record)) {
            $this->getEventManager()->trigger('tokenExpired', $this, array('user' => $user, 'token' => $token, 'record' => $record));

            return false;
        }
        $this->getEventManager()->trigger('tokenValid', $this, array('user' => $user, 'token' => $token, 'record' => $record));

        return true;
    }

    /**
     * {@inheritDoc}
     */
    public function isTokenExpired(UserRegistrationInterface $record)
    {
        $expiryDate = new DateTime($this->getOptions()->getRequestExpiry() . ' seconds ago');

        return $record->getRequestTime() < $expiryDate;<commit_msg>Changed __METHOD__ to __FUNCTION__<commit_after><?php
namespace HtUserRegistration\Service;

use HtUserRegistration\Mapper\UserRegistrationMapperInterface;
use Zend\EventManager\EventInterface;
use ZfcUser\Entity\UserInterface;
use ZfcBase\EventManager\EventProvider;
use DateTime;
use HtUserRegistration\Entity\UserRegistrationInterface;
use Zend\Crypt\Password\Bcrypt;

class UserRegistrationService extends EventProvider implements UserRegistrationServiceInterface
{
    /**
     * @var UserRegistrationMapperInterface
     */
    protected $userRegistrationMapper;

    /**
     * @var \HtUserRegistration\Options\ModuleOptions
     */
    protected $moduleOptions;

    /**
     * @var \ZfcUser\Options\ModuleOptions
     */
    protected $zfcUserOptions;

    /**
     * @var \ZfcUser\Mapper\UserInterface
     */
    protected $userMapper;

    use \Zend\ServiceManager\ServiceLocatorAwareTrait;

    /**
     * {@inheritDoc}
     */
    public function onUserRegistration(EventInterface $e)
    {
        $user = $e->getParam('user');
        if ($this->getOptions()->getSendVerificationEmail() && $this->getZfcUserOptions()->getEnableRegistration()) {
            $this->sendVerificationEmail($user);
        } elseif ($this->getOptions()->getSendPasswordRequestEmail() && !$this->getZfcUserOptions()->getEnableRegistration()) {
            $this->sendPasswordRequestEmail($user);
        }

        // do nothing
    }

    /**
     * {@inheritDoc}
     */
    public function sendVerificationEmail(UserInterface $user)
    {
        $registrationRecord = $this->createRegistrationRecord($user);
        $mailer = $this->getServiceLocator()->get('HtUserRegistration\Mailer\Mailer');
        $mailer->sendVerificationEmail($registrationRecord);

    }

    /**
     * {@inheritDoc}
     */
    public function sendPasswordRequestEmail(UserInterface $user)
    {
        $registrationRecord = $this->createRegistrationRecord($user);
        $mailer = $this->getServiceLocator()->get('HtUserRegistration\Mailer\Mailer');
        $mailer->sendPasswordRequestEmail($registrationRecord);
    }

    /**
     * Stored user registration record to database
     *
     * @return UserInterface             $user
     * @return UserRegistrationInterface
     */
    protected function createRegistrationRecord(UserInterface $user)
    {
        $entityClass = $this->getOptions()->getRegistrationEntityClass();
        $entity = new $entityClass($user);
        $this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $user, 'record' => $entity));
        $entity->generateToken();
        $this->getUserRegistrationMapper()->insert($entity);
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array('user' => $user, 'record' => $entity));

        return $entity;
    }

    /**
     * {@inheritDoc}
     */
    public function verifyEmail(UserInterface $user, $token)
    {
        $record = $this->getUserRegistrationMapper()->findByUser($user);
        $this->getEventManager()->trigger(__FUNCTION__, $this, array('user' => $user, 'token' => $token, 'record' => $record));

        if (!$record || !$this->isTokenValid($user, $token, $record)) {
            return false;
        }
        if (!$record->isResponded()) {
            $record->setResponded(UserRegistrationInterface::EMAIL_RESPONDED);
            $this->getUserRegistrationMapper()->update($record);
        }

        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, array('user' => $user, 'token' => $token, 'record' => $record));

        return true;
    }

    /**
     * {@inheritDoc}
     */
    public function isTokenValid(UserInterface $user, $token, UserRegistrationInterface $record)
    {
        if ($record->getToken() !== $token) {
            $this->getEventManager()->trigger('tokenInvalid', $this, array('user' => $user, 'token' => $token, 'record' => $record));

            return false;
        } elseif ($this->getOptions()->getEnableRequestExpiry() && $this->isTokenExpired($record)) {
            $this->getEventManager()->trigger('tokenExpired', $this, array('user' => $user, 'token' => $token, 'record' => $record));

            return false;
        }
        $this->getEventManager()->trigger('tokenValid', $this, array('user' => $user, 'token' => $token, 'record' => $record));

        return true;
    }

    /**
     * {@inheritDoc}
     */
    public function isTokenExpired(UserRegistrationInterface $record)
    {
        $expiryDate = new DateTime($this->getOptions()->getRequestExpiry() . ' seconds ago');

        return $record->getRequestTime() < $expiryDate;
{'fraction_non_alphanumeric': 0.15431361909929464, 'fraction_numerical': 0.0, 'mean_word_length': 3.1929026387625115, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 145, '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': '2125174', 'n_tokens_mistral': 2558, 'n_tokens_neox': 2500, 'n_words': 422}