Completed
Push — master ( 651eb5...e7f95e )
by Tomáš
16:52 queued 15:34
created

GitLoggerEventSubscriber::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GitWrapper\EventSubscriber;
6
7
use GitWrapper\Event\AbstractGitEvent;
8
use GitWrapper\Event\GitBypassEvent;
9
use GitWrapper\Event\GitErrorEvent;
10
use GitWrapper\Event\GitOutputEvent;
11
use GitWrapper\Event\GitPrepareEvent;
12
use GitWrapper\Event\GitSuccessEvent;
13
use GitWrapper\Exception\GitException;
14
use Psr\Log\LoggerAwareInterface;
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\LogLevel;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
19
final class GitLoggerEventSubscriber implements EventSubscriberInterface, LoggerAwareInterface
20
{
21
    /**
22
     * Mapping of event to log level.
23
     *
24
     * @var string[]
25
     */
26
    private $logLevelMappings = [
27
        GitPrepareEvent::class => LogLevel::INFO,
28
        GitOutputEvent::class => LogLevel::DEBUG,
29
        GitSuccessEvent::class => LogLevel::INFO,
30
        GitErrorEvent::class => LogLevel::ERROR,
31
        GitBypassEvent::class => LogLevel::INFO,
32
    ];
33
34
    /**
35
     * @var LoggerInterface
36
     */
37
    private $logger;
38
39
    public function __construct(LoggerInterface $logger)
40
    {
41
        $this->logger = $logger;
42
    }
43
44
    /**
45
     * Required by interface
46
     */
47
    public function setLogger(LoggerInterface $logger): void
48
    {
49
        $this->logger = $logger;
50
    }
51
52
    public function setLogLevelMapping(string $eventName, string $logLevel): void
53
    {
54
        $this->logLevelMappings[$eventName] = $logLevel;
55
    }
56
57
    /**
58
     * Returns the log level mapping for an event.
59
     */
60
    public function getLogLevelMapping(string $eventName): string
61
    {
62
        if (! isset($this->logLevelMappings[$eventName])) {
63
            throw new GitException(sprintf('Unknown event "%s"', $eventName));
64
        }
65
66
        return $this->logLevelMappings[$eventName];
67
    }
68
69
    public static function getSubscribedEvents(): array
70
    {
71
        return [
72
            GitPrepareEvent::class => ['onPrepare', 0],
73
            GitOutputEvent::class => ['handleOutput', 0],
74
            GitSuccessEvent::class => ['onSuccess', 0],
75
            GitErrorEvent::class => ['onError', 0],
76
            GitBypassEvent::class => ['onBypass', 0],
77
        ];
78
    }
79
80
    /**
81
     * Adds a log message using the level defined in the mappings.
82
     *
83
     * @param mixed[] $context
84
     */
85
    public function log(AbstractGitEvent $gitEvent, string $message, array $context = []): void
86
    {
87
        $method = $this->getLogLevelMapping(get_class($gitEvent));
88
        $context += [
89
            'command' => $gitEvent->getProcess()->getCommandLine(),
90
        ];
91
92
        $this->logger->{$method}($message, $context);
93
    }
94
95
    public function onPrepare(GitPrepareEvent $gitPrepareEvent): void
96
    {
97
        $this->log($gitPrepareEvent, 'Git command preparing to run');
98
    }
99
100
    public function handleOutput(GitOutputEvent $gitOutputEvent): void
101
    {
102
        $context = ['error' => $gitOutputEvent->isError()];
103
        $this->log($gitOutputEvent, $gitOutputEvent->getBuffer(), $context);
104
    }
105
106
    public function onSuccess(GitSuccessEvent $gitSuccessEvent): void
107
    {
108
        $this->log($gitSuccessEvent, 'Git command successfully run');
109
    }
110
111
    public function onError(GitErrorEvent $gitErrorEvent): void
112
    {
113
        $this->log($gitErrorEvent, 'Error running Git command');
114
    }
115
116
    public function onBypass(GitBypassEvent $gitBypassEvent): void
117
    {
118
        $this->log($gitBypassEvent, 'Git command bypassed');
119
    }
120
}
121