Refactor response caching in AuthorizedSenderDomainController

In the previous implementation the getDomainRecords couldn't be used without
calling getAllSenderDomains or getVerifiedSenderDomains upfront.
[MAILPOET-4302]
This commit is contained in:
Rostislav Wolny
2022-08-04 15:40:26 +02:00
committed by Veljko V
parent e03e375942
commit 968cbb67d8

View File

@@ -19,7 +19,8 @@ class AuthorizedSenderDomainController {
/** @var DmarcPolicyChecker */ /** @var DmarcPolicyChecker */
private $dmarcPolicyChecker; private $dmarcPolicyChecker;
private $currentRecords; /** @var null|array Cached response for with authorized domains */
private $currentRecords = null;
public function __construct( public function __construct(
Bridge $bridge, Bridge $bridge,
@@ -30,13 +31,14 @@ class AuthorizedSenderDomainController {
} }
/** /**
* Get the most recent cached record of Bridge::getAuthorizedSenderDomains * Get record of Bridge::getAuthorizedSenderDomains
*/ */
public function getDomainRecords($domain = ''): array { public function getDomainRecords(string $domain = ''): array {
$records = $this->getAllRecords();
if ($domain) { if ($domain) {
return $this->currentRecords[$domain] ?? []; return $records[$domain] ?? [];
} }
return $this->currentRecords; return $records;
} }
/** /**
@@ -45,16 +47,14 @@ class AuthorizedSenderDomainController {
* Note: This includes both verified and unverified domains * Note: This includes both verified and unverified domains
*/ */
public function getAllSenderDomains(): array { public function getAllSenderDomains(): array {
$records = $this->currentRecords = $this->bridge->getAuthorizedSenderDomains(); return $this->returnAllDomains($this->getAllRecords());
return $this->returnAllDomains($records);
} }
/** /**
* Get all Verified Sender Domains * Get all Verified Sender Domains
*/ */
public function getVerifiedSenderDomains(): array { public function getVerifiedSenderDomains(): array {
$records = $this->currentRecords = $this->bridge->getAuthorizedSenderDomains(); return $this->returnVerifiedDomains($this->getAllRecords());
return $this->returnVerifiedDomains($records);
} }
/** /**
@@ -80,6 +80,9 @@ class AuthorizedSenderDomainController {
throw new \InvalidArgumentException($finalData['error']); throw new \InvalidArgumentException($finalData['error']);
} }
// Reset cached value since a new domain was added
$this->currentRecords = null;
return $finalData; return $finalData;
} }
@@ -176,4 +179,11 @@ class AuthorizedSenderDomainController {
return $verifiedDomains; return $verifiedDomains;
} }
private function getAllRecords(): array {
if ($this->currentRecords === null) {
$this->currentRecords = $this->bridge->getAuthorizedSenderDomains();
}
return $this->currentRecords;
}
} }