Passed
Push — master ( bace86...bb4cad )
by Dmitriy
05:30 queued 02:57
created

Debugger::stop()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 5
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Yiisoft\Strings\WildcardPattern;
9
use Yiisoft\Yii\Console\Event\ApplicationStartup;
10
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
11
use Yiisoft\Yii\Debug\Storage\StorageInterface;
12
use Yiisoft\Yii\Http\Event\BeforeRequest;
13
14
final class Debugger
15
{
16
    private bool $skipCollect = false;
17
    private bool $active = false;
18 7
19
    public function __construct(
20
        private DebuggerIdGenerator $idGenerator,
21
        private StorageInterface $target,
22
        /**
23
         * @var CollectorInterface[]
24
         */
25
        private array $collectors,
26
        private array $ignoredRequests = [],
27
        private array $ignoredCommands = [],
28
    ) {
29
        register_shutdown_function([$this, 'shutdown']);
30 1
    }
31
32 1
    public function getId(): string
33
    {
34
        return $this->idGenerator->getId();
35 3
    }
36
37 3
    public function startup(object $event): void
38 1
    {
39 1
        $this->active = true;
40
        $this->skipCollect = false;
41
42 2
        if ($event instanceof BeforeRequest && $this->isRequestIgnored($event->getRequest())) {
43
            $this->skipCollect = true;
44
            return;
45
        }
46
47 2
        if ($event instanceof ApplicationStartup && $this->isCommandIgnored($event->commandName)) {
48 2
            $this->skipCollect = true;
49 2
            return;
50 2
        }
51
52
        $this->idGenerator->reset();
53
        foreach ($this->collectors as $collector) {
54 2
            $this->target->addCollector($collector);
55
            $collector->startup();
56 2
        }
57 2
    }
58 2
59 1
    public function shutdown(): void
60
    {
61
        if (!$this->active) {
62 1
            return;
63
        }
64
65
        try {
66
            if (!$this->skipCollect) {
67
                $this->target->flush();
68
            }
69
        } finally {
70
            foreach ($this->collectors as $collector) {
71
                $collector->shutdown();
72
            }
73
            $this->active = false;
74
        }
75
    }
76
77
    public function stop(): void
78 2
    {
79
        if (!$this->active) {
80
            return;
81 2
        }
82 1
83
        try {
84 2
            $this->target->clear();
85 2
        } finally {
86 2
            foreach ($this->collectors as $collector) {
87
                $collector->shutdown();
88 2
            }
89
            $this->active = false;
90
        }
91
    }
92
93
    private function isRequestIgnored(ServerRequestInterface $request): bool
94
    {
95
        $path = $request->getUri()->getPath();
96
        foreach ($this->ignoredRequests as $pattern) {
97
            if ((new WildcardPattern($pattern))->match($path)) {
98
                return true;
99 1
            }
100
        }
101 1
        return false;
102 1
    }
103 1
104
    private function isCommandIgnored(?string $command): bool
105
    {
106
        if ($command === null || $command === '') {
107
            return true;
108
        }
109
        foreach ($this->ignoredCommands as $pattern) {
110
            if ((new WildcardPattern($pattern))->match($command)) {
111
                return true;
112
            }
113 1
        }
114
        return false;
115 1
    }
116 1
117 1
    /**
118
     * @param array $ignoredRequests Patterns for ignored request URLs.
119
     *
120
     * @see WildcardPattern
121
     */
122
    public function withIgnoredRequests(array $ignoredRequests): self
123
    {
124
        $new = clone $this;
125
        $new->ignoredRequests = $ignoredRequests;
126
        return $new;
127
    }
128
129
    /**
130
     * @param array $ignoredCommands Patterns for ignored commands names.
131
     *
132
     * @see WildcardPattern
133
     */
134
    public function withIgnoredCommands(array $ignoredCommands): self
135
    {
136
        $new = clone $this;
137
        $new->ignoredCommands = $ignoredCommands;
138
        return $new;
139
    }
140
}
141