SecurityController::index()   F
last analyzed

Complexity

Conditions 19
Paths 1014

Size

Total Lines 104

Duplication

Lines 16
Ratio 15.38 %

Importance

Changes 0
Metric Value
cc 19
nc 1014
nop 2
dl 16
loc 104
rs 0.2799
c 0
b 0
f 0

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) EC-CUBE CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.ec-cube.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
25
namespace Eccube\Controller\Admin\Setting\System;
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Util\Str;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\Yaml\Yaml;
33
34
35
class SecurityController extends AbstractController
36
{
37
    public function index(Application $app, Request $request)
38
    {
39
40
        $builder = $app['form.factory']->createBuilder('admin_security');
41
        $form = $builder->getForm();
42
43
        if ('POST' === $request->getMethod()) {
44
45
            $form->handleRequest($request);
46
47
            if ($form->isValid()) {
48
                $data = $form->getData();
49
50
                // 現在のセキュリティ情報を更新
51
                $adminRoot = $app['config']['admin_route'];
52
53
                $configFile = $app['config']['root_dir'].'/app/config/eccube/config';
54 View Code Duplication
                if (file_exists($configFile.'.php')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
                    $config = require $configFile.'.php';
56
                } elseif (file_exists($configFile.'.yml')) {
57
                    $config = Yaml::parse(file_get_contents($configFile.'.yml'));
58
                }
59
60
                // trim処理
61
                $allowHost = Str::convertLineFeed($data['admin_allow_host']);
62
                if (empty($allowHost)) {
63
                    $config['admin_allow_host'] = null;
0 ignored issues
show
Bug introduced by
The variable $config does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
64
                } else {
65
                    $config['admin_allow_host'] = explode("\n", $allowHost);
66
                }
67
68
                if ($data['force_ssl']) {
69
                    // SSL制限にチェックをいれた場合、https経由で接続されたか確認
70
                    if ($request->isSecure()) {
71
                        // httpsでアクセスされたらSSL制限をチェック
72
                        $config['force_ssl'] = Constant::ENABLED;
73
                    } else {
74
                        // httpから変更されたらfalseのまま
75
                        $config['force_ssl'] = Constant::DISABLED;
76
                        $data['force_ssl'] = (bool) Constant::DISABLED;
77
                    }
78
                } else {
79
                    $config['force_ssl'] = Constant::DISABLED;
80
                }
81
                $form = $builder->getForm();
82
                $form->setData($data);
83
84 View Code Duplication
                if (file_exists($configFile.'.php')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
                    file_put_contents($configFile.'.php', sprintf('<?php return %s', var_export($config, true)).';');
86
                }
87
                if (file_exists($configFile.'.yml')) {
88
                    file_put_contents($configFile.'.yml', Yaml::dump($config));
89
                }
90
91
                if ($adminRoot != $data['admin_route_dir']) {
92
                    // admin_routeが変更されればpath.(yml|php)を更新
93
                    $pathFile = $app['config']['root_dir'].'/app/config/eccube/path';
94
95 View Code Duplication
                    if (file_exists($pathFile.'.php')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
                        $config = require $pathFile.'.php';
97
                    } elseif (file_exists($pathFile.'.yml')) {
98
                        $config = Yaml::parse(file_get_contents($pathFile.'.yml'));
99
                    }
100
101
                    $config['admin_route'] = $data['admin_route_dir'];
102
103 View Code Duplication
                    if (file_exists($pathFile.'.php')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
                        file_put_contents($pathFile.'.php', sprintf('<?php return %s', var_export($config, true)).';');
105
                    }
106
                    if (file_exists($pathFile.'.yml')) {
107
                        file_put_contents($pathFile.'.yml', Yaml::dump($config));
108
                    }
109
110
                    $app->addSuccess('admin.system.security.route.dir.complete', 'admin');
111
112
                    // ログアウト
113
                    $this->getSecurity($app)->setToken(null);
114
115
                    // 管理者画面へ再ログイン
116
                    return $app->redirect($request->getBaseUrl().'/'.$config['admin_route']);
117
                }
118
119
                $app->addSuccess('admin.system.security.save.complete', 'admin');
120
121
            }
122
        } else {
123
            // セキュリティ情報の取得
124
            $form->get('admin_route_dir')->setData($app['config']['admin_route']);
125
            $allowHost = $app['config']['admin_allow_host'];
126
            if (is_array($allowHost) && count($allowHost) > 0) {
127
                $form->get('admin_allow_host')->setData(Str::convertLineFeed(implode("\n", $allowHost)));
128
            }
129
            $form->get('force_ssl')->setData((bool)$app['config']['force_ssl']);
130
        }
131
132
        // 管理画面URLのチェック
133
        if (isset($app['config']['admin_route']) && $app['config']['admin_route'] == 'admin') {
134
            $app->addWarning('admin.system.security.admin.url.warning', 'admin');
135
        }
136
137
        return $app->render('Setting/System/security.twig', array(
138
            'form' => $form->createView(),
139
        ));
140
    }
141
}
142