Failed Conditions
Pull Request — master (#1861)
by k-yamamura
501:04 queued 494:33
created

EccubeMonologHelper::getHandler()   F

Complexity

Conditions 15
Paths 1024

Size

Total Lines 99
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 62
CRAP Score 15

Importance

Changes 0
Metric Value
dl 0
loc 99
ccs 62
cts 62
cp 1
rs 2
c 0
b 0
f 0
cc 15
eloc 57
nc 1024
nop 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
 * 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
        $logDir = isset($app['config']['log_realdir']) ? $app['config']['log_realdir'].'/' : $app['config']['root_dir'].'/app/log/';
84 1061
        $filename = $logDir.$logFileName.'.log';
85 1061
        $RotateHandler = new RotatingFileHandler($filename, $maxFiles, $level);
86 1061
        $RotateHandler->setFilenameFormat(
87 1061
            $logFileName.$delimiter.'{date}'.$app['config']['log']['suffix'],
88
            $dateFormat
89 1061
        );
90
91
        // ログフォーマットの設定(設定ファイルで定義)
92 1061
        $RotateHandler->setFormatter(new LineFormatter($logFormat.PHP_EOL, $logDateFormat, true, true));
93
94
        // FingerCossedHandlerの設定
95 1061
        $FingerCrossedHandler = new FingersCrossedHandler(
96 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...
97 1061
            new ErrorLevelActivationStrategy($levels[$actionLevel])
98 1061
        );
99
100
101
        // Processorの内容をログ出力
102 1061
        $webProcessor = new EccubeWebProcessor();
103 1061
        $uidProcessor = new UidProcessor(8);
104
105 1062
        $FingerCrossedHandler->pushProcessor(function ($record) use ($app, $uidProcessor, $webProcessor) {
106
            // ログフォーマットに出力する値を独自に設定
107
108 1062
            $record['level_name'] = sprintf("%-5s", $record['level_name']);
109
110
            // セッションIDと会員IDを設定
111 1062
            $record['session_id'] = null;
112 1062
            $record['user_id'] = null;
113 1062
            if ($app->isBooted()) {
114 1061
                if (isset($app['session'])) {
115 1058
                    $record['session_id'] = substr(sha1($app['session']->getId()), 0, 8);
116 1058
                }
117 1061
                $user = $app->user();
118 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...
119 381
                    $record['user_id'] = $user->getId();
120 381
                }
121 1061
            }
122
123 1062
            $record['uid'] = $uidProcessor->getUid();
124
125 1062
            $record['url'] = $webProcessor->getRequestUri();
126 1062
            $record['ip'] = $webProcessor->getClientIp();
127 1062
            $record['referrer'] = $webProcessor->getReferer();
128 1062
            $record['method'] = $webProcessor->getMethod();
129 1062
            $record['user_agent'] = $webProcessor->getUserAgent();
130
131
            // クラス名などを一旦保持し、不要な情報は削除
132 1062
            $line = $record['extra']['line'];
133 1062
            $functionName = $record['extra']['function'];
134
            // php5.3だとclass名が取得できないため、ファイル名を元に出力
135
            // $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...
136 1062
            $className = $record['extra']['file'];
137
138
            // 不要な情報を削除
139 1062
            unset($record['extra']['file']);
140 1062
            unset($record['extra']['line']);
141 1062
            unset($record['extra']['class']);
142 1062
            unset($record['extra']['function']);
143
144 1062
            $record['class'] = pathinfo($className, PATHINFO_FILENAME);
145 1062
            $record['function'] = $functionName;
146 1062
            $record['line'] = $line;
147
148 1062
            return $record;
149 1061
        });
150
151
        // クラス名等を取得するProcessor、ログ出力時にクラス名を無視するための設定を行っている
152 1061
        $intro = new IntrospectionProcessor(Logger::DEBUG, array('Psr\\Log\\', 'EccubeLog'));
153 1061
        $FingerCrossedHandler->pushProcessor($intro);
154
155 1061
        return $FingerCrossedHandler;
156
157
    }
158
159
}
160