whitemerry/phpkin

View on GitHub
src/Identifier/TraceIdentifier.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
namespace whitemerry\phpkin\Identifier;

/**
 * Class TraceIdentifier
 *
 * @author Piotr Bugaj <whitemerry@outlook.com>
 * @package whitemerry\phpkin\identifier
 */
class TraceIdentifier extends Identifier
{
    /**
     * @inheritdoc
     *
     * @param $fromString string Optional, creates identifier from string
     */
    public function __construct($fromString = null)
    {
        if ($fromString && is_zipkin_trace_identifier($fromString)) {
            $this->value = $fromString;
        } else {
            parent::__construct();
        }
    }

    /**
     * Generates 128-bit hex-encoded identifier
     * http://zipkin.io/pages/instrumenting.html#trace-identifiers
     *
     * @inheritdoc
     */
    protected function generate()
    {
        return bin2hex(openssl_random_pseudo_bytes(16));
    }
}