Passed
Pull Request — master (#309)
by Sander
02:53
created

ConsoleEventSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\AuditorBundle\Event;
6
7
use DH\Auditor\Configuration;
8
use DH\Auditor\User\UserProviderInterface;
9
use DH\AuditorBundle\User\ConsoleUserProvider;
10
use Symfony\Component\Console\ConsoleEvents;
11
use Symfony\Component\Console\Event\ConsoleCommandEvent;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
class ConsoleEventSubscriber implements EventSubscriberInterface
15
{
16
    private ConsoleUserProvider $consoleUserProvider;
17
    private Configuration $configuration;
18
    private UserProviderInterface $provider;
19
20
    public function __construct(ConsoleUserProvider $consoleUserProvider, Configuration $configuration, UserProviderInterface $provider)
21
    {
22
        $this->consoleUserProvider = $consoleUserProvider;
23
        $this->configuration = $configuration;
24
        $this->provider = $provider;
25
    }
26
27
    public static function getSubscribedEvents(): array
28
    {
29
        return [
30
            ConsoleEvents::COMMAND => 'registerConsoleUser',
31
            ConsoleEvents::TERMINATE => 'restoreDefaultUser',
32
        ];
33
    }
34
35
    public function registerConsoleUser(ConsoleCommandEvent $commandEvent): void
36
    {
37
        $command = $commandEvent->getCommand();
38
        $this->consoleUserProvider->setCurrentCommand($command);
39
        $this->configuration->setUserProvider($this->consoleUserProvider);
40
    }
41
42
    public function restoreDefaultUser(): void
43
    {
44
        $this->consoleUserProvider->setCurrentCommand(null);
45
        $this->configuration->setUserProvider($this->provider);
46
    }
47
}
48