]> git.mxchange.org Git - friendica.git/commitdiff
Reuse existing Strings::getRandomHex function
authorPhilipp Holzer <admin@philipp.info>
Wed, 27 Feb 2019 17:32:25 +0000 (18:32 +0100)
committerPhilipp Holzer <admin@philipp.info>
Wed, 27 Feb 2019 17:32:25 +0000 (18:32 +0100)
src/Util/Logger/WorkerLogger.php
tests/src/Util/Logger/WorkerLoggerTest.php

index a9295834d9b218951a3583c3aa3921747158365e..be7f82a9f4f4207e761247ef6ee2196fc5a2adfe 100644 (file)
@@ -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);
        }
 
        /**
index 0713f64d0b8179f5844c075658b6f8ece45f5d63..7676b01e5b745a8284e2830e87d6d7cea72d77a6 100644 (file)
@@ -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);
        }