GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 784a42...226f28 )
by Gjero
03:03
created

Logger::init()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 10
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 17
rs 8.2222
1
<?php
2
/**
3
 * Pimf
4
 *
5
 * @copyright Copyright (c)  Gjero Krsteski (http://krsteski.de)
6
 * @license   http://opensource.org/licenses/MIT MIT
7
 */
8
9
namespace Pimf;
10
11
use Pimf\Adapter\File;
12
13
/**
14
 * Logger with common logging options into a file.
15
 *
16
 * @package Pimf
17
 * @author  Gjero Krsteski <[email protected]>
18
 */
19
class Logger
20
{
21
    /**
22
     * @var resource
23
     */
24
    private $handle;
25
26
    /**
27
     * @var resource
28
     */
29
    private $warnHandle;
30
31
    /**
32
     * @var resource
33
     */
34
    private $errorHandle;
35
36
    /**
37
     * @var string
38
     */
39
    private $storageDir;
40
41
    /**
42
     * @var string
43
     */
44
    private static $remoteIp;
45
46
    /**
47
     * @var string
48
     */
49
    private static $script;
50
51
    /**
52
     * @param string $remoteIp
53
     * @param string $script
54
     */
55
    public static function setup($remoteIp, $script)
56
    {
57
        self::$remoteIp = $remoteIp;
58
        self::$script = $script;
59
    }
60
61
    /**
62
     * @param string $localeStorageDir Use better the local TMP dir or dir with mod 777.
63
     */
64
    public function __construct($localeStorageDir)
65
    {
66
        $this->storageDir = (string)$localeStorageDir;
67
    }
68
69
    /**
70
     * @throws \RuntimeException If something went wrong on creating the log dir and file.
71
     */
72
    public function init()
73
    {
74
        if (is_resource($this->errorHandle)
75
            && is_resource($this->handle)
76
            && is_resource($this->warnHandle)
77
        ) {
78
            return;
79
        }
80
81
        $this->handle = (new File($this->storageDir, "pimf-logs.txt"))->open();
82
        $this->warnHandle = (new File($this->storageDir, "pimf-warnings.txt"))->open();
83
        $this->errorHandle = (new File($this->storageDir, "pimf-errors.txt"))->open();
84
85
        if (!$this->errorHandle || !$this->handle || !$this->warnHandle) {
86
            throw new \RuntimeException("failed to obtain a handle to logger file");
87
        }
88
    }
89
90
    /**
91
     * @param string $msg
92
     *
93
     * @return Logger
94
     */
95
    public function debug($msg)
96
    {
97
        if ($this->iniGetBool('display_errors') === true) {
98
            $this->write((string)$msg, 'DEBUG');
99
        }
100
101
        return $this;
102
    }
103
104
    /**
105
     * @param string $msg
106
     *
107
     * @return Logger
108
     */
109
    public function warn($msg)
110
    {
111
        if ($this->iniGetBool('display_errors') === true) {
112
            $this->write((string)$msg, 'WARNING');
113
        }
114
115
        return $this;
116
    }
117
118
    /**
119
     * @param string $msg
120
     *
121
     * @return Logger
122
     */
123
    public function error($msg)
124
    {
125
        $this->write((string)$msg, 'ERROR');
126
127
        return $this;
128
    }
129
130
    /**
131
     * @param string $msg
132
     *
133
     * @return Logger
134
     */
135
    public function info($msg)
136
    {
137
        if ($this->iniGetBool('display_errors') === true) {
138
            $this->write((string)$msg, 'INFO');
139
        }
140
141
        return $this;
142
    }
143
144
    /**
145
     * @param string $msg
146
     * @param string $severity
147
     */
148
    protected function write($msg, $severity = 'DEBUG')
149
    {
150
        $msg = $this->format($msg, $severity);
151
152
        if ($severity == 'WARNING') {
153
            fwrite($this->warnHandle, $msg);
154
        } elseif ($severity == 'ERROR') {
155
            fwrite($this->errorHandle, $msg);
156
        } else {
157
            fwrite($this->handle, $msg);
158
159
        }
160
    }
161
162
    public function __destruct()
163
    {
164
        if (is_resource($this->handle)
165
            && is_resource($this->warnHandle)
166
            && is_resource($this->errorHandle)
167
        ) {
168
169
            if (fclose($this->handle) === false) {
170
                $this->error('Logger failed to close the handle to the log file');
171
            }
172
173
            fclose($this->warnHandle);
174
            fclose($this->errorHandle);
175
        }
176
    }
177
178
    /**
179
     * Formats the error message in representable manner.
180
     *
181
     * @param string $message
182
     * @param string $severity
183
     *
184
     * @return string
185
     */
186
    private function format($message, $severity)
187
    {
188
        $msg = date("m-d-Y") . " " . date("G:i:s") . " " . self::$remoteIp;
189
190
        $IPLength = strlen(self::$remoteIp);
191
        $numWhitespaces = 15 - $IPLength;
192
193
        for ($i = 0; $i < $numWhitespaces; $i++) {
194
            $msg .= " ";
195
        }
196
197
        $msg .= " " . $severity . ": ";
198
199
        $lastSlashIndex = strrpos(self::$script, "/");
200
        $fileName = self::$script;
201
202
        if ($lastSlashIndex !== false) {
203
            $fileName = substr(self::$script, $lastSlashIndex + 1);
204
        }
205
206
        $msg .= $fileName . "\t";
207
        $msg .= ": " . $message . "\r\n";
208
209
        return $msg;
210
    }
211
212
    /**
213
     * @param string $varname
214
     *
215
     * @return bool
216
     */
217
    protected function iniGetBool($varname)
218
    {
219
        $varvalue = ini_get($varname);
220
221
        switch (strtolower($varvalue)) {
222
            case 'on':
223
            case 'yes':
224
            case 'true':
225
                return 'assert.active' !== $varname;
226
            case 'stdout':
227
            case 'stderr':
228
                return 'display_errors' === $varname;
229
            default:
230
                return (bool)(int)$varvalue;
231
        }
232
    }
233
}
234