From: Philipp Holzer Date: Wed, 27 Feb 2019 17:32:25 +0000 (+0100) Subject: Reuse existing Strings::getRandomHex function X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=2b537840941634d561ba3eed2215aedca7897471;p=friendica.git Reuse existing Strings::getRandomHex function --- diff --git a/src/Util/Logger/WorkerLogger.php b/src/Util/Logger/WorkerLogger.php index a9295834d9..be7f82a9f4 100644 --- a/src/Util/Logger/WorkerLogger.php +++ b/src/Util/Logger/WorkerLogger.php @@ -2,6 +2,7 @@ namespace Friendica\Util\Logger; +use Friendica\Util\Strings; use Psr\Log\LoggerInterface; /** @@ -34,29 +35,7 @@ class WorkerLogger implements LoggerInterface { $this->logger = $logger; $this->functionName = $functionName; - $this->workerId = $this->generateWorkerId($idLength); - } - - /** - * Generates an ID - * - * @param int $length - * - * @return string - */ - private function generateWorkerId($length) - { - if ($length <= 0) { - $this->logger->alert('id length must be greater than 0.'); - return ''; - } - - try { - return substr(bin2hex(random_bytes(ceil($length / 2))), 0, $length); - } catch (\Exception $exception) { - $this->logger->alert('random_bytes threw an error', ['exception' => $exception]); - return ''; - } + $this->workerId = Strings::getRandomHex($idLength); } /** diff --git a/tests/src/Util/Logger/WorkerLoggerTest.php b/tests/src/Util/Logger/WorkerLoggerTest.php index 0713f64d0b..7676b01e5b 100644 --- a/tests/src/Util/Logger/WorkerLoggerTest.php +++ b/tests/src/Util/Logger/WorkerLoggerTest.php @@ -15,15 +15,11 @@ class WorkerLoggerTest extends MockedTest /** * Test the a id with length zero - * @expectedException + * @expectedException \Error */ public function testGetWorkerIdZero() { $logger = \Mockery::mock(LoggerInterface::class); - $logger - ->shouldReceive('alert') - ->with('id length must be greater than 0.') - ->once(); new WorkerLogger($logger, 'test', 0); }