<commit_before><?php
/**
 * BaconUser
 *
 * @link      http://github.com/Bacon/BaconUser For the canonical source repository
 * @copyright 2013 <NAME> 'DASPRiD'
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
 */

namespace BaconUser\Service;

use BaconUser\Entity\ResetPassword;
use BaconUser\Exception;
use BaconUser\Options\ResetPasswordOptionsInterface;
use DateTime;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Crypt\Utils;

/**
 * Service that allows to generate and verify reset passwords
 */
class ResetPasswordService implements EventManagerAwareInterface
{
    /**
     * Event names
     */
    const EVENT_RESET_PASSWORD_CREATED = 'resetPasswordCreated';

    /**
     * @var EventManagerInterface
     */
    protected $eventManager;

    /**
     * @var ObjectManager
     */
    protected $objectManager;

    /**
     * @var ObjectRepository
     */
    protected $resetPasswordRepository;

    /**
     * @var ResetPasswordOptionsInterface
     */
    protected $resetPasswordOptions;

    /**
     * @param ObjectManager                 $objectManager
     * @param ObjectRepository              $resetPasswordRepository
     * @param ResetPasswordOptionsInterface $resetPasswordOptions
     */
    public function __construct(
        ObjectManager $objectManager,
        ObjectRepository $resetPasswordRepository,
        ResetPasswordOptionsInterface $resetPasswordOptions
    ) {
        $this->objectManager           = $objectManager;
        $this->resetPasswordRepository = $resetPasswordRepository;
        $this->resetPasswordOptions    = $resetPasswordOptions;
    }

    /**
     * Create a new reset password request for a given email, and saves it to the database
     *
     * @param  string $email
     * @return ResetPassword
     */
    public function createResetPasswordRequest($email)
    {
        // We first check if a token already exists for the given mail, so that we can reuse it
        $resetPassword = $this->resetPasswordRepository->findOneBy(array('email' => $email));

        if (null === $resetPassword) {
            $resetPassword = new ResetPassword();
            $resetPassword->setEmail($email)
                          ->setToken(sha1(uniqid() . $email));
        }

        $now              = new DateTime();
        $validityInterval = $this->resetPasswordOptions->getTokenValidityInterval();

        $resetPassword->setExpirationDate($now->add($validityInterval));

        $this->objectManager->persist($resetPassword);
        $this->objectManager->flush();<commit_msg>Regenerate token if outdated<commit_after><?php
/**
 * BaconUser
 *
 * @link      http://github.com/Bacon/BaconUser For the canonical source repository
 * @copyright 2013 <NAME> 'DASPRiD'
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
 */

namespace BaconUser\Service;

use BaconUser\Entity\ResetPassword;
use BaconUser\Exception;
use BaconUser\Options\ResetPasswordOptionsInterface;
use DateTime;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Crypt\Utils;

/**
 * Service that allows to generate and verify reset passwords
 */
class ResetPasswordService implements EventManagerAwareInterface
{
    /**
     * Event names
     */
    const EVENT_RESET_PASSWORD_CREATED = 'resetPasswordCreated';

    /**
     * @var EventManagerInterface
     */
    protected $eventManager;

    /**
     * @var ObjectManager
     */
    protected $objectManager;

    /**
     * @var ObjectRepository
     */
    protected $resetPasswordRepository;

    /**
     * @var ResetPasswordOptionsInterface
     */
    protected $resetPasswordOptions;

    /**
     * @param ObjectManager                 $objectManager
     * @param ObjectRepository              $resetPasswordRepository
     * @param ResetPasswordOptionsInterface $resetPasswordOptions
     */
    public function __construct(
        ObjectManager $objectManager,
        ObjectRepository $resetPasswordRepository,
        ResetPasswordOptionsInterface $resetPasswordOptions
    ) {
        $this->objectManager           = $objectManager;
        $this->resetPasswordRepository = $resetPasswordRepository;
        $this->resetPasswordOptions    = $resetPasswordOptions;
    }

    /**
     * Create a new reset password request for a given email, and saves it to the database
     *
     * @param  string $email
     * @return ResetPassword
     */
    public function createResetPasswordRequest($email)
    {
        // We first check if a token already exists for the given mail, so that we can reuse it
        $resetPassword = $this->resetPasswordRepository->findOneBy(array('email' => $email));

        if (null === $resetPassword) {
            $resetPassword = new ResetPassword();
            $resetPassword->setEmail($email);
        }

        // If the token does not exist OR is expirated (which is the case when the same reset password
        // request is reused
        $now            = new DateTime();
        $expirationDate = $resetPassword->getExpirationDate();

        if (null === $expirationDate || $expirationDate < $now) {
            $resetPassword->setToken(sha1(uniqid() . $email));
        }

        $validityInterval = $this->resetPasswordOptions->getTokenValidityInterval();

        $resetPassword->setExpirationDate($now->add($validityInterval));

        $this->objectManager->persist($resetPassword);
        $this->objectManager->flush();
{'fraction_non_alphanumeric': 0.09833247378373731, 'fraction_numerical': 0.0020629190304280558, 'mean_word_length': 2.960517358747447, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 38, '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': '4731513', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1459, 'n_words': 350}