initial commit
This commit is contained in:
commit
0db3e92ee6
59 changed files with 3384 additions and 0 deletions
82
src/DomainObjects/Token.php
Normal file
82
src/DomainObjects/Token.php
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace VeruA\DomainObjects;
|
||||
|
||||
use VeruA\DomainObjects\{
|
||||
DomainObject,
|
||||
Owner
|
||||
};
|
||||
use VeruA\DomainObjects\ValueObjects\{
|
||||
Key,
|
||||
IntKey,
|
||||
Varchar,
|
||||
DateTime
|
||||
};
|
||||
|
||||
/**
|
||||
* A Token DomainObject
|
||||
*
|
||||
* @property ValueObjects\Intkey $ownerId The pk of the Owner the token belongs to
|
||||
* @property Owner $owner The Owner of the token
|
||||
* @property ValueObjects\Varchar $string The token string
|
||||
* @property ValueObjects\DateTime $creationTime The script or binary
|
||||
*
|
||||
* @author norb
|
||||
*/
|
||||
class Token extends DomainObject
|
||||
{
|
||||
use \VeruA\Log;
|
||||
|
||||
public function __construct($data=null)
|
||||
{
|
||||
parent::__construct($data);
|
||||
|
||||
if ($data instanceof Key)
|
||||
{
|
||||
$this->set('idOwner', $data);
|
||||
$this->markNew();
|
||||
$this->createToken();
|
||||
}
|
||||
|
||||
$this->log()->debug("Values after instantiation", $this->data);
|
||||
}
|
||||
|
||||
protected function fields(array ...$superFields): array
|
||||
{
|
||||
return parent::fields([
|
||||
'id' => IntKey::class,
|
||||
'idOwner' => IntKey::class,
|
||||
'owner' => Owner::class,
|
||||
'string' => Varchar::class,
|
||||
'creationTime' => DateTime::class,
|
||||
], ...$superFields);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random generated token consisting of letters and numbers
|
||||
*
|
||||
* @param int $length The character length of the token
|
||||
* @return string The token
|
||||
*/
|
||||
public function createToken(int $length = 64): string
|
||||
{
|
||||
$this->string = bin2hex(random_bytes($length/2));
|
||||
$this->creationTime = new \DateTimeImmutable();
|
||||
return $this->string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the token is still valid
|
||||
*
|
||||
* @return true if creationTime not older than 14 days
|
||||
*/
|
||||
public function good(): bool
|
||||
{
|
||||
return ($this->creationTime->value()->diff(new \DateTimeImmutable())->days < 14);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* jEdit buffer local properties {{{
|
||||
* :folding=explicit:collapseFolds=1:
|
||||
}}}*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue