summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Trace/StatusData.php
blob: c28ea22abf0419fbc3ca0e0b5ade8f1a69b16a0e (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
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Trace;

use OpenTelemetry\API\Trace as API;

final class StatusData implements StatusDataInterface
{
    private static ?self $ok = null;
    private static ?self $unset = null;
    private static ?self $error = null;
    private string $code;
    private string $description;

    /** @psalm-param API\StatusCode::STATUS_* $code */
    public function __construct(
        string $code,
        string $description
    ) {
        $this->code = $code;
        $this->description = $description;
    }

    /** @psalm-param API\StatusCode::STATUS_* $code */
    public static function create(string $code, ?string $description = null): self
    {
        if (empty($description)) {
            switch ($code) {
                case API\StatusCode::STATUS_UNSET:
                    return self::unset();
                case API\StatusCode::STATUS_ERROR:
                    return self::error();
                case API\StatusCode::STATUS_OK:
                    return self::ok();
            }
        }

        // Ignore description for non Error statuses.
        if (API\StatusCode::STATUS_ERROR !== $code) {
            $description = '';
        }

        return new self($code, $description); /** @phan-suppress-current-line PhanTypeMismatchArgumentNullable */
    }

    public static function ok(): self
    {
        if (null === self::$ok) {
            self::$ok = new self(API\StatusCode::STATUS_OK, '');
        }

        return self::$ok;
    }

    public static function error(): self
    {
        if (null === self::$error) {
            self::$error = new self(API\StatusCode::STATUS_ERROR, '');
        }

        return self::$error;
    }

    public static function unset(): self
    {
        if (null === self::$unset) {
            self::$unset = new self(API\StatusCode::STATUS_UNSET, '');
        }

        return self::$unset;
    }

    public function getCode(): string
    {
        return $this->code;
    }

    public function getDescription(): string
    {
        return $this->description;
    }
}