k911/swoole-bundle

View on GitHub
src/Component/AtomicCounter.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

declare(strict_types=1);

namespace K911\Swoole\Component;

use Swoole\Atomic;

final class AtomicCounter
{
    private $counter;

    public function __construct(Atomic $counter)
    {
        $this->counter = $counter;
    }

    public function increment(): void
    {
        $this->counter->add(1);
    }

    public function get(): int
    {
        return $this->counter->get();
    }

    public static function fromZero(): self
    {
        return new self(new Atomic(0));
    }
}