Passed
Pull Request — master (#1)
by ANTHONIUS
02:28
created

Output::stripFileName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the dotfiles project.
7
 *
8
 *     (c) Anthonius Munthi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Dotfiles\Core\Console;
15
16
use Dotfiles\Core\Config\Config;
17
use Symfony\Component\Console\Output\ConsoleOutput;
18
19
class Output extends ConsoleOutput
20
{
21
    /**
22
     * @var Config
23
     */
24
    private $config;
25
26
    public function __construct(Config $config)
27
    {
28
        parent::__construct();
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function write($messages, $newline = false, $options = \Symfony\Component\Console\Output\Output::OUTPUT_NORMAL): void
36
    {
37
        $messages = $this->stripFileName($messages);
38
        parent::write($messages, $newline, $options);
39
    }
40
41
    /**
42
     * Strip long filename to be shorter.
43
     *
44
     * @param string $messages
45
     *
46
     * @return string
47
     */
48
    private function stripFileName(string $messages): string
49
    {
50
        $config = $this->config;
51
        $dirs = array(
52
            $config->get('dotfiles.home_dir') => '$home_dir',
53
            $config->get('dotfiles.temp_dir') => '$temp_dir',
54
            $config->get('dotfiles.bin_dir') => '$dotfiles_dir/bin',
55
        );
56
        if (getcwd() !== ($dir = $config->get('dotfiles.repo_dir'))) {
57
            $dirs[$dir] = '$repo_dir';
58
        }
59
60
        $messages = strtr($messages, $dirs);
61
62
        return $messages;
63
    }
64
}
65