LogTrait::log()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Yawik project.
5
 *
6
 *      (c) Anthonius Munthi
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Yawik\Composer;
13
14
use Composer\IO\IOInterface;
15
use Psr\Log\LoggerInterface;
16
use Psr\Log\LogLevel;
17
use Symfony\Component\Console\Input\StringInput;
18
use Symfony\Component\Console\Output\ConsoleOutput;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Yawik\Composer\Event\PreConfigureEvent;
22
23
trait LogTrait
24
{
25
26
    /**
27
     * @var OutputInterface
28
     */
29
    protected $output;
30
31
    /**
32
     * @var InputInterface
33
     */
34
    protected $input;
35
36
    /**
37
     * @var LoggerInterface
38
     */
39
    private $logger;
40
41 1
    public function onPreConfigureEvent(PreConfigureEvent $event)
42
    {
43 1
        $this->setOutputFromComposerIO($event->getOutput());
44 1
    }
45
46 4
    public function setOutputFromComposerIO(IOInterface $output)
47
    {
48 4
        $level   = OutputInterface::VERBOSITY_NORMAL;
49
50 4
        if ($output->isVeryVerbose() || $output->isDebug()) {
51 2
            $level = OutputInterface::VERBOSITY_VERY_VERBOSE;
52 2
        } elseif ($output->isVerbose()) {
53 1
            $level = OutputInterface::VERBOSITY_VERBOSE;
54
        }
55
56 4
        $this->getOutput()->setVerbosity($level);
57 4
        $this->getOutput()->setDecorated($output->isDecorated());
58 4
    }
59
60 1
    public function getInput()
61
    {
62 1
        if (is_null($this->input)) {
63 1
            $this->input = new StringInput('');
64
        }
65 1
        return $this->input;
66
    }
67
68
    /**
69
     * @param InputInterface $input
70
     * @return $this
71
     */
72 1
    public function setInput($input)
73
    {
74 1
        $this->input = $input;
75 1
        return $this;
76
    }
77
78
    /**
79
     * @return OutputInterface
80
     */
81 8
    public function getOutput()
82
    {
83 8
        if (is_null($this->output)) {
84 1
            $this->output = new ConsoleOutput();
85
        }
86 8
        return $this->output;
87
    }
88
89
    /**
90
     * @param OutputInterface $output
91
     * @return $this
92
     */
93 7
    public function setOutput($output)
94
    {
95 7
        $this->output = $output;
96 7
        return $this;
97
    }
98
99
100
    /**
101
     * Set a logger to use
102
     * @param LoggerInterface $logger
103
     * @return $this
104
     */
105 3
    public function setLogger(LoggerInterface $logger)
106
    {
107 3
        $this->logger = $logger;
108
109 3
        return $this;
110
    }
111
112
    /**
113
     * @param string $message
114
     */
115 1
    public function logDebug($message, $context = 'yawik')
116
    {
117 1
        $this->doLog(LogLevel::DEBUG, $message, $context);
118 1
    }
119
120
    /**
121
     * @param string $message
122
     */
123 1
    public function logError($message, $context = 'yawik')
124
    {
125 1
        $this->doLog(LogLevel::ERROR, $message, $context);
126 1
    }
127
128
    /**
129
     * @param string $message
130
     * @param string $context
131
     */
132 1
    public function log($message, $context = 'yawik')
133
    {
134 1
        $this->doLog(LogLevel::INFO, $message, $context);
135 1
    }
136
137 3
    public function isCli()
138
    {
139 3
        return php_sapi_name() === 'cli';
140
    }
141
142 3
    public function doLog($level, $message, $context = 'yawik')
143
    {
144 3
        $message = str_replace(getcwd().DIRECTORY_SEPARATOR, '', $message);
145 3
        if (is_object($this->logger)) {
146 3
            $this->logger->log($level, $message, [$context]);
147
        }
148 3
        if ($this->isCli()) {
149
            switch ($level) {
150 3
                case LogLevel::DEBUG:
151 1
                    $outputLevel = OutputInterface::VERBOSITY_VERY_VERBOSE;
152 1
                    break;
153 2
                case LogLevel::ERROR:
154 1
                    $message = '<error>'.$message.'</error>';
155 1
                    $outputLevel = OutputInterface::OUTPUT_NORMAL;
156 1
                    break;
157 1
                case LogLevel::INFO:
158
                default:
159 1
                    $outputLevel = OutputInterface::OUTPUT_NORMAL;
160 1
                    break;
161
            }
162 3
            $this->doWrite($message, $outputLevel, $context);
163
        }
164 3
    }
165
166 3
    public function doWrite($message, $outputLevel = 0, $context = 'yawik')
167
    {
168 3
        $message = sprintf(
169 3
            '<info>[%s]</info> %s',
170 3
            $context,
171 3
            $message
172
        );
173 3
        $this->getOutput()->writeln($message, $outputLevel);
174 3
    }
175
}
176