summaryrefslogtreecommitdiff
path: root/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockSpanContext.php
blob: d094ea0e08ae2e90a6ee5d89515a494931afee5b (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

declare(strict_types=1);

namespace OpenTracing\Mock;

use OpenTracing\SpanContext;
use ArrayIterator;

final class MockSpanContext implements SpanContext
{
    /**
     * @var int
     */
    private $traceId;

    /**
     * @var int
     */
    private $spanId;

    /**
     * @var bool
     */
    private $isSampled;

    /**
     * @var array
     */
    private $items;

    private function __construct(int $traceId, int $spanId, bool $isSampled, array $items)
    {
        $this->traceId = $traceId;
        $this->spanId = $spanId;
        $this->isSampled = $isSampled;
        $this->items = $items;
    }

    public static function create(int $traceId, int $spanId, bool $sampled = true, array $items = []): SpanContext
    {
        return new self($traceId, $spanId, $sampled, $items);
    }

    public static function createAsRoot(bool $sampled = true, array $items = []): SpanContext
    {
        $traceId = $spanId = self::nextId();
        return new self($traceId, $spanId, $sampled, $items);
    }

    public static function createAsChildOf(MockSpanContext $spanContext): SpanContext
    {
        $spanId = self::nextId();
        return new self($spanContext->traceId, $spanId, $spanContext->isSampled, $spanContext->items);
    }

    public function getTraceId(): int
    {
        return $this->traceId;
    }

    public function getSpanId(): int
    {
        return $this->spanId;
    }

    public function isSampled(): bool
    {
        return $this->isSampled;
    }

    /**
     * {@inheritdoc}
     */
    public function getIterator(): ArrayIterator
    {
        return new ArrayIterator($this->items);
    }

    /**
     * {@inheritdoc}
     */
    public function getBaggageItem(string $key): ?string
    {
        return array_key_exists($key, $this->items) ? $this->items[$key] : null;
    }

    /**
     * {@inheritdoc}
     */
    public function withBaggageItem(string $key, string $value): SpanContext
    {
        return new self($this->traceId, $this->spanId, $this->isSampled, array_merge($this->items, [$key => $value]));
    }

    private static function nextId(): int
    {
        return mt_rand(0, 99999);
    }
}