Failed Conditions
Pull Request — master (#1873)
by Tsuyoshi
273:59 queued 266:59
created

EccubeHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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 1062
    public function __construct($app)
49
    {
50 1062
        $this->app = $app;
51 1062
    }
52
53
    /**
54
     * log.ymlの内容に応じたHandlerの設定を行う
55
     *
56
     * @param array $channelValues
57
     * @return FingersCrossedHandler
58
     */
59 1062
    public function getHandler(array $channelValues)
60
    {
61 1062
        $app = $this->app;
62
63 1062
        $levels = Logger::getLevels();
64
65
        // ファイル名などの設定を行い、設定がなければデフォルト値を設定
66 1062
        $logFileName = isset($channelValues['filename']) ? $channelValues['filename'] : $app['config']['log']['filename'];
67 1062
        $delimiter = isset($channelValues['delimiter']) ? $channelValues['delimiter'] : $app['config']['log']['delimiter'];
68 1062
        $dateFormat = isset($channelValues['dateformat']) ? $channelValues['dateformat'] : $app['config']['log']['dateformat'];
69 1062
        $logLevel = isset($channelValues['log_level']) ? $channelValues['log_level'] : $app['config']['log']['log_level'];
70 1062
        $actionLevel = isset($channelValues['action_level']) ? $channelValues['action_level'] : $app['config']['log']['action_level'];
71 1062
        $maxFiles = isset($channelValues['max_files']) ? $channelValues['max_files'] : $app['config']['log']['max_files'];
72 1062
        $logDateFormat = isset($channelValues['log_dateformat']) ? $channelValues['log_dateformat'] : $app['config']['log']['log_dateformat'];
73 1062
        $logFormat = isset($channelValues['log_format']) ? $channelValues['log_format'] : $app['config']['log']['log_format'];
74
75 1062
        if ($app['debug']) {
76 1055
            $level = Logger::DEBUG;
77 1055
        } else {
78 7
            $level = $logLevel;
79
        }
80
81
82
        // RotateHandlerの設定
83 1062
        $filename = $app['config']['root_dir'].'/app/log/'.$logFileName.'.log';
84 1062
        $RotateHandler = new RotatingFileHandler($filename, $maxFiles, $level);
85 1062
        $RotateHandler->setFilenameFormat(
86 1062
            $logFileName.$delimiter.'{date}'.$app['config']['log']['suffix'],
87
            $dateFormat
88 1062
        );
89
90
        // ログフォーマットの設定(設定ファイルで定義)
91 1062
        $RotateHandler->setFormatter(new LineFormatter($logFormat.PHP_EOL, $logDateFormat, true, true));
92
93
        // FingerCossedHandlerの設定
94 1062
        $FingerCrossedHandler = new FingersCrossedHandler(
95 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...
96 1062
            new ErrorLevelActivationStrategy($levels[$actionLevel])
97 1062
        );
98
99
100
        // Processorの内容をログ出力
101 1062
        $webProcessor = new WebProcessor();
102 1062
        $uidProcessor = new UidProcessor(8);
103
104 1062
        $FingerCrossedHandler->pushProcessor(function ($record) use ($app, $uidProcessor, $webProcessor) {
105
            // ログフォーマットに出力する値を独自に設定
106
107 1062
            $record['level_name'] = sprintf("%-5s", $record['level_name']);
108
109
            // セッションIDと会員IDを設定
110 1062
            $record['session_id'] = null;
111 1062
            $record['user_id'] = null;
112 1062
            if ($app->isBooted()) {
113 1061
                if (isset($app['session'])) {
114 1058
                    $sessionId = $app['session']->getId();
115 1058
                    if ($sessionId) {
116 520
                        $record['session_id'] = substr(sha1($sessionId), 0, 8);
117 520
                    }
118 1058
                }
119 1061
                $user = $app->user();
120 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...
121 381
                    $record['user_id'] = $user->getId();
122 381
                }
123 1061
            }
124
125 1062
            $record['uid'] = $uidProcessor->getUid();
126
127 1062
            $record['url'] = $webProcessor->getRequestUri();
128 1062
            $record['ip'] = $webProcessor->getClientIp();
129 1062
            $record['referrer'] = $webProcessor->getReferer();
130 1062
            $record['method'] = $webProcessor->getMethod();
131 1062
            $record['user_agent'] = $webProcessor->getUserAgent();
132
133
            // クラス名などを一旦保持し、不要な情報は削除
134 1062
            $line = $record['extra']['line'];
135 1062
            $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 1062
            $className = $record['extra']['file'];
139
140
            // 不要な情報を削除
141 1062
            unset($record['extra']['file']);
142 1062
            unset($record['extra']['line']);
143 1062
            unset($record['extra']['class']);
144 1062
            unset($record['extra']['function']);
145
146 1062
            $record['class'] = pathinfo($className, PATHINFO_FILENAME);
147 1062
            $record['function'] = $functionName;
148 1062
            $record['line'] = $line;
149
150 1062
            return $record;
151 1062
        });
152
153
        // クラス名等を取得するProcessor、ログ出力時にクラス名を無視するための設定を行っている
154 1062
        $intro = new IntrospectionProcessor(Logger::DEBUG, array('Psr\\Log\\', 'EccubeLog'));
155 1062
        $FingerCrossedHandler->pushProcessor($intro);
156
157 1062
        return $FingerCrossedHandler;
158
159
    }
160
161
}
162