RequestFactoryTrait::endRequest()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 20
rs 9.8333
cc 2
nc 2
nop 3
1
<?php
2
3
/**
4
 * This file is part of Telemetry
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Telemetry\TelemetryClient;
13
14
use Slick\Telemetry\Model\Request;
15
use Slick\Telemetry\TelemetryClient;
16
use Slick\Telemetry\Trackable;
17
18
/**
19
 * RequestFactoryTrait trait
20
 *
21
 * @package Slick\Telemetry\TelemetryClient
22
 */
23
trait RequestFactoryTrait
24
{
25
    abstract public function track(Trackable $trackableData): TelemetryClient;
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function beginRequest(string $message, string $path, iterable $context = []): Request
31
    {
32
        $context = array_merge($context, ['start' => microtime(true)]);
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type iterable; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        $context = array_merge(/** @scrutinizer ignore-type */ $context, ['start' => microtime(true)]);
Loading history...
33
        return new Request($message, $path, time(), 200, 0, $context);
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function endRequest(Request $request, int $statusCode = 200, iterable $context = []): TelemetryClient
40
    {
41
        $start = $request->context()['start'] ?? (float)($request->startTime() * 1000);
42
43
        $duration = ((float) microtime(true) - $start) * 1000;
44
        $context = array_merge((array) $request->context(), $context);
0 ignored issues
show
Bug introduced by
It seems like $context can also be of type iterable; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        $context = array_merge((array) $request->context(), /** @scrutinizer ignore-type */ $context);
Loading history...
45
46
        if (array_key_exists('start', $context)) {
47
            unset($context['start']);
48
        }
49
50
        $this->trackRequest(
0 ignored issues
show
Bug introduced by
It seems like trackRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $this->/** @scrutinizer ignore-call */ 
51
               trackRequest(
Loading history...
51
            $request->message(),
52
            $request->path(),
53
            $request->startTime(),
54
            $statusCode,
55
            $duration,
56
            $context
57
        );
58
        return $this;
59
    }
60
}
61