Completed
Pull Request — master (#97)
by
unknown
02:25
created

ReplaceCommandHookDispatcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 61
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setLogger() 0 3 1
A hasReplaceCommandHook() 0 4 1
A getReplaceCommandHooks() 0 9 1
B getReplacementCommand() 0 22 4
1
<?php
2
3
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
4
5
use Consolidation\AnnotatedCommand\CommandData;
6
use Consolidation\AnnotatedCommand\Hooks\HookManager;
7
use Psr\Log\LoggerInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\ConsoleEvents;
10
use Symfony\Component\Console\Event\ConsoleCommandEvent;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
/**
15
 * Call hooks.
16
 */
17
class ReplaceCommandHookDispatcher extends HookDispatcher
18
{
19
20
    /**
21
     * @var LoggerInterface|null
22
     */
23
    protected $logger = null;
24
25
    public function setLogger(LoggerInterface $logger) {
26
        $this->logger = $logger;
27
    }
28
29
    /**
30
     * @return int
31
     */
32
    public function hasReplaceCommandHook()
33
    {
34
        return count($this->getReplaceCommandHooks());
35
    }
36
37
    /**
38
     * @return \callable[]
39
     */
40
    public function getReplaceCommandHooks()
41
    {
42
        $hooks = [
43
            HookManager::REPLACE_COMMAND_HOOK,
44
        ];
45
        $replaceCommandHooks = $this->getHooks($hooks);
46
47
        return $replaceCommandHooks;
48
    }
49
50
    /**
51
     * @param \Consolidation\AnnotatedCommand\CommandData $commandData
52
     *
53
     * @return callable
54
     */
55
    public function getReplacementCommand(CommandData $commandData)
56
    {
57
        $replaceCommandHooks = $this->getReplaceCommandHooks();
58
59
        // We only take the first hook implementation of "replace-command" as the replacement. Commands shouldn't have
60
        // more than one replacement.
61
        $replacementCommand = reset($replaceCommandHooks);
62
63
        if ($this->logger && count($replaceCommandHooks) > 1) {
64
            $command_name = $commandData->annotationData()->get('command', 'unknown');
65
            $message = "Multiple implementations of the \"replace - command\" hook exist for the \"$command_name\" command.\n";
66
            foreach($replaceCommandHooks as $replaceCommandHook) {
67
                $class = get_class($replaceCommandHook[0]);
68
                $method = $replaceCommandHook[1];
69
                $hook_name = "$class->$method";
70
                $message .= "  - $hook_name\n";
71
            }
72
            $this->logger->warning($message);
73
        }
74
75
        return $replacementCommand;
76
    }
77
}
78