Tracer::startSpan()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
c 1
b 0
f 1
dl 0
loc 27
rs 9.7333
cc 4
nc 6
nop 2
1
<?php
2
/**
3
 * OpenTracing::Tracer
4
 * User: moyo
5
 * Date: 23/11/2017
6
 * Time: 10:38 AM
7
 */
8
9
namespace Carno\Tracing\Standard;
10
11
use Carno\Tracing\Chips\RootInitializer;
12
use Carno\Tracing\Contracts\Platform;
13
use Carno\Tracing\Contracts\Vars\FMT;
14
use Carno\Tracing\Exception\UnsupportedCarrierInstance;
15
use OpenTracing\Exceptions\SpanContextNotFound;
16
use OpenTracing\Exceptions\UnsupportedFormat;
17
use OpenTracing\Span as OpenSpan;
18
use OpenTracing\SpanContext;
19
use OpenTracing\SpanOptions;
20
use OpenTracing\Tracer as TracerAPI;
21
use Psr\Http\Message\MessageInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\MessageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
class Tracer implements TracerAPI
24
{
25
    use RootInitializer;
26
27
    /**
28
     * @var Platform
29
     */
30
    private $platform = null;
31
32
    /**
33
     * Tracer constructor.
34
     * @param Platform $platform
35
     */
36
    public function __construct(Platform $platform)
37
    {
38
        $this->platform = $platform;
39
    }
40
41
    /**
42
     * @param string $operationName
43
     * @param SpanOptions $options
44
     * @return OpenSpan
45
     */
46
    public function startSpan($operationName, $options = null) : OpenSpan
47
    {
48
        $context = null;
49
        $startTime = null;
50
51
        if ($options) {
52
            $startTime = $options->getStartTime();
53
            $references = $options->getReferences();
54
            if ($references) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $references of type OpenTracing\Reference[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
55
                $context = (reset($references))->getContext();
56
            }
57
        }
58
59
        $span = new Span(
60
            $this->platform->serializer(),
61
            $this->platform->transporter(),
62
            $context ?? $this->newContext(),
63
            $startTime
64
        );
65
66
        $span->overwriteOperationName($operationName);
67
68
        if ($options) {
69
            $span->setTags($options->getTags());
70
        }
71
72
        return $span;
73
    }
74
75
    /**
76
     * @param SpanContext $spanContext
77
     * @param string $format
78
     * @param mixed $carrier
79
     */
80
    public function inject(SpanContext $spanContext, $format, &$carrier) : void
81
    {
82
        switch ($format) {
83
            case FMT::HTTP_HEADERS:
84
                if ($carrier instanceof MessageInterface) {
85
                    $this->platform->carrier()->http($carrier)->fusedContext($spanContext);
86
                } else {
87
                    throw UnsupportedCarrierInstance::for(gettype($carrier));
88
                }
89
                break;
90
            default:
91
                throw UnsupportedFormat::forFormat($format);
92
        }
93
    }
94
95
    /**
96
     * @param string $format
97
     * @param mixed $carrier
98
     * @return SpanContext
99
     */
100
    public function extract($format, $carrier) : SpanContext
101
    {
102
        switch ($format) {
103
            case FMT::PREV_CTX:
104
                return $this->platform->carrier()->ctx($carrier)->fusedContext();
105
            case FMT::HTTP_HEADERS:
106
                if ($carrier instanceof MessageInterface) {
107
                    return $this->platform->carrier()->http($carrier)->fusedContext();
108
                }
109
                throw SpanContextNotFound::create();
110
            default:
111
                throw UnsupportedFormat::forFormat($format);
112
        }
113
    }
114
115
    /**
116
     * flush to some collector
117
     */
118
    public function flush() : void
119
    {
120
        $this->platform->transporter()->flushing();
121
    }
122
}
123