Skip to content

Commit

Permalink
refactor: clean up unnecessary exceptions and bytes logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Oct 20, 2022
1 parent f0c5bbc commit 2cb824a
Show file tree
Hide file tree
Showing 25 changed files with 145 additions and 522 deletions.
26 changes: 0 additions & 26 deletions src/Exception/InvalidCacheKey.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/Exception/MacAddressNotFound.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/Exception/RandomSourceNotFound.php

This file was deleted.

14 changes: 1 addition & 13 deletions src/Service/Counter/RandomCounter.php
Expand Up @@ -16,9 +16,6 @@

namespace Ramsey\Identifier\Service\Counter;

use Ramsey\Identifier\Exception\RandomSourceNotFound;
use Throwable;

use function random_int;

use const PHP_INT_MAX;
Expand All @@ -31,17 +28,8 @@
*/
final class RandomCounter implements Counter
{
/**
* @throws RandomSourceNotFound
*/
public function next(): int
{
try {
return random_int(0, PHP_INT_MAX);
// @codeCoverageIgnoreStart
} catch (Throwable $exception) {
throw new RandomSourceNotFound('Cannot find an appropriate source of randomness', 0, $exception);
// @codeCoverageIgnoreEnd
}
return random_int(0, PHP_INT_MAX);
}
}
70 changes: 22 additions & 48 deletions src/Service/Dce/SystemDce.php
Expand Up @@ -17,9 +17,7 @@
namespace Ramsey\Identifier\Service\Dce;

use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\InvalidArgumentException as CacheInvalidArgumentException;
use Ramsey\Identifier\Exception\DceIdentifierNotFound;
use Ramsey\Identifier\Exception\InvalidCacheKey;
use Ramsey\Identifier\Service\Os\Os;
use Ramsey\Identifier\Service\Os\PhpOs;

Expand All @@ -45,12 +43,12 @@ final class SystemDce implements Dce
/**
* Key to use when caching the GID value in a PSR-16 cache instance
*/
private const GID_CACHE_KEY = self::class . '::$groupId';
private const GID_CACHE_KEY = '__ramsey_identifier_27a5';

/**
* Key to use when caching the UID value in a PSR-16 cache instance
*/
private const UID_CACHE_KEY = self::class . '::$userId';
private const UID_CACHE_KEY = '__ramsey_identifier_690f';

