Completed
Push — master ( 0ab984...c72dd5 )
by Théo
02:46
created

ConsoleLogger::finish()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 1
dl 0
loc 25
ccs 0
cts 14
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\Logger;
16
17
use Symfony\Component\Console\Application;
18
use Symfony\Component\Console\Helper\ProgressBar;
19
use Symfony\Component\Console\Output\NullOutput;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Console\Style\SymfonyStyle;
22
23
/**
24
 * @private
25
 * @final
26
 */
27
class ConsoleLogger
28
{
29
    private $application;
30
    private $io;
31
    private $startTime;
32
    private $progressBar;
33
34
    public function __construct(Application $application, SymfonyStyle $io)
35
    {
36
        $this->io = $io;
37
        $this->application = $application;
38
        $this->startTime = microtime(true);
39
        $this->progressBar = new ProgressBar(new NullOutput());
40
    }
41
42
    /**
43
     * @param string   $prefix
44
     * @param string[] $paths
45
     */
46
    public function outputScopingStart(string $prefix, array $paths)
47
    {
48
        $this->io->writeln($this->application->getHelp());
49
50
        $newLine = 1;
51
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
52
            $this->io->section('Input');
53
            $this->io->writeln(
54
                sprintf(
55
                    'Prefix: %s',
56
                    $prefix
57
                )
58
            );
59
            $this->io->writeln('Paths:');
60
            $this->io->listing($paths);
61
            $this->io->section('Processing');
62
            $newLine = 0;
63
        }
64
65
        $this->io->newLine($newLine);
66
    }
67
68
    /**
69
     * Output file count message if relevant.
70
     *
71
     * @param int $count
72
     */
73
    public function outputFileCount(int $count)
74
    {
75
        if (OutputInterface::VERBOSITY_NORMAL === $this->io->getVerbosity()) {
76
            $this->progressBar = $this->io->createProgressBar($count);
77
            $this->progressBar->start(0);
78
        } elseif ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
79
            $this->progressBar = new ProgressBar(new NullOutput());
80
        }
81
    }
82
83
    /**
84
     * Output scoping success message.
85
     *
86
     * @param string $path
87
     */
88 View Code Duplication
    public function outputSuccess(string $path)
89
    {
90
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
91
            $this->io->writeln(
92
                sprintf(
93
                    ' * [<info>OK</info>] %s',
94
                    $path
95
                )
96
            );
97
        }
98
99
        $this->progressBar->advance();
100
    }
101
102
    /**
103
     * Output scoping failure message.
104
     *
105
     * @param string $path
106
     */
107 View Code Duplication
    public function outputFail(string $path)
108
    {
109
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
110
            $this->io->writeln(
111
                sprintf(
112
                    ' * [<error>FA</error>] %s',
113
                    $path
114
                )
115
            );
116
        }
117
118
        $this->progressBar->advance();
119
    }
120
121
    public function outputScopingEnd()
122
    {
123
        $this->finish(false);
124
    }
125
126
    public function outputScopingEndWithFailure()
127
    {
128
        $this->finish(true);
129
    }
130
131
    private function finish(bool $failed)
132
    {
133
        $this->progressBar->finish();
134
        $this->io->newLine(2);
135
136
        if (false === $failed) {
137
            $this->io->success(
138
                sprintf(
139
                    'Successfully prefixed %d files.',
140
                    $this->progressBar->getMaxSteps()
141
                )
142
            );
143
        }
144
145
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
146
            $this->io->comment(
147
                sprintf(
148
                    '<info>Memory usage: %.2fMB (peak: %.2fMB), time: %.2fs<info>',
149
                    round(memory_get_usage() / 1024 / 1024, 2),
150
                    round(memory_get_peak_usage() / 1024 / 1024, 2),
151
                    round(microtime(true) - $this->startTime, 2)
152
                )
153
            );
154
        }
155
    }
156
}
157