Failed Conditions
Pull Request — master (#1861)
by k-yamamura
33:00
created

EccubeMonologHelper::getHandler()   F

Complexity

Conditions 14
Paths 512

Size

Total Lines 98
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 62
CRAP Score 14

Importance

Changes 0
Metric Value
dl 0
loc 98
ccs 62
cts 62
cp 1
rs 3.0216
c 0
b 0
f 0
cc 14
eloc 56
nc 512
nop 1
crap 14

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Entity\Customer;
28
use Eccube\Entity\Member;
29
use Eccube\Monolog\Processor\EccubeWebProcessor;
30
use Monolog\Formatter\LineFormatter;
31
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
32
use Monolog\Handler\FingersCrossedHandler;
33
use Monolog\Handler\RotatingFileHandler;
34
use Monolog\Logger;
35
use Monolog\Processor\IntrospectionProcessor;
36
use Monolog\Processor\UidProcessor;
37
38
class EccubeMonologHelper
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
39
{
40
41
    /** @var  \Eccube\Application */
42
    protected $app;
43
44
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
45
     * EccubeMonologHelper constructor.
46
     *
47
     */
48 1061
    public function __construct($app)
49
    {
50 1061
        $this->app = $app;
51 1061
    }
52
53
    /**
54
     * log.ymlの内容に応じたHandlerの設定を行う
55
     *
56
     * @param array $channelValues
57
     * @return FingersCrossedHandler
58
     */
59 1062
    public function getHandler(array $channelValues)
60
    {
61 1061
        $app = $this->app;
62
63 1061
        $levels = Logger::getLevels();
64
65
        // ファイル名などの設定を行い、設定がなければデフォルト値を設定
66 1061
        $logFileName = isset($channelValues['filename']) ? $channelValues['filename'] : $app['config']['log']['filename'];
67 1061
        $delimiter = isset($channelValues['delimiter']) ? $channelValues['delimiter'] : $app['config']['log']['delimiter'];
68 1061
        $dateFormat = isset($channelValues['dateformat']) ? $channelValues['dateformat'] : $app['config']['log']['dateformat'];
69 1061
        $logLevel = isset($channelValues['log_level']) ? $channelValues['log_level'] : $app['config']['log']['log_level'];
70 1061
        $actionLevel = isset($channelValues['action_level']) ? $channelValues['action_level'] : $app['config']['log']['action_level'];
71 1061
        $maxFiles = isset($channelValues['max_files']) ? $channelValues['max_files'] : $app['config']['log']['max_files'];
72 1061
        $logDateFormat = isset($channelValues['log_dateformat']) ? $channelValues['log_dateformat'] : $app['config']['log']['log_dateformat'];
73 1061
        $logFormat = isset($channelValues['log_format']) ? $channelValues['log_format'] : $app['config']['log']['log_format'];
74
75 1061
        if ($app['debug']) {
76 1054
            $level = Logger::DEBUG;
77 1054
        } else {
78 7
            $level = $logLevel;
79
        }
80
81
82
        // RotateHandlerの設定
83 1061
        $filename = $app['config']['root_dir'].'/app/log/'.$logFileName.'.log';
84 1061
        $RotateHandler = new RotatingFileHandler($filename, $maxFiles, $level);
85 1061
        $RotateHandler->setFilenameFormat(
86 1061
            $logFileName.$delimiter.'{date}'.$app['config']['log']['suffix'],
87 1061
            $dateFormat
88
        );
89 1061
90
        // ログフォーマットの設定(設定ファイルで定義)
91
        $RotateHandler->setFormatter(new LineFormatter($logFormat.PHP_EOL, $logDateFormat, true, true));
92 1061
93
        // FingerCossedHandlerの設定
94
        $FingerCrossedHandler = new FingersCrossedHandler(
95 1061
            $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...
96 1061
            new ErrorLevelActivationStrategy($levels[$actionLevel])
97 1061
        );
98 1061
99
100
        // Processorの内容をログ出力
101
        $webProcessor = new EccubeWebProcessor();
102 1061
        $uidProcessor = new UidProcessor(8);
103 1061
104
        $FingerCrossedHandler->pushProcessor(function ($record) use ($app, $uidProcessor, $webProcessor) {
105 1062
            // ログフォーマットに出力する値を独自に設定
106
107
            $record['level_name'] = sprintf("%-5s", $record['level_name']);
108 1062
109
            // セッションIDと会員IDを設定
110
            $record['session_id'] = null;
111 1062
            $record['user_id'] = null;
112 1062
            if ($app->isBooted()) {
113 1062
                if (isset($app['session'])) {
114 1061
                    $record['session_id'] = substr(sha1($app['session']->getId()), 0, 8);
115 1058
                }
116 1058
                $user = $app->user();
117 1061
                if ($user instanceof Customer || $user instanceof Member) {
0 ignored issues
show
Bug introduced by
The class Eccube\Entity\Customer does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
Bug introduced by
The class Eccube\Entity\Member does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
118 1061
                    $record['user_id'] = $user->getId();
119 381
                }
120 381
            }
121 1061
122
            $record['uid'] = $uidProcessor->getUid();
123 1062
124
            $record['url'] = $webProcessor->getRequestUri();
125 1062
            $record['ip'] = $webProcessor->getClientIp();
126 1062
            $record['referrer'] = $webProcessor->getReferer();
127 1062
            $record['method'] = $webProcessor->getMethod();
128 1062
            $record['user_agent'] = $webProcessor->getUserAgent();
129 1062
130
            // クラス名などを一旦保持し、不要な情報は削除
131
            $line = $record['extra']['line'];
132 1062
            $functionName = $record['extra']['function'];
133 1062
            // php5.3だとclass名が取得できないため、ファイル名を元に出力
134
            // $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...
135
            $className = $record['extra']['file'];
136 1062
137
            // 不要な情報を削除
138
            unset($record['extra']['file']);
139 1062
            unset($record['extra']['line']);
140 1062
            unset($record['extra']['class']);
141 1062
            unset($record['extra']['function']);
142 1062
143
            $record['class'] = pathinfo($className, PATHINFO_FILENAME);
144 1062
            $record['function'] = $functionName;
145 1062
            $record['line'] = $line;
146 1062
147
            return $record;
148 1062
        });
149 1061
150
        // クラス名等を取得するProcessor、ログ出力時にクラス名を無視するための設定を行っている
151
        $intro = new IntrospectionProcessor(Logger::DEBUG, array('Psr\\Log\\', 'EccubeLog'));
152 1061
        $FingerCrossedHandler->pushProcessor($intro);
153 1061
154
        return $FingerCrossedHandler;
155 1061
156
    }
157
158
}
159