Completed
Push — master ( c523cc...f6da78 )
by Francimar
05:08
created

Logger::errorLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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\Log;
29
30
/**
31
 * Salva mensagens de erro, depuração entre outros
32
 */
33
class Logger
34
{
35
    private $directory;
36
    private $write_function;
37
    private static $instance;
38
39 2
    public function __construct($logger = array())
40
    {
41 2
        $this->fromArray($logger);
42 2
        $this->setWriteFunction(array($this, 'writeLog'));
0 ignored issues
show
Documentation introduced by
array($this, 'writeLog') is of type array<integer,this<NFe\L...Logger>","1":"string"}>, but the function expects a object<NFe\Log\function>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43 2
    }
44
45 9
    public static function getInstance()
46
    {
47 9
        if (is_null(self::$instance)) {
48 1
            self::$instance = new self();
49 1
        }
50 9
        return self::$instance;
51
    }
52
53
    /**
54
     * Pasta onde serão salvos os arquivos de Log
55
     */
56 1
    public function getDirectory()
57
    {
58 1
        return $this->directory;
59
    }
60
61 3
    public function setDirectory($directory)
62
    {
63 3
        $this->directory = $directory;
64 3
        return $this;
65
    }
66
67
    /**
68
     * Altera a função que escreve os logs
69
     * @param function $write_function nova função que será usada
70
     */
71 10
    public function setWriteFunction($write_function)
72
    {
73 10
        $this->write_function = $write_function;
74 10
        return $this;
75
    }
76
77 1
    public function toArray()
78
    {
79 1
        $logger = array();
80 1
        $logger['directory'] = $this->getDirectory();
81 1
        return $logger;
82
    }
83
84 3
    public function fromArray($logger = array())
85
    {
86 3
        if ($logger instanceof Logger) {
87 1
            $logger = $logger->toArray();
88 3
        } elseif (!is_array($logger)) {
89 1
            return $this;
90
        }
91 3
        if (!isset($logger['directory'])) {
92 2
            $this->setDirectory(dirname(dirname(dirname(__DIR__))).'/docs/logs');
93 2
        } else {
94 1
            $this->setDirectory($logger['directory']);
95
        }
96 3
        return $this;
97
    }
98
99
    private function writeLog($type, $message)
100
    {
101
        $filename = $this->getDirectory().'/'.date('Ymd').'.txt';
102
        $fp = @fopen($filename, 'a');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $fp. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
103
        if (!$fp) {
104
            throw new \Exception('Não foi possível abrir o arquivo de log "'.$filename.'"', 500);
105
        }
106
        fwrite($fp, date('d/m/Y H:i:s').' - '.$type.': '.$message."\n");
107
        fclose($fp);
108
        chmod($filename, 0755);
109
    }
110
111 2
    private function write($type, $message)
0 ignored issues
show
Unused Code introduced by
The parameter $type 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...
Unused Code introduced by
The parameter $message 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...
112
    {
113 2
        if (!is_null($this->write_function)) {
114 2
            call_user_func_array($this->write_function, func_get_args());
115 2
        }
116 2
    }
117
118 1
    private function errorLog($message)
119
    {
120 1
        $this->write('error', $message);
121 1
    }
122
123 2
    private function warningLog($message)
124
    {
125 2
        $this->write('warning', $message);
126 2
    }
127
128 1
    private function debugLog($message)
129
    {
130 1
        $this->write('debug', $message);
131 1
    }
132
133 1
    private function informationLog($message)
134
    {
135 1
        $this->write('information', $message);
136 1
    }
137
138 2
    public function __call($name, $arguments)
139
    {
140
        switch ($name) {
141 2
            case 'error':
142 2
            case 'warning':
143 2
            case 'debug':
144 2
            case 'information':
145 1
                return call_user_func_array(array($this, $name . 'Log'), $arguments);
146 1
            default:
147 1
                throw new \BadMethodCallException(
148 1
                    sprintf('Call to undefined function: %s->%s().', get_class($this), $name),
149
                    500
150 1
                );
151 1
        }
152
    }
153
154 3
    public static function __callStatic($name, $arguments)
155
    {
156
        switch ($name) {
157 3
            case 'error':
158 3
            case 'warning':
159 3
            case 'debug':
160 3
            case 'information':
161 2
                return call_user_func_array(array(self::getInstance(), $name . 'Log'), $arguments);
162 1
            default:
163 1
                throw new \BadMethodCallException(
164 1
                    sprintf('Call to undefined function: %s::%s().', __CLASS__, $name),
165
                    500
166 1
                );
167 1
        }
168
    }
169
}
170