Failed Conditions
Pull Request — master (#1861)
by k-yamamura
474:27 queued 464:07
created

EccubeMonologHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 9
dl 0
loc 119
ccs 62
cts 62
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F getHandler() 0 96 12
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Monolog\Helper;
25
26
use Eccube\Application;
27
use Eccube\Monolog\Processor\EccubeWebProcessor;
28
use Monolog\Formatter\LineFormatter;
29
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
30
use Monolog\Handler\FingersCrossedHandler;
31
use Monolog\Handler\RotatingFileHandler;
32
use Monolog\Logger;
33
use Monolog\Processor\IntrospectionProcessor;
34
use Monolog\Processor\UidProcessor;
35
use Symfony\Component\Security\Core\User\UserInterface;
36
37
class EccubeMonologHelper
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
38
{
39
40
    /** @var  \Eccube\Application */
41
    protected $app;
42
43
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
44
     * EccubeMonologHelper constructor.
45
     *
46
     */
47 1062
    public function __construct($app)
48
    {
49 1062
        $this->app = $app;
50 1062
    }
51
52
    /**
53
     * log.ymlの内容に応じたHandlerの設定を行う
54
     *
55
     * @param array $channelValues
56
     * @return FingersCrossedHandler
57
     */
58 1062
    public function getHandler(array $channelValues)
59
    {
60 1062
        $app = $this->app;
61
62 1062
        $levels = Logger::getLevels();
63
64
        // ファイル名などの設定を行い、設定がなければデフォルト値を設定
65 1062
        $logFileName = isset($channelValues['filename']) ? $channelValues['filename'] : $app['config']['log']['filename'];
66 1062
        $delimiter = isset($channelValues['delimiter']) ? $channelValues['delimiter'] : $app['config']['log']['delimiter'];
67 1062
        $dateFormat = isset($channelValues['dateformat']) ? $channelValues['dateformat'] : $app['config']['log']['dateformat'];
68 1062
        $logLevel = isset($channelValues['log_level']) ? $channelValues['log_level'] : $app['config']['log']['log_level'];
69 1062
        $actionLevel = isset($channelValues['action_level']) ? $channelValues['action_level'] : $app['config']['log']['action_level'];
70 1062
        $maxFiles = isset($channelValues['max_files']) ? $channelValues['max_files'] : $app['config']['log']['max_files'];
71 1062
        $logDateFormat = isset($channelValues['log_dateformat']) ? $channelValues['log_dateformat'] : $app['config']['log']['log_dateformat'];
72 1062
        $logFormat = isset($channelValues['log_format']) ? $channelValues['log_format'] : $app['config']['log']['log_format'];
73
74 1062
        if ($app['debug']) {
75 1055
            $level = Logger::DEBUG;
76 1055
        } else {
77 7
            $level = $logLevel;
78
        }
79
80
81
        // RotateHandlerの設定
82 1062
        $filename = $app['config']['root_dir'].'/app/log/'.$logFileName.'.log';
83 1062
        $RotateHandler = new RotatingFileHandler($filename, $maxFiles, $level);
84 1062
        $RotateHandler->setFilenameFormat(
85 1062
            $logFileName.$delimiter.'{date}'.$app['config']['log']['suffix'],
86
            $dateFormat
87 1062
        );
88
89
        // ログフォーマットの設定(設定ファイルで定義)
90 1062
        $RotateHandler->setFormatter(new LineFormatter($logFormat."\n", $logDateFormat, true, true));
91
92
        // FingerCossedHandlerの設定
93 1062
        $FingerCrossedHandler = new FingersCrossedHandler(
94 1062
            $RotateHandler,
0 ignored issues
show
Documentation introduced by
$RotateHandler is of type object<Monolog\Handler\RotatingFileHandler>, but the function expects a callable.

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...
95 1062
            new ErrorLevelActivationStrategy($levels[$actionLevel])
96 1062
        );
97
98
99
        // Processorの内容をログ出力
100 1062
        $webProcessor = new EccubeWebProcessor();
101 1062
        $uidProcessor = new UidProcessor(8);
102
103 1062
        $FingerCrossedHandler->pushProcessor(function ($record) use ($app, $uidProcessor, $webProcessor) {
104
            // ログフォーマットに出力する値を独自に設定
105
106 1062
            $record['level_name'] = sprintf("%-5s", $record['level_name']);
107
108
            // セッションIDと会員IDを設定
109 1062
            $record['session_id'] = null;
110 1062
            $record['user_id'] = null;
111 1062
            if ($app->isBooted()) {
112 1061
                $record['session_id'] = substr(sha1($app['session']->getId()), 0, 8);
113 1058
                $user = $app->user();
114 1058
                if ($user instanceof UserInterface) {
115 381
                    $record['user_id'] = $user->getId();
0 ignored issues
show
Bug introduced by
The method getId() does not seem to exist on object<Symfony\Component...ore\User\UserInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116 381
                }
117 1058
            }
118
119 1059
            $record['uid'] = $uidProcessor->getUid();
120
121 1059
            $record['url'] = $webProcessor->getRequestUri();
122 1059
            $record['ip'] = $webProcessor->getClientIp();
123 1059
            $record['referrer'] = $webProcessor->getReferer();
124 1059
            $record['method'] = $webProcessor->getMethod();
125 1059
            $record['user_agent'] = $webProcessor->getUserAgent();
126
127
            // クラス名などを一旦保持し、不要な情報は削除
128 1059
            $line = $record['extra']['line'];
129 1059
            $functionName = $record['extra']['function'];
130
            // php5.3だとclass名が取得できないため、ファイル名を元に出力
131
            // $className = $record['extra']['class'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
132 1059
            $className = $record['extra']['file'];
133
134
            // 不要な情報を削除
135 1059
            unset($record['extra']['file']);
136 1059
            unset($record['extra']['line']);
137 1059
            unset($record['extra']['class']);
138 1059
            unset($record['extra']['function']);
139
140 1059
            $record['class'] = pathinfo($className, PATHINFO_FILENAME);
141 1059
            $record['function'] = $functionName;
142 1059
            $record['line'] = $line;
143
144 1059
            return $record;
145 1062
        });
146
147
        // クラス名等を取得するProcessor、ログ出力時にクラス名を無視するための設定を行っている
148 1062
        $intro = new IntrospectionProcessor(Logger::DEBUG, array('Psr\\Log\\', 'EccubeLog'));
149 1062
        $FingerCrossedHandler->pushProcessor($intro);
150
151 1062
        return $FingerCrossedHandler;
152
153
    }
154
155
}
156