<?php

namespace Modules\Common\Support\Trace;

class TraceContext
{
    private $traceId;
    private $spanId = 0;
    private $parentSpanId = 0;

    public function __construct($traceId = null, $parentSpanId = 0, $spanId = 0)
    {
        $this->traceId = $traceId ?? uniqid();
        $this->parentSpanId = $parentSpanId;
        $this->spanId = $spanId;
    }

    public static function newFromParent($traceInfo) {
        $traceContext = new self($traceInfo['traceId'], $traceInfo['spanId']);
        return $traceContext;
    }

    public function getTraceInfo() {
        return [
            'traceId' => $this->getTraceId(),
            'parentSpanId' => $this->getParentSpanId(),
            'spanId' => $this->getSpanId()
        ];
    }
    public function getParentSpanId() {
        return $this->parentSpanId;
    }

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

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