Merge pull request #6770 from nupplaphil/issue/reuse_strings_random
Reuse existing Strings::getRandomHex function
This commit is contained in:
commit
c9ddbf247d
2 changed files with 3 additions and 28 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Friendica\Util\Logger;
|
namespace Friendica\Util\Logger;
|
||||||
|
|
||||||
|
use Friendica\Util\Strings;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,29 +35,7 @@ class WorkerLogger implements LoggerInterface
|
||||||
{
|
{
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->functionName = $functionName;
|
$this->functionName = $functionName;
|
||||||
$this->workerId = $this->generateWorkerId($idLength);
|
$this->workerId = Strings::getRandomHex($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 '';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -15,15 +15,11 @@ class WorkerLoggerTest extends MockedTest
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the a id with length zero
|
* Test the a id with length zero
|
||||||
* @expectedException
|
* @expectedException \Error
|
||||||
*/
|
*/
|
||||||
public function testGetWorkerIdZero()
|
public function testGetWorkerIdZero()
|
||||||
{
|
{
|
||||||
$logger = \Mockery::mock(LoggerInterface::class);
|
$logger = \Mockery::mock(LoggerInterface::class);
|
||||||
$logger
|
|
||||||
->shouldReceive('alert')
|
|
||||||
->with('id length must be greater than 0.')
|
|
||||||
->once();
|
|
||||||
new WorkerLogger($logger, 'test', 0);
|
new WorkerLogger($logger, 'test', 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue