ConsoleUserInteraction   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 79
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A onCommand() 0 5 1
A writeTitle() 0 6 1
A write() 0 4 1
A ask() 0 6 1
A getInput() 0 8 2
A getOutput() 0 8 2
1
<?php
2
3
namespace Dock\Cli\IO;
4
5
use Dock\IO\UserInteraction;
6
use Symfony\Component\Console\Event\ConsoleCommandEvent;
7
use Symfony\Component\Console\Helper\FormatterHelper;
8
use Symfony\Component\Console\Helper\QuestionHelper;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Question\Question;
12
13
class ConsoleUserInteraction implements UserInteraction
14
{
15
    /**
16
     * @var OutputInterface
17
     */
18
    private $output;
19
20
    /**
21
     * @var InputInterface
22
     */
23
    private $input;
24
25
    /**
26
     * This method is called when a console command event is fired.
27
     *
28
     * @param ConsoleCommandEvent $event
29
     */
30
    public function onCommand(ConsoleCommandEvent $event)
31
    {
32
        $this->input = $event->getInput();
33
        $this->output = $event->getOutput();
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function writeTitle($name)
40
    {
41
        $formatter = new FormatterHelper();
42
        $formattedBlock = $formatter->formatBlock([$name], 'info');
43
        $this->getOutput()->writeln($formattedBlock);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function write($string)
50
    {
51
        $this->getOutput()->writeln($string);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function ask(Question $question)
58
    {
59
        $questionHelper = new QuestionHelper();
60
61
        return $questionHelper->ask($this->getInput(), $this->getOutput(), $question);
62
    }
63
64
    /**
65
     * @throws \RuntimeException
66
     *
67
     * @return InputInterface
68
     */
69
    private function getInput()
70
    {
71
        if (null === $this->input) {
72
            throw new \RuntimeException('No user interaction context available.');
73
        }
74
75
        return $this->input;
76
    }
77
78
    /**
79
     * @throws \RuntimeException
80
     *
81
     * @return OutputInterface
82
     */
83
    public function getOutput()
84
    {
85
        if (null === $this->output) {
86
            throw new \RuntimeException('No user interaction context available.');
87
        }
88
89
        return $this->output;
90
    }
91
}
92