<?php 

namespace NPS\Model;

use DateTime;
use NPS\Model\User;
use NPS\Entity\AbstractTokenEntity;

/**
 * Represents a token model object
 * @author Lukas Korinek 
 */
abstract class AbstractToken implements IDomainObject
{

    /** @var AbstractTokenEntity $entity */
    protected $entity;

    /** @var int $ttl */
    protected $ttl;

    public function __construct(AbstractTokenEntity $entity, $ttl)
    {
        $this->entity = $entity;
        $this->ttl = $ttl;
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->entity->getId();
    }
    
    /**
     * @inheritdoc
     */
    public function getEntity()
    {
        return $this->entity;
    }

    /**
     * Checks if the token has been used already
     * @return bool
     */
    public function isUsed()
    {
        return $this->entity->used;
    }

    /**
     * Checks if the token is valid
     * @return bool
     */
    public function isValid()
    {
        return !$this->isUsed() && (($this->entity->dateCreated->getTimestamp() + $this->ttl) - time() > 0);
    }

    /**
     * Marks the reset token as used
     */
    public function markAsUsed()
    {
        $this->entity->used = true;
    }


}

