Failed Conditions
Push — master ( 746c21...bb3715 )
by Kentaro
35:35
created

SecurityController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 15.69 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 5
dl 16
loc 102
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F index() 16 99 16

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
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
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
36
{
37
    public function index(Application $app, Request $request)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
38
    {
39
40
        $builder = $app['form.factory']->createBuilder('admin_security');
41
        $form = $builder->getForm();
42
43
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
121
            }
122
        } else {
123
            // セキュリティ情報の取得
124
            $form->get('admin_route_dir')->setData($app['config']['admin_route']);
125
            $allowHost = $app['config']['admin_allow_host'];
126
            if (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']);
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
130
        }
131
132
        return $app->render('Setting/System/security.twig', array(
133
            'form' => $form->createView(),
134
        ));
135
    }
136
}
137