Completed
Pull Request — 2.0 (#981)
by Rob
12:38
created

ImagineStyle::status()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Component\Console\Style;
13
14
use Liip\ImagineBundle\Exception\InvalidArgumentException;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Style\SymfonyStyle;
18
use Symfony\Component\Console\Terminal;
19
20
/**
21
 * @internal
22
 */
23
class ImagineStyle implements ImagineStyleInterface
24
{
25
    /**
26
     * @var SymfonyStyle
27
     */
28
    private $io;
29
30
    /**
31
     * @var bool
32
     */
33
    private $decoration;
34
35
    /**
36
     * @param InputInterface  $input
37
     * @param OutputInterface $output
38
     * @param bool            $decoration
39
     */
40
    public function __construct(InputInterface $input, OutputInterface $output, bool $decoration = true)
41
    {
42
        $this->io = new SymfonyStyle($input, $output);
43
        $this->decoration = $decoration;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function text(string $string, array $replacements = []): ImagineStyleInterface
50
    {
51
        $this->io->write($this->compileString($string, $replacements));
52
53
        return $this;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function line(string $string, array $replacements = []): ImagineStyleInterface
60
    {
61
        $this->io->writeln($this->compileString($string, $replacements));
62
63
        return $this;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function newline(int $count = 1): ImagineStyleInterface
70
    {
71
        $this->io->newLine($count);
72
73
        return $this;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function space(int $count = 1): ImagineStyleInterface
80
    {
81
        return $this->text(str_repeat(' ', $count));
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function separator(string $character = null, int $width = null, string $fg = null, bool $newline = true): ImagineStyleInterface
88
    {
89
        if (null === $width) {
90
            $width = class_exists(Terminal::class) ? (new Terminal())->getWidth() : 80;
91
        }
92
93
        $this->text('<fg=%2$s;>%1$s</>', [str_repeat($character ?: '-', $width), $fg ?: 'default']);
94
95
        if ($newline) {
96
            $this->newline();
97
        }
98
99
        return $this;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function status(string $status, string $fg = null, string $bg = null): ImagineStyleInterface
106
    {
107
        return $this->text(
108
            sprintf('<fg=%2$s;bg=%3$s>(</><fg=%2$s;bg=%3$s;options=bold>%1$s</><fg=%2$s;bg=%3$s>)</>', $status, $fg ?: 'default', $bg ?: 'default')
109
        );
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function group(string $item, string $group, string $fg = null, string $bg = null): ImagineStyleInterface
116
    {
117
        return $this->text(
118
            sprintf('<fg=%3$s;bg=%4$s;options=bold>%1$s[</><fg=%3$s;bg=%4$s>%2$s</><fg=%3$s;bg=%4$s;options=bold>]</>', $item, $group, $fg ?: 'default', $bg ?: 'default')
119
        );
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function title(string $title, string $type = null, string $fg = null, string $bg = null): ImagineStyleInterface
126
    {
127
        if (!$this->decoration) {
128
            return $this->plainTitle($title, $type);
129
        }
130
131
        return $this->block($title, $type, $fg ?: 'white', $bg ?: 'magenta');
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function smallBlock(string $string, string $type, string $fg = null, string $bg = null, string $prefix = null): ImagineStyleInterface
138
    {
139
        return $this->block($string, $type, $fg, $bg, $prefix, false);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function largeBlock(string $string, string $type, string $fg = null, string $bg = null, string $prefix = null): ImagineStyleInterface
146
    {
147
        return $this->block($string, $type, $fg, $bg, $prefix, true);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function okayBlock(string $string, array $replacements = []): ImagineStyleInterface
154
    {
155
        return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'OKAY', 'black', 'green', '-');
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function noteBlock(string $string, array $replacements = []): ImagineStyleInterface
162
    {
163
        return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'NOTE', 'yellow', 'black', '/');
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function critBlock(string $string, array $replacements = []): ImagineStyleInterface
170
    {
171
        return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'ERROR', 'white', 'red', '#');
172
    }
173
174
    /**
175
     * @param string      $title
176
     * @param string|null $type
177
     *
178
     * @return ImagineStyleInterface
179
     */
180
    private function plainTitle(string $title, string $type = null): ImagineStyleInterface
181
    {
182
        $this->newline();
183
184
        if ($type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
185
            $this->line('# [%s] %s', [$type, $title]);
186
        } else {
187
            $this->line('# %s', [$title]);
188
        }
189
190
        return $this->newline();
191
    }
192
193
    /**
194
     * @param string      $string
195
     * @param string|null $type
196
     * @param string|null $fg
197
     * @param string|null $bg
198
     * @param string|null $prefix
199
     * @param bool        $padding
200
     *
201
     * @return ImagineStyleInterface
202
     */
203
    private function block(string $string, string $type = null, string $fg = null, string $bg = null, string $prefix = null, bool $padding = true): ImagineStyleInterface
204
    {
205
        if (!$this->decoration) {
206
            return $this->plainBlock($string, $type);
207
        }
208
209
        $this->io->block($string, $type, sprintf('fg=%s;bg=%s', $fg ?: 'default', $bg ?: 'default'), $prefix ? sprintf(' %s ', $prefix) : ' ', $padding);
210
211
        return $this;
212
    }
213
214
    /**
215
     * @param string $string
216
     * @param string $type
217
     *
218
     * @return ImagineStyleInterface
219
     */
220
    private function plainBlock(string $string, string $type): ImagineStyleInterface
221
    {
222
        return $this
223
            ->newline()
224
            ->line('[%s] %s', [$type, $string])
225
            ->newline();
226
    }
227
228
    /**
229
     * @param string $format
230
     * @param array  $replacements
231
     *
232
     * @return string
233
     */
234
    private function compileString(string $format, array $replacements = []): string
235
    {
236
        if (!$this->decoration) {
237
            $format = strip_tags($format);
238
        }
239
240
        if (0 === count($replacements)) {
241
            return $format;
242
        }
243
244
        if (false !== $compiled = @vsprintf($format, $replacements)) {
245
            return $compiled;
246
        }
247
248
        throw new InvalidArgumentException(
249
            sprintf('Invalid string format "%s" or replacements "%s".', $format, implode(', ', array_map(function ($replacement) {
250
                return var_export($replacement, true);
251
            }, $replacements)))
252
        );
253
    }
254
}
255