/**
* @var int<0, max> | null
Expand Down Expand Up @@ -80,8 +78,6 @@ public function __construct(

/**
* @throws DceIdentifierNotFound if unable to obtain a system group ID
* @throws InvalidCacheKey if a problem occurs when fetching data from the
* PSR-16 cache instance, if provided
*/
public function groupId(): int
{
Expand Down Expand Up @@ -119,8 +115,6 @@ public function orgId(): int

/**
* @throws DceIdentifierNotFound if unable to obtain a system user ID
* @throws InvalidCacheKey if a problem occurs when fetching data from the
* PSR-16 cache instance, if provided
*/
public function userId(): int
{
Expand Down Expand Up @@ -176,29 +170,19 @@ private function getSystemGid(): ?int

/**
* @return int<0, max> | null
*
* @throws InvalidCacheKey
*/
private function getSystemGidFromCache(): ?int
{
try {
/**
* The return value of -1 is useful for testing purposes.
*
* @var int<-1, max> | null $gid
*/
$gid = $this->cache?->get(self::GID_CACHE_KEY);

if ($gid === null) {
$gid = $this->getSystemGid();
$this->cache?->set(self::GID_CACHE_KEY, $gid);
}
} catch (CacheInvalidArgumentException $exception) {
throw new InvalidCacheKey(
sprintf('A problem occurred when attempting to use the cache key "%s"', self::GID_CACHE_KEY),
$exception->getCode(),
$exception,
);
/**
* The return value of -1 is useful for testing purposes.
*
* @var int<-1, max> | null $gid
*/
$gid = $this->cache?->get(self::GID_CACHE_KEY);

if ($gid === null) {
$gid = $this->getSystemGid();
$this->cache?->set(self::GID_CACHE_KEY, $gid);
}

return $gid >= 0 ? $gid : null;
Expand All @@ -217,29 +201,19 @@ private function getSystemUid(): ?int

/**
* @return int<0, max> | null
*
* @throws InvalidCacheKey
*/
private function getSystemUidFromCache(): ?int
{
try {
/**
* The return value of -1 is useful for testing purposes.
*
* @var int<-1, max> | null $uid
*/
$uid = $this->cache?->get(self::UID_CACHE_KEY);

if ($uid === null) {
$uid = $this->getSystemUid();
$this->cache?->set(self::UID_CACHE_KEY, $uid);
}
} catch (CacheInvalidArgumentException $exception) {
throw new InvalidCacheKey(
sprintf('A problem occurred when attempting to use the cache key "%s"', self::UID_CACHE_KEY),
$exception->getCode(),
$exception,
);
/**
* The return value of -1 is useful for testing purposes.
*
* @var int<-1, max> | null $uid
*/
$uid = $this->cache?->get(self::UID_CACHE_KEY);

if ($uid === null) {
$uid = $this->getSystemUid();
$this->cache?->set(self::UID_CACHE_KEY, $uid);
}

return $uid >= 0 ? $uid : null;
Expand Down
53 changes: 0 additions & 53 deletions src/Service/Nic/FallbackNic.php

This file was deleted.

4 changes: 0 additions & 4 deletions src/Service/Nic/Nic.php
Expand Up @@ -16,8 +16,6 @@

namespace Ramsey\Identifier\Service\Nic;

use Ramsey\Identifier\Exception\MacAddressNotFound;

/**
* Defines a network interface controller (NIC) for obtaining a MAC address
*
Expand All @@ -35,8 +33,6 @@ interface Nic
* @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.5 Node IDs that Do Not Identify the Host
*
* @return non-empty-string
*
* @throws MacAddressNotFound MUST throw if a MAC address is not available
*/
public function address(): string;
}
25 changes: 3 additions & 22 deletions src/Service/Nic/RandomNic.php
Expand Up @@ -16,13 +16,8 @@

namespace Ramsey\Identifier\Service\Nic;

use Ramsey\Identifier\Exception\RandomSourceNotFound;
use Throwable;

use function bin2hex;
use function pack;
use function random_bytes;
use function unpack;
use function random_int;
use function sprintf;

/**
* A NIC that generates a random MAC address and sets the multicast bit,
Expand All @@ -32,23 +27,9 @@
*/
final class RandomNic implements Nic
{
/**
* @throws RandomSourceNotFound
*/
public function address(): string
{
try {
$bytes = random_bytes(6);
// @codeCoverageIgnoreStart
} catch (Throwable $exception) {
throw new RandomSourceNotFound('Unable to find an appropriate source of randomness', 0, $exception);
// @codeCoverageIgnoreEnd
}

/** @var int[] $parts */
$parts = unpack('n*', $bytes);

/** @var non-empty-string */
return bin2hex(pack('n*', $parts[1] | 0x0100, $parts[2], $parts[3]));
return sprintf('%06x%06x', random_int(0, 0xffffff) | 0x010000, random_int(0, 0xffffff));
}
}
10 changes: 5 additions & 5 deletions src/Service/Nic/StaticNic.php
Expand Up @@ -56,20 +56,20 @@ public function __construct(int | string $address, private readonly Os $os = new
if (is_int($address)) {
if ($this->os->getIntSize() >= 8) {
/** @var non-empty-string $address */
$address = substr(bin2hex(pack('J*', $address | 0x010000000000)), -12);
$address = substr(bin2hex(pack('J', $address | 0x010000000000)), -12);
} else {
/** @var int[] $parts */
$parts = unpack('n*', pack('N*', $address));
$parts = unpack('n2', pack('N', $address));

/** @var non-empty-string $address */
$address = bin2hex(pack('n*', 0x0100, ...$parts));
$address = bin2hex(pack('n3', 0x0100, ...$parts));
}
} elseif (strspn($address, Format::MASK_HEX) === strlen($address) && strlen($address) <= 12) {
/** @var int[] $parts */
$parts = unpack('n*', (string) hex2bin(sprintf('%012s', $address)));
$parts = unpack('n3', (string) hex2bin(sprintf('%012s', $address)));

/** @var non-empty-string $address */
$address = bin2hex(pack('n*', $parts[1] | 0x0100, $parts[2], $parts[3]));
$address = bin2hex(pack('n3', $parts[1] | 0x0100, $parts[2], $parts[3]));
} else {
throw new InvalidArgument(
'Address must be a 48-bit integer or hexadecimal string',
Expand Down

0 comments on commit 2cb824a

Please sign in to comment.