XHProfService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 52
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A enable() 0 4 2
A __construct() 0 20 6
A disable() 0 4 2
1
<?php
2
3
namespace Bavix\XHProf\Services;
4
5
use Bavix\XHProf\Providers\ProviderInterface;
6
use Bavix\XHProf\Providers\TidewaysProvider;
7
use Bavix\XHProf\Providers\XHProfProvider;
8
use function extension_loaded;
9
use function mt_getrandmax;
10
use function mt_rand;
11
12
class XHProfService
13
{
14
15
    /**
16
     * @var ProviderInterface
17
     */
18
    protected $provider;
19
20
    /**
21
     * XHProfService constructor.
22
     */
23
    public function __construct()
24
    {
25
        if (!extension_loaded(config('xhprof.extension_name', 'xhprof'))) {
26
            return;
27
        }
28
29
        if (!config('xhprof.enabled', false)) {
30
            return;
31
        }
32
33
        $freq = config('xhprof.freq', 0.01);
34
        if ($freq >= (mt_rand() / mt_getrandmax())) {
35
            switch (config('xhprof.provider', XHProfProvider::class)) {
36
                case TidewaysProvider::class:
37
                    $this->provider = new TidewaysProvider();
38
                    break;
39
                default:
40
                case XHProfProvider::class:
41
                    $this->provider = new XHProfProvider();
42
                    break;
43
            }
44
        }
45
    }
46
47
    /**
48
     * @return void
49
     */
50
    public function enable(): void
51
    {
52
        if ($this->provider) {
53
            $this->provider->enable();
54
        }
55
    }
56
57
    /**
58
     * @return void
59
     */
60
    public function disable(): void
61
    {
62
        if ($this->provider) {
63
            $this->provider->disable();
64
        }
65
    }
66
67
}
68