Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from WyriHaximus/implement-package
Implement Package
- Loading branch information
Showing
16 changed files
with
1,177 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WyriHaximus\Metrics\LazyRegistry; | ||
|
||
use InvalidArgumentException; | ||
use WyriHaximus\Metrics\Counter as CounterInterface; | ||
use WyriHaximus\Metrics\Label; | ||
|
||
use function func_get_args; | ||
|
||
use const WyriHaximus\Constants\Numeric\ONE; | ||
use const WyriHaximus\Constants\Numeric\ZERO; | ||
|
||
final class Counter implements CounterInterface | ||
{ | ||
private ?CounterInterface $counter = null; | ||
|
||
private string $name; | ||
private string $description; | ||
/** @var array<Label> */ | ||
private array $labels; | ||
|
||
/** @var array<string|array<mixed>> */ | ||
private array $queue = []; | ||
|
||
public function __construct(string $name, string $description, Label ...$labels) | ||
{ | ||
$this->name = $name; | ||
$this->description = $description; | ||
$this->labels = $labels; | ||
} | ||
|
||
public function name(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function description(): string | ||
{ | ||
return $this->description; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
if ($this->counter instanceof CounterInterface) { | ||
return $this->counter->count(); | ||
} | ||
|
||
return ZERO; | ||
} | ||
|
||
/** | ||
* @return array<Label> | ||
*/ | ||
public function labels(): array | ||
{ | ||
return $this->labels; | ||
} | ||
|
||
public function incr(): void | ||
{ | ||
if ($this->counter instanceof CounterInterface) { | ||
$this->counter->incr(); | ||
|
||
return; | ||
} | ||
|
||
$this->queue[] = [__FUNCTION__, func_get_args()]; | ||
} | ||
|
||
public function incrBy(int $incr): void | ||
{ | ||
if ($this->counter instanceof CounterInterface) { | ||
$this->counter->incrBy($incr); | ||
|
||
return; | ||
} | ||
|
||
$this->queue[] = [__FUNCTION__, func_get_args()]; | ||
} | ||
|
||
public function incrTo(int $count): void | ||
{ | ||
if ($this->counter instanceof CounterInterface) { | ||
$this->counter->incrTo($count); | ||
|
||
return; | ||
} | ||
|
||
$this->queue[] = [__FUNCTION__, func_get_args()]; | ||
} | ||
|
||
public function register(CounterInterface $counter): void | ||
{ | ||
if ($this->counter instanceof CounterInterface) { | ||
throw new InvalidArgumentException(); | ||
} | ||
|
||
$this->counter = $counter; | ||
|
||
foreach ($this->queue as $call) { | ||
/** @psalm-suppress PossiblyInvalidMethodCall */ | ||
$this->counter->{$call[ZERO]}(...$call[ONE]); /** @phpstan-ignore-line */ | ||
} | ||
|
||
$this->queue = []; | ||
} | ||
} |
Oops, something went wrong.