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

getReplacementCommandArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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
    {
27
        $this->logger = $logger;
28
    }
29
30
    /**
31
     * @return int
32
     */
33
    public function hasReplaceCommandHook()
34
    {
35
        return count($this->getReplaceCommandHooks());
36
    }
37
38
    /**
39
     * @return \callable[]
40
     */
41
    public function getReplaceCommandHooks()
42
    {
43
        $hooks = [
44
            HookManager::REPLACE_COMMAND_HOOK,
45
        ];
46
        $replaceCommandHooks = $this->getHooks($hooks);
47
48
        return $replaceCommandHooks;
49
    }
50
51
    /**
52
     * @param \Consolidation\AnnotatedCommand\CommandData $commandData
53
     *
54
     * @return callable
55
     */
56
    public function getReplacementCommand(CommandData $commandData)
57
    {
58
        $replaceCommandHooks = $this->getReplaceCommandHooks();
59
60
        // We only take the first hook implementation of "replace-command" as the replacement. Commands shouldn't have
61
        // more than one replacement.
62
        $replacementCommand = reset($replaceCommandHooks);
63
64
        if ($this->logger && count($replaceCommandHooks) > 1) {
65
            $command_name = $commandData->annotationData()->get('command', 'unknown');
66
            $message = "Multiple implementations of the \"replace - command\" hook exist for the \"$command_name\" command.\n";
67
            foreach ($replaceCommandHooks as $replaceCommandHook) {
68
                $class = get_class($replaceCommandHook[0]);
69
                $method = $replaceCommandHook[1];
70
                $hook_name = "$class->$method";
71
                $message .= "  - $hook_name\n";
72
            }
73
            $this->logger->warning($message);
74
        }
75
76
        return $replacementCommand;
77
    }
78
79
    /**
80
     * @param \Consolidation\AnnotatedCommand\CommandData $commandData
81
     *
82
     * @return array
83
     */
84
    public function getReplacementCommandArguments(CommandData $commandData) {
85
        $args_and_options = $commandData->getArgsAndOptions();
86
        $args = [ $args_and_options ];
87
88
        return $args;
89
    }
90
}
91