XHProfService::disable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
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