summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Common/Time/StopWatchFactory.php
blob: f60c377fc1ccfade6d4d3bc68b2d2f25a33f4123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Time;

final class StopWatchFactory implements StopWatchFactoryInterface
{
    private static ?StopWatchInterface $default = null;

    private ClockInterface $clock;
    private ?int $initialStartTime;

    public function __construct(?ClockInterface $clock = null, ?int $initialStartTime = null)
    {
        $this->clock = $clock ?? ClockFactory::getDefault();
        $this->initialStartTime = $initialStartTime;
    }

    public static function create(?ClockInterface $clock = null, ?int $initialStartTime = null): self
    {
        return new self($clock, $initialStartTime);
    }

    public static function fromClockFactory(ClockFactoryInterface $factory, ?int $initialStartTime = null): self
    {
        return self::create($factory->build(), $initialStartTime);
    }

    public function build(): StopWatch
    {
        return new StopWatch($this->clock, $this->initialStartTime);
    }

    public static function getDefault(): StopWatchInterface
    {
        return self::$default ?? self::$default = self::create()->build();
    }

    public static function setDefault(?StopWatchInterface $default): void
    {
        self::$default = $default;
    }
}