Test Failed
Push — master ( 2d8dc6...3a3a08 )
by Théo
03:32 queued 01:28
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 1
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 Humbug\PhpScoper\Throwable\Exception\ParsingException;
18
use Symfony\Component\Console\Application;
19
use Symfony\Component\Console\Helper\ProgressBar;
20
use Symfony\Component\Console\Output\NullOutput;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Style\SymfonyStyle;
23
24
/**
25
 * @private
26
 * @final
27
 */
28
class ConsoleLogger
29
{
30
    private $application;
31
    private $io;
32
    private $startTime;
33
    private $progressBar;
34
35
    public function __construct(Application $application, SymfonyStyle $io)
36
    {
37
        $this->io = $io;
38
        $this->application = $application;
39
        $this->startTime = microtime(true);
40
        $this->progressBar = new ProgressBar(new NullOutput());
41
    }
42
43
    /**
44
     * @param string   $prefix
45
     * @param string[] $paths
46
     */
47
    public function outputScopingStart(string $prefix, array $paths)
48
    {
49
        $this->io->writeln($this->application->getHelp());
50
51
        $newLine = 1;
52
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
53
            $this->io->section('Input');
54
            $this->io->writeln(
55
                sprintf(
56
                    'Prefix: %s',
57
                    $prefix
58
                )
59
            );
60
            $this->io->writeln('Paths:');
61
            $this->io->listing($paths);
62
            $this->io->section('Processing');
63
            $newLine = 0;
64
        }
65
66
        $this->io->newLine($newLine);
67
    }
68
69
    /**
70
     * Output file count message if relevant.
71
     *
72
     * @param int $count
73
     */
74
    public function outputFileCount(int $count)
75
    {
76
        if (OutputInterface::VERBOSITY_NORMAL === $this->io->getVerbosity()) {
77
            $this->progressBar = $this->io->createProgressBar($count);
78
            $this->progressBar->start();
79
        } elseif ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
80
            $this->progressBar = new ProgressBar(new NullOutput());
81
        }
82
    }
83
84
    /**
85
     * Output scoping success message.
86
     *
87
     * @param string $path
88
     */
89 View Code Duplication
    public function outputSuccess(string $path)
90
    {
91
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
92
            $this->io->writeln(
93
                sprintf(
94
                    ' * [<info>OK</info>] %s',
95
                    $path
96
                )
97
            );
98
        }
99
100
        $this->progressBar->advance();
101
    }
102
103
    public function outputWarnOfFailure(string $path, ParsingException $exception)
104
    {
105
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
106
            $this->io->writeln(
107
                sprintf(
108
                    ' * [<error>NO</error>] %s',
109
                    $path
110
                )
111
            );
112
        }
113
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
114
            $this->io->writeln(
115
                sprintf(
116
                    "\t".'%s: %s',
117
                    $exception->getMessage(),
118
                    $exception->getPrevious()
119
                )
120
            );
121
        }
122
123
        $this->progressBar->advance();
124
    }
125
126
    /**
127
     * Output scoping failure message.
128
     *
129
     * @param string $path
130
     */
131 View Code Duplication
    public function outputFail(string $path)
132
    {
133
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
134
            $this->io->writeln(
135
                sprintf(
136
                    ' * [<error>FA</error>] %s',
137
                    $path
138
                )
139
            );
140
        }
141
142
        $this->progressBar->advance();
143
    }
144
145
    public function outputScopingEnd()
146
    {
147
        $this->finish(false);
148
    }
149
150
    public function outputScopingEndWithFailure()
151
    {
152
        $this->finish(true);
153
    }
154
155
    private function finish(bool $failed)
156
    {
157
        $this->progressBar->finish();
158
        $this->io->newLine(2);
159
160
        if (false === $failed) {
161
            $this->io->success(
162
                sprintf(
163
                    'Successfully prefixed %d files.',
164
                    $this->progressBar->getMaxSteps()
165
                )
166
            );
167
        }
168
169
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
170
            $this->io->comment(
171
                sprintf(
172
                    '<info>Memory usage: %.2fMB (peak: %.2fMB), time: %.2fs<info>',
173
                    round(memory_get_usage() / 1024 / 1024, 2),
174
                    round(memory_get_peak_usage() / 1024 / 1024, 2),
175
                    round(microtime(true) - $this->startTime, 2)
176
                )
177
            );
178
        }
179
    }
180
}
181