Passed
Pull Request — master (#144)
by Dmitriy
02:38
created

Debugger::withIgnoredCommands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
18 7
    public function __construct(
19
        private DebuggerIdGenerator $idGenerator,
20
        private StorageInterface $target,
21
        /**
22
         * @var CollectorInterface[]
23
         */
24
        private array $collectors,
25
        private array $ignoredRequests = [],
26
        private array $ignoredCommands = [],
27
    ) {
28
    }
29
30 1
    public function getId(): string
31
    {
32 1
        return $this->idGenerator->getId();
33
    }
34
35 3
    public function startup(object $event): void
36
    {
37 3
        if ($event instanceof BeforeRequest && $this->isRequestIgnored($event->getRequest())) {
38 1
            $this->skipCollect = true;
39 1
            return;
40
        }
41
42 2
        if ($event instanceof ApplicationStartup && $this->isCommandIgnored($event->commandName)) {
0 ignored issues
show
Bug introduced by
The property commandName does not seem to exist on Yiisoft\Yii\Console\Event\ApplicationStartup.
Loading history...
43
            $this->skipCollect = true;
44
            return;
45
        }
46
47 2
        $this->idGenerator->reset();
48 2
        foreach ($this->collectors as $collector) {
49 2
            $this->target->addCollector($collector);
50 2
            $collector->startup();
51
        }
52
    }
53
54 2
    private function isRequestIgnored(ServerRequestInterface $request): bool
55
    {
56 2
        $path = $request->getUri()->getPath();
57 2
        foreach ($this->ignoredRequests as $pattern) {
58 2
            if ((new WildcardPattern($pattern))->match($path)) {
59 1
                return true;
60
            }
61
        }
62 1
        return false;
63
    }
64
65
    private function isCommandIgnored(?string $command): bool
66
    {
67
        if ($command === null || $command === '') {
68
            return true;
69
        }
70
        foreach ($this->ignoredCommands as $pattern) {
71
            if ((new WildcardPattern($pattern))->match($command)) {
72
                return true;
73
            }
74
        }
75
        return false;
76
    }
77
78 2
    public function shutdown(): void
79
    {
80
        try {
81 2
            if (!$this->skipCollect) {
82 1
                $this->target->flush();
83
            }
84 2
        } finally {
85 2
            foreach ($this->collectors as $collector) {
86 2
                $collector->shutdown();
87
            }
88 2
            $this->skipCollect = false;
89
        }
90
    }
91
92
    /**
93
     * @param array $ignoredRequests Patterns for ignored request URLs.
94
     *
95
     * @return self
96
     *
97
     * @see WildcardPattern
98
     */
99 1
    public function withIgnoredRequests(array $ignoredRequests): self
100
    {
101 1
        $new = clone $this;
102 1
        $new->ignoredRequests = $ignoredRequests;
103 1
        return $new;
104
    }
105
106
    /**
107
     * @param array $ignoredCommands Patterns for ignored commands names.
108
     *
109
     * @return self
110
     *
111
     * @see WildcardPattern
112
     */
113 1
    public function withIgnoredCommands(array $ignoredCommands): self
114
    {
115 1
        $new = clone $this;
116 1
        $new->ignoredCommands = $ignoredCommands;
117 1
        return $new;
118
    }
119
}
120