BenchmarkTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
iocContainer() 0 1 ?
A setBenchmarker() 0 4 1
A benchmark() 0 15 4
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Debug\Traits;
10
11
use Interop\Container\ContainerInterface;
12
use Spiral\Debug\BenchmarkerInterface;
13
14
/**
15
 * Provides access to benchmark function.
16
 */
17
trait BenchmarkTrait
18
{
19
    /**
20
     * @invisible
21
     *
22
     * @var BenchmarkerInterface
23
     */
24
    private $benchmarker;
25
26
    /**
27
     * Set custom benchmarker.
28
     *
29
     * @param BenchmarkerInterface $benchmarker
30
     */
31 1
    public function setBenchmarker(BenchmarkerInterface $benchmarker)
32
    {
33 1
        $this->benchmarker = $benchmarker;
34 1
    }
35
36
    /**
37
     * Benchmarks used to record long or important operations inside spiral components. Method
38
     * should return elapsed time when record are be closed (same set of arguments has to be
39
     * provided).
40
     *
41
     * @param string|array $record  Benchmark record name or set of names.
42
     * @param string       $context Record context (if any).
43
     *
44
     * @return bool|float|mixed
45
     */
46 3
    private function benchmark($record, string $context = '')
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
47
    {
48 3
        if (empty($this->benchmarker)) {
49 2
            $container = $this->iocContainer();
50
51 2
            if (empty($container) || !$container->has(BenchmarkerInterface::class)) {
52
                //Nothing to do
53 1
                return false;
54
            }
55
56 1
            $this->benchmarker = $container->get(BenchmarkerInterface::class);
57
        }
58
59 2
        return $this->benchmarker->benchmark($this, $record, $context);
60
    }
61
62
    /**
63
     * @return ContainerInterface
64
     */
65
    abstract protected function iocContainer();
66
}
67