Failed Conditions
Push — master ( d1d972...0addd3 )
by chihiro
500:16 queued 493:07
created

EccubeHelper::getHandler()   F

Complexity

Conditions 15
Paths 512

Size

Total Lines 101
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 64
CRAP Score 15

Importance

Changes 0
Metric Value
cc 15
eloc 58
nc 512
nop 1
dl 0
loc 101
rs 3.0055
c 0
b 0
f 0
ccs 64
cts 64
cp 1
crap 15

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
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23
 */
24
25
namespace Eccube\Log\Monolog\Helper;
26
27
use Eccube\Entity\Customer;
28
use Eccube\Entity\Member;
29
use Eccube\Log\Monolog\Processor\WebProcessor;
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 EccubeHelper
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 1068
    public function __construct($app)
49
    {
50 1068
        $this->app = $app;
51 1068
    }
52
53
    /**
54
     * log.ymlの内容に応じたHandlerの設定を行う
55
     *
56
     * @param array $channelValues
57
     * @return FingersCrossedHandler
58
     */
59 1068
    public function getHandler(array $channelValues)
60
    {
61 1068
        $app = $this->app;
62
63 1068
        $levels = Logger::getLevels();
64
65
        // ファイル名などの設定を行い、設定がなければデフォルト値を設定
66 1068
        $logFileName = isset($channelValues['filename']) ? $channelValues['filename'] : $app['config']['log']['filename'];
67 1068
        $delimiter = isset($channelValues['delimiter']) ? $channelValues['delimiter'] : $app['config']['log']['delimiter'];
68 1068
        $dateFormat = isset($channelValues['dateformat']) ? $channelValues['dateformat'] : $app['config']['log']['dateformat'];
69 1068
        $logLevel = isset($channelValues['log_level']) ? $channelValues['log_level'] : $app['config']['log']['log_level'];
70 1068
        $actionLevel = isset($channelValues['action_level']) ? $channelValues['action_level'] : $app['config']['log']['action_level'];
71 1068
        $maxFiles = isset($channelValues['max_files']) ? $channelValues['max_files'] : $app['config']['log']['max_files'];
72 1068
        $logDateFormat = isset($channelValues['log_dateformat']) ? $channelValues['log_dateformat'] : $app['config']['log']['log_dateformat'];
73 1068
        $logFormat = isset($channelValues['log_format']) ? $channelValues['log_format'] : $app['config']['log']['log_format'];
74
75 1068
        if ($app['debug']) {
76 1061
            $level = Logger::DEBUG;
77 1061
        } else {
78 7
            $level = $logLevel;
79
        }
80
81
82
        // RotateHandlerの設定
83 1068
        $filename = $app['config']['root_dir'].'/app/log/'.$logFileName.'.log';
84 1068
        $RotateHandler = new RotatingFileHandler($filename, $maxFiles, $level);
85 1068
        $RotateHandler->setFilenameFormat(
86 1068
            $logFileName.$delimiter.'{date}'.$app['config']['log']['suffix'],
87
            $dateFormat
88 1068
        );
89
90
        // ログフォーマットの設定(設定ファイルで定義)
91 1068
        $RotateHandler->setFormatter(new LineFormatter($logFormat.PHP_EOL, $logDateFormat, true, true));
92
93
        // FingerCossedHandlerの設定
94 1068
        $FingerCrossedHandler = new FingersCrossedHandler(
95 1068
            $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 1068
            new ErrorLevelActivationStrategy($levels[$actionLevel])
97 1068
        );
98
99
100
        // Processorの内容をログ出力
101 1068
        $webProcessor = new WebProcessor();
102 1068
        $uidProcessor = new UidProcessor(8);
103
104 1068
        $FingerCrossedHandler->pushProcessor(function ($record) use ($app, $uidProcessor, $webProcessor) {
105
            // ログフォーマットに出力する値を独自に設定
106
107 1068
            $record['level_name'] = sprintf("%-5s", $record['level_name']);
108
109
            // セッションIDと会員IDを設定
110 1068
            $record['session_id'] = null;
111 1068
            $record['user_id'] = null;
112 1068
            if ($app->isBooted()) {
113 1067
                if (isset($app['session'])) {
114 1064
                    $sessionId = $app['session']->getId();
115 1064
                    if ($sessionId) {
116 522
                        $record['session_id'] = substr(sha1($sessionId), 0, 8);
117 522
                    }
118 1064
                }
119 1067
                $user = $app->user();
120 1067
                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...
121 383
                    $record['user_id'] = $user->getId();
122 383
                }
123 1067
            }
124
125 1068
            $record['uid'] = $uidProcessor->getUid();
126
127 1068
            $record['url'] = $webProcessor->getRequestUri();
128 1068
            $record['ip'] = $webProcessor->getClientIp();
129 1068
            $record['referrer'] = $webProcessor->getReferer();
130 1068
            $record['method'] = $webProcessor->getMethod();
131 1068
            $record['user_agent'] = $webProcessor->getUserAgent();
132
133
            // クラス名などを一旦保持し、不要な情報は削除
134 1068
            $line = $record['extra']['line'];
135 1068
            $functionName = $record['extra']['function'];
136
            // php5.3だとclass名が取得できないため、ファイル名を元に出力
137
            // $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...
138 1068
            $className = $record['extra']['file'];
139
140
            // 不要な情報を削除
141 1068
            unset($record['extra']['file']);
142 1068
            unset($record['extra']['line']);
143 1068
            unset($record['extra']['class']);
144 1068
            unset($record['extra']['function']);
145
146 1068
            $record['class'] = pathinfo($className, PATHINFO_FILENAME);
147 1068
            $record['function'] = $functionName;
148 1068
            $record['line'] = $line;
149
150 1068
            return $record;
151 1068
        });
152
153
        // クラス名等を取得するProcessor、ログ出力時にクラス名を無視するための設定を行っている
154 1068
        $intro = new IntrospectionProcessor(Logger::DEBUG, array('Psr\\Log\\', 'EccubeLog'));
155 1068
        $FingerCrossedHandler->pushProcessor($intro);
156
157 1068
        return $FingerCrossedHandler;
158
159
    }
160
161
}
162