added encryption, constructor property promotion

This commit is contained in:
tracer 2022-09-22 18:57:10 +02:00
parent 8ec1a2942d
commit 51b9e67ea9
1 changed files with 81 additions and 33 deletions

View File

@ -2,7 +2,11 @@
namespace App\Entity;
use App\Controller\ConfigController;
use App\Controller\EncryptionController;
use Exception;
use OpenApi\Attributes as OAT;
use SodiumException;
/**
*
@ -11,24 +15,60 @@ use OpenApi\Attributes as OAT;
class Nameserver
{
private int $id;
private String $name;
private String $a;
private String $aaaa;
private String $apikey;
public function __construct(String $name, int $id = 0, String $a = '', String $aaaa = '', String $apikey = '')
/**
* @param string $name
* @param int $id
* @param string $a
* @param string $aaaa
* @param string $passphrase
* @param string $apikey
* @param string $apikeyPrefix
*/
public function __construct(
private string $name,
private int $id = 0,
private string $a = '',
private string $aaaa = '',
private readonly string $passphrase = '',
private string $apikey = '',
private string $apikeyPrefix = '')
{
$this->id = $id;
$this->name = $name;
$this->a = $a;
$this->aaaa = $aaaa;
$this->apikey = $apikey;
}
if ($this->passphrase) {
$configController = new ConfigController();
$encryptionController = new EncryptionController();
$encryptionKey = $configController->getConfig(configKey: 'encryptionKey');
[$this->apikeyPrefix] = explode(separator: '.', string: $this->passphrase);
try {
$this->apikey = $encryptionController->safeEncrypt(message: $this->passphrase, key: $encryptionKey);
} catch (Exception|SodiumException $e) {
die($e->getMessage() . PHP_EOL);
}
}
}
/**
* @return string
*/
public function getApikeyPrefix(): string
{
return $this->apikeyPrefix;
}
/**
* @param string $apikeyPrefix
*/
public function setApikeyPrefix(string $apikeyPrefix): void
{
$this->apikeyPrefix = $apikeyPrefix;
}
/**
* @return String
* @return string
*/
#[OAT\Property(type: 'string')]
public function getA(): string
@ -37,7 +77,7 @@ class Nameserver
}
/**
* @param String $a
* @param string $a
*/
public function setA(string $a): void
{
@ -45,7 +85,7 @@ class Nameserver
}
/**
* @return String
* @return string
*/
#[OAT\Property(type: 'string')]
public function getAaaa(): string
@ -54,7 +94,7 @@ class Nameserver
}
/**
* @param String $aaaa
* @param string $aaaa
*/
public function setAaaa(string $aaaa): void
{
@ -62,7 +102,7 @@ class Nameserver
}
/**
* @return String
* @return string
*/
#[OAT\Property(type: 'string')]
public function getApikey(): string
@ -71,7 +111,7 @@ class Nameserver
}
/**
* @param String $apikey
* @param string $apikey
*/
public function setApikey(string $apikey): void
{
@ -96,7 +136,7 @@ class Nameserver
}
/**
* @return String
* @return string
*/
#[OAT\Property(type: 'string')]
public function getName(): string
@ -105,11 +145,19 @@ class Nameserver
}
/**
* @param String $name
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @return string
*/
public function getPassphrase(): string
{
return $this->passphrase;
}
}