Passed
Push — master ( e0f6a7...4e7cde )
by Théo
02:22
created

ScoperLogger::outputFail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ScoperLogger::outputScopingEndWithFailure() 0 3 1
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\Console;
16
17
use Humbug\PhpScoper\Throwable\Exception\ParsingException;
18
use Symfony\Component\Console\Application as SymfonyApplication;
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 ScoperLogger
29
{
30
    private $application;
31
    private $io;
32
    private $startTime;
33
    private $progressBar;
34
35
    public function __construct(SymfonyApplication $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): void
48
    {
49
        $this->io->writeln($this->application->getHelp());
50
51
        $newLine = 1;
52
53
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
54
            $this->io->section('Input');
55
56
            $this->io->writeln(
57
                sprintf(
58
                    'Prefix: %s',
59
                    $prefix
60
                )
61
            );
62
63
            $this->io->write('Paths:');
64
65
            if (0 === count($paths)) {
66
                $this->io->writeln(' Loaded from config');
67
            } else {
68
                $this->io->writeln('');
69
                $this->io->listing($paths);
70
            }
71
72
            $this->io->section('Processing');
73
            $newLine = 0;
74
        }
75
76
        $this->io->newLine($newLine);
77
    }
78
79
    /**
80
     * Output file count message if relevant.
81
     *
82
     * @param int $count
83
     */
84
    public function outputFileCount(int $count): void
85
    {
86
        if (OutputInterface::VERBOSITY_NORMAL === $this->io->getVerbosity()) {
87
            $this->progressBar = $this->io->createProgressBar($count);
88
            $this->progressBar->start();
89
        } elseif ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
90
            $this->progressBar = new ProgressBar(new NullOutput());
91
        }
92
    }
93
94
    /**
95
     * Output scoping success message.
96
     *
97
     * @param string $path
98
     */
99
    public function outputSuccess(string $path): void
100
    {
101
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
102
            $this->io->writeln(
103
                sprintf(
104
                    ' * [<info>OK</info>] %s',
105
                    $path
106
                )
107
            );
108
        }
109
110
        $this->progressBar->advance();
111
    }
112
113
    public function outputWarnOfFailure(string $path, ParsingException $exception): void
114
    {
115
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
116
            $this->io->writeln(
117
                sprintf(
118
                    ' * [<error>NO</error>] %s',
119
                    $path
120
                )
121
            );
122
        }
123
124
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
125
            $previous = $exception->getPrevious();
126
127
            $this->io->writeln(
128
                sprintf(
129
                    "\t".'%s: %s',
130
                    $exception->getMessage(),
131
                    null === $previous ? '' : $previous->getMessage()
132
                )
133
            );
134
        }
135
136
        $this->progressBar->advance();
137
    }
138
139
    public function outputScopingEnd(): void
140
    {
141
        $this->finish(false);
142
    }
143
144
    public function outputScopingEndWithFailure(): void
145
    {
146
        $this->finish(true);
147
    }
148
149
    private function finish(bool $failed): void
150
    {
151
        $this->progressBar->finish();
152
        $this->io->newLine(2);
153
154
        if (false === $failed) {
155
            $this->io->success(
156
                sprintf(
157
                    'Successfully prefixed %d files.',
158
                    $this->progressBar->getMaxSteps()
159
                )
160
            );
161
        }
162
163
        if ($this->io->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) {
164
            $this->io->comment(
165
                sprintf(
166
                    '<info>Memory usage: %.2fMB (peak: %.2fMB), time: %.2fs<info>',
167
                    round(memory_get_usage() / 1024 / 1024, 2),
168
                    round(memory_get_peak_usage() / 1024 / 1024, 2),
169
                    round(microtime(true) - $this->startTime, 2)
170
                )
171
            );
172
        }
173
    }
174
}
175