Test Failed
Push — master ( e53a66...43458c )
by Francimar
16:26
created

Log   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 2
dl 0
loc 172
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getInstance() 0 7 2
A getDirectory() 0 4 1
A setDirectory() 0 9 2
A setHandler() 0 9 2
A toArray() 0 6 1
A fromArray() 0 14 4
A error() 0 4 1
A warning() 0 4 1
A debug() 0 4 1
A info() 0 4 1
1
<?php
2
/**
3
 * MIT License
4
 *
5
 * Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA
6
 *
7
 * @author Francimar Alves <[email protected]>
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
namespace NFe\Logger;
29
30
/**
31
 * Salva mensagens de erro, depuração entre outros
32
 * 
33
 * @author Francimar Alves <[email protected]>
34
 */
35
class Log
36
{
37
    /**
38
     * Pasta onde será salvo os arquivos de log
39
     * 
40
     * @var string
41
     */
42
    private $directory;
43
44
    /**
45
     * Processador de log
46
     * 
47
     * @var \Monolog\Logger
48
     */
49
    private $logger;
50
51
    /**
52
     * Instância salva para uso estático
53
     * 
54
     * @var Log
55
     */
56
    private static $instance;
57
58
    /**
59
     * Cria uma instância de Log
60
     * @param array $log campos para preencher o log
61
     */
62 2
    public function __construct($log = [])
63
    {
64 2
        $this->logger = new \Monolog\Logger('NFeAPI');
65 2
        $this->fromArray($log);
66 2
    }
67
68
    /**
69
     * Get current or create a new instance
70
     * @return Log current instance
71
     */
72 11
    public static function getInstance()
73
    {
74 11
        if (is_null(self::$instance)) {
75 1
            self::$instance = new self();
76
        }
77 11
        return self::$instance;
78
    }
79
80
    /**
81
     * Pasta onde serão salvos os arquivos de Log
82
     * @return string diretório atual onde os logs são salvos
83
     */
84 12
    public function getDirectory()
85
    {
86 12
        return $this->directory;
87
    }
88
89
    /**
90
     * Informa a nova pasta onde os logs serão salvos
91
     * @param string $directory caminho absoluto da pasta
92
     * @return Log a própria instência de Log
93
     */
94 3
    public function setDirectory($directory)
95
    {
96 3
        if ($this->directory == $directory) {
97 1
            return $this;
98
        }
99 2
        $this->directory = $directory;
100 2
        $this->setHandler(null);
101 2
        return $this;
102
    }
103
104
    /**
105
     * Altera o gerenciador que escreve os logs, informe null para restaurar o padrão
106
     * @param \Monolog\Handler\AbstractHandler $write_function nova função que será usada
0 ignored issues
show
Bug introduced by
There is no parameter named $write_function. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
107
     * @return Log a própria instência de Log
108
     */
109 12
    public function setHandler($handler)
110
    {
111 12
        if ($handler === null) {
112 12
            $handler = new \Monolog\Handler\RotatingFileHandler($this->getDirectory() . '/{date}.txt');
113 12
            $handler->setFilenameFormat('{date}', 'Ymd');
114
        }
115 12
        $this->logger->pushHandler($handler);
116 12
        return $this;
117
    }
118
119
    /**
120
     * Converte a instância da classe para um array de campos com valores
121
     * @param boolean $recursive informa se os campos devem ser convertidos para array
122
     * @return array Array contendo todos os campos e valores da instância
123
     */
124 1
    public function toArray($recursive = false)
0 ignored issues
show
Unused Code introduced by
The parameter $recursive is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126 1
        $log = [];
127 1
        $log['directory'] = $this->getDirectory();
128 1
        return $log;
129
    }
130
131
    /**
132
     * Atribui os valores do array para a instância atual
133
     * @param array|Log $log Array ou instância de Log, para copiar os valores
134
     * @return Log A própria instância da classe
135
     */
136 3
    public function fromArray($log = [])
137
    {
138 3
        if ($log instanceof Log) {
139 1
            $log = $log->toArray();
140 3
        } elseif (!is_array($log)) {
141 1
            return $this;
142
        }
143 3
        if (!isset($log['directory'])) {
144 2
            $this->setDirectory(dirname(dirname(dirname(__DIR__))).'/storage/logs');
145
        } else {
146 1
            $this->setDirectory($log['directory']);
147
        }
148 3
        return $this;
149
    }
150
151
    /**
152
     * Adds a log record at the ERROR level.
153
     *
154
     * This method allows for compatibility with common interfaces.
155
     *
156
     * @param  string  $message The log message
157
     * @param  array   $context The log context
158
     * @return Boolean Whether the record has been processed
159
     */
160 4
    public static function error($message, $context = [])
161
    {
162 4
        return self::getInstance()->logger->error($message, $context);
163
    }
164
165
    /**
166
     * Adds a log record at the WARNING level.
167
     *
168
     * This method allows for compatibility with common interfaces.
169
     *
170
     * @param  string  $message The log message
171
     * @param  array   $context The log context
172
     * @return Boolean Whether the record has been processed
173
     */
174 2
    public static function warning($message, $context = [])
175
    {
176 2
        return self::getInstance()->logger->warning($message, $context);
177
    }
178
179
    /**
180
     * Adds a log record at the DEBUG level.
181
     *
182
     * This method allows for compatibility with common interfaces.
183
     *
184
     * @param  string  $message The log message
185
     * @param  array   $context The log context
186
     * @return Boolean Whether the record has been processed
187
     */
188 2
    public static function debug($message, $context = [])
189
    {
190 2
        return self::getInstance()->logger->debug($message, $context);
191
    }
192
193
    /**
194
     * Adds a log record at the INFO level.
195
     *
196
     * This method allows for compatibility with common interfaces.
197
     *
198
     * @param  string  $message The log message
199
     * @param  array   $context The log context
200
     * @return Boolean Whether the record has been processed
201
     */
202 1
    public static function info($message, $context = [])
203
    {
204 1
        return self::getInstance()->logger->info($message, $context);
205
    }
206
}
207