Completed
Pull Request — master (#102)
by Greg
02:15
created

PrepareTerminalWidthOption::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
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
    protected $terminal;
14
15
    /** var int */
16
    protected $defaultWidth;
17
18
    /** var int */
19
    protected $maxWidth = PHP_INT_MAX;
20
21
    /** var int */
22
    protected $minWidth = 0;
23
24
    /* var boolean */
25
    protected $shouldWrap = true;
26
27
    public function __construct($defaultWidth = 0)
28
    {
29
        $this->defaultWidth = $defaultWidth;
30
31
        // If STDOUT is not attached to a terminal, then disable
32
        // automatic width detection.
33
        if (function_exists('posix_isatty') && !posix_isatty(STDOUT)) {
34
            $this->shouldWrap = false;
35
        }
36
    }
37
38
    public function setApplication(Application $application)
39
    {
40
        $this->application = $application;
41
    }
42
43
    public function setTerminal($terminal)
44
    {
45
        $this->terminal = $terminal;
46
    }
47
48
    public function getTerminal()
49
    {
50
        if (!$this->terminal && class_exists('\Symfony\Component\Console\Terminal')) {
51
            $this->terminal = new \Symfony\Component\Console\Terminal();
52
        }
53
        return $this->terminal;
54
    }
55
56
    public function enableWrap($shouldWrap)
57
    {
58
        $this->shouldWrap = $shouldWrap;
59
    }
60
61
    public function prepare(CommandData $commandData, FormatterOptions $options)
62
    {
63
        $width = $this->getTerminalWidth();
64
        if (!$width) {
65
            $width = $this->defaultWidth;
66
        }
67
68
        // Enforce minimum and maximum widths
69
        $width = min($width, $this->getMaxWidth($commandData));
70
        $width = max($width, $this->getMinWidth($commandData));
71
72
        $options->setWidth($width);
73
    }
74
75
    protected function getTerminalWidth()
76
    {
77
        // Don't wrap if wrapping has been disabled.
78
        if (!$this->shouldWrap) {
79
            return 0;
80
        }
81
82
        $terminal = $this->getTerminal();
83
        if ($terminal) {
84
            return $terminal->getWidth();
85
        }
86
87
        return $this->getTerminalWidthViaApplication();
88
    }
89
90
    protected function getTerminalWidthViaApplication()
91
    {
92
        if (!$this->application) {
93
            return 0;
94
        }
95
        $dimensions = $this->application->getTerminalDimensions();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Consol...getTerminalDimensions() has been deprecated with message: since version 3.2, to be removed in 4.0. Create a Terminal instance instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
96
        if ($dimensions[0] == null) {
97
            return 0;
98
        }
99
100
        return $dimensions[0];
101
    }
102
103
    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...
104
    {
105
        return $this->maxWidth;
106
    }
107
108
    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...
109
    {
110
        return $this->minWidth;
111
    }
112
}
113