feature/RVRNEXT-2-implement-login-to-old-rvr #6
							
								
								
									
										105
									
								
								src/PersistentData/Model/OAuthToken.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										105
									
								
								src/PersistentData/Model/OAuthToken.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,105 @@
 | 
			
		||||
<?php namespace RVR\PersistentData\Model;
 | 
			
		||||
 | 
			
		||||
use DateTime;
 | 
			
		||||
use SokoWeb\PersistentData\Model\Model;
 | 
			
		||||
 | 
			
		||||
class OAuthToken extends Model
 | 
			
		||||
{
 | 
			
		||||
    protected static string $table = 'oauth_tokens';
 | 
			
		||||
 | 
			
		||||
    protected static array $fields = ['nonce', 'user_id', 'code', 'created', 'expires'];
 | 
			
		||||
 | 
			
		||||
    protected static array $relations = ['user' => User::class];
 | 
			
		||||
 | 
			
		||||
    private string $nonce = '';
 | 
			
		||||
 | 
			
		||||
    private ?User $user = null;
 | 
			
		||||
 | 
			
		||||
    private ?int $userId = null;
 | 
			
		||||
 | 
			
		||||
    private string $code = '';
 | 
			
		||||
 | 
			
		||||
    private DateTime $created;
 | 
			
		||||
 | 
			
		||||
    private DateTime $expires;
 | 
			
		||||
 | 
			
		||||
    public function setNonce(string $nonce): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->nonce = $nonce;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setUser(User $user): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->user = $user;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setUserId(int $userId): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->userId = $userId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setCode(string $code): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->code = $code;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setCreatedDate(DateTime $created): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->created = $created;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setExpiresDate(DateTime $expires): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->expires = $expires;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setCreated(string $created): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->created = new DateTime($created);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setExpires(string $expires): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->expires = new DateTime($expires);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getNonce(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->nonce;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getUser(): ?User
 | 
			
		||||
    {
 | 
			
		||||
        return $this->user;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getUserId(): ?int
 | 
			
		||||
    {
 | 
			
		||||
        return $this->userId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getCode(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->code;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getCreatedDate(): DateTime
 | 
			
		||||
    {
 | 
			
		||||
        return $this->created;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getCreated(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->created->format('Y-m-d H:i:s');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getExpiresDate(): DateTime
 | 
			
		||||
    {
 | 
			
		||||
        return $this->expires;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getExpires(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->expires->format('Y-m-d H:i:s');
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								src/Repository/OAuthTokenRepository.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/Repository/OAuthTokenRepository.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
			
		||||
<?php namespace RVR\Repository;
 | 
			
		||||
 | 
			
		||||
use DateTime;
 | 
			
		||||
use Generator;
 | 
			
		||||
use SokoWeb\Database\Query\Select;
 | 
			
		||||
use RVR\PersistentData\Model\OAuthToken;
 | 
			
		||||
use SokoWeb\PersistentData\PersistentDataManager;
 | 
			
		||||
 | 
			
		||||
class OAuthTokenRepository
 | 
			
		||||
{
 | 
			
		||||
    private PersistentDataManager $pdm;
 | 
			
		||||
 | 
			
		||||
    public function __construct()
 | 
			
		||||
    {
 | 
			
		||||
        $this->pdm = new PersistentDataManager();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getById(int $id): ?OAuthToken
 | 
			
		||||
    {
 | 
			
		||||
        return $this->pdm->selectFromDbById($id, OAuthToken::class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getByCode(string $code): ?OAuthToken
 | 
			
		||||
    {
 | 
			
		||||
        $select = new Select(\Container::$dbConnection);
 | 
			
		||||
        $select->where('code', '=', $code);
 | 
			
		||||
 | 
			
		||||
        return $this->pdm->selectFromDb($select, OAuthToken::class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function getAllExpired(): Generator
 | 
			
		||||
    {
 | 
			
		||||
        $select = new Select(\Container::$dbConnection);
 | 
			
		||||
        $select->where('expires', '<', (new DateTime())->format('Y-m-d H:i:s'));
 | 
			
		||||
 | 
			
		||||
        yield from $this->pdm->selectMultipleFromDb($select, OAuthToken::class);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user