Completed
Pull Request — master (#57)
by Greg
02:10
created

PrepareTerminalWidthOption::prepare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
namespace Consolidation\AnnotatedCommand\Options;
3
4
use Symfony\Component\Console\Application;
5
use Consolidation\AnnotatedCommand\CommandData;
6
use Consolidation\OutputFormatters\Options\FormatterOptions;
7
8
class PrepareTerminalWidthOption implements PrepareFormatter
9
{
10
    /** var Application */
11
    protected $application;
12
13
    /** var int */
14
    protected $defaultWidth;
15
16
    /** var int */
17
    protected $maxWidth = PHP_INT_MAX;
18
19
    /** var int */
20
    protected $minWidth = 0;
21
22
    public function __construct($defaultWidth = 0)
23
    {
24
        $this->defaultWidth = $defaultWidth;
25
    }
26
27
    public function setApplication(Application $application)
28
    {
29
        $this->application = $application;
30
    }
31
32
    public function prepare(CommandData $commandData, FormatterOptions $options)
33
    {
34
        $width = $this->getTerminalWidth();
35
        if (!$width) {
36
            $width = $this->defaultWidth;
37
        }
38
39
        // Enforce minimum and maximum widths
40
        $width = min($width, $this->getMaxWidth($commandData));
41
        $width = max($width, $this->getMinWidth($commandData));
42
43
        $options->setWidth($width);
44
    }
45
46
    protected function getTerminalWidth()
47
    {
48
        if (!$this->application) {
49
            return 0;
50
        }
51
52
        $dimensions = $this->application->getTerminalDimensions();
53
        if ($dimensions[0] == null) {
54
            return 0;
55
        }
56
57
        return $dimensions[0];
58
    }
59
60
    protected function getMaxWidth(CommandData $commandData)
0 ignored issues
show
Unused Code introduced by
The parameter $commandData is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        return $this->maxWidth;
63
    }
64
65
    protected function getMinWidth(CommandData $commandData)
0 ignored issues
show
Unused Code introduced by
The parameter $commandData is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        return $this->minWidth;
68
    }
69
}
70