From 6d5d2a550e23e3ae3bb6707af6abcc137ae694a3 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 8 Jan 2021 00:10:17 -0800 Subject: [PATCH] (REF) CryptoToken - Allow optional injection of $registry --- Civi/Crypto/CryptoToken.php | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/Civi/Crypto/CryptoToken.php b/Civi/Crypto/CryptoToken.php index 9ee7375ac6..316159a3e4 100644 --- a/Civi/Crypto/CryptoToken.php +++ b/Civi/Crypto/CryptoToken.php @@ -55,11 +55,19 @@ class CryptoToken { */ protected $delim; + /** + * @var \Civi\Crypto\CryptoRegistry|null + */ + private $registry; + /** * CryptoToken constructor. + * + * @param CryptoRegistry $registry */ - public function __construct() { + public function __construct($registry = NULL) { $this->delim = chr(2); + $this->registry = $registry; } /** @@ -85,7 +93,7 @@ class CryptoToken { */ public function encrypt($plainText, $keyIdOrTag) { /** @var CryptoRegistry $registry */ - $registry = \Civi::service('crypto.registry'); + $registry = $this->getRegistry(); $key = $registry->findKey($keyIdOrTag); if ($key['suite'] === 'plain') { @@ -128,7 +136,7 @@ class CryptoToken { } /** @var CryptoRegistry $registry */ - $registry = \Civi::service('crypto.registry'); + $registry = $this->getRegistry(); $tokenData = $this->parse($token); @@ -156,7 +164,7 @@ class CryptoToken { */ public function rekey($oldToken, $keyTag) { /** @var \Civi\Crypto\CryptoRegistry $registry */ - $registry = \Civi::service('crypto.registry'); + $registry = $this->getRegistry(); $sourceKeys = $registry->findKeysByTag($keyTag); $targetKey = array_shift($sourceKeys); @@ -200,4 +208,14 @@ class CryptoToken { return $tokenData; } + /** + * @return CryptoRegistry + */ + protected function getRegistry(): CryptoRegistry { + if ($this->registry === NULL) { + $this->registry = \Civi::service('crypto.registry'); + } + return $this->registry; + } + } -- 2.25.1