Failed Conditions
Pull Request — experimental/3.1 (#2494)
by chihiro
49:51 queued 21:25
created

SecurityController::index()   C

Complexity

Conditions 9
Paths 27

Size

Total Lines 87
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 48
nc 27
nop 2
dl 0
loc 87
rs 5.3053
c 1
b 0
f 0
ccs 0
cts 43
cp 0
crap 90

How to fix   Long Method   

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
25
namespace Eccube\Controller\Admin\Setting\System;
26
27
use Eccube\Annotation\Component;
28
use Eccube\Annotation\Inject;
29
use Eccube\Application;
30
use Eccube\Common\Constant;
31
use Eccube\Controller\AbstractController;
32
use Eccube\Form\Type\Admin\SecurityType;
33
use Eccube\Util\Str;
34
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
35
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
36
use Symfony\Component\Filesystem\Filesystem;
37
use Symfony\Component\Finder\Finder;
38
use Symfony\Component\Form\FormFactory;
39
use Symfony\Component\HttpFoundation\Request;
40
41
/**
42
 * @Component
43
 * @Route(service=SecurityController::class)
44
 */
45
class SecurityController extends AbstractController
46
{
47
    /**
48
     * @Inject("config")
49
     * @var array
50
     */
51
    protected $appConfig;
52
53
    /**
54
     * @Inject("form.factory")
55
     * @var FormFactory
56
     */
57
    protected $formFactory;
58
59
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
60
     * @Route("/{_admin}/setting/system/security", name="admin_setting_system_security")
61
     * @Template("Setting/System/security.twig")
62
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
63
    public function index(Application $app, Request $request)
64
    {
65
66
        $builder = $this->formFactory->createBuilder(SecurityType::class);
67
        $form = $builder->getForm();
68
69
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
70
71
            $form->handleRequest($request);
72
73
            if ($form->isValid()) {
74
                $data = $form->getData();
75
76
                // 現在のセキュリティ情報を更新
77
                $adminRoot = $this->appConfig['admin_route'];
78
79
                $configFile = $this->appConfig['root_dir'].'/app/config/eccube/config.php';
80
                $config = require $configFile;
81
82
                // trim処理
83
                $allowHost = Str::convertLineFeed($data['admin_allow_host']);
84
                if (empty($allowHost)) {
85
                    $config['admin_allow_host'] = null;
86
                } else {
87
                    $config['admin_allow_host'] = explode("\n", $allowHost);
88
                }
89
90
                if ($data['force_ssl']) {
91
                    // SSL制限にチェックをいれた場合、https経由で接続されたか確認
92
                    if ($request->isSecure()) {
93
                        // httpsでアクセスされたらSSL制限をチェック
94
                        $config['force_ssl'] = Constant::ENABLED;
95
                    } else {
96
                        // httpから変更されたらfalseのまま
97
                        $config['force_ssl'] = Constant::DISABLED;
98
                        $data['force_ssl'] = (bool)Constant::DISABLED;
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
99
                    }
100
                } else {
101
                    $config['force_ssl'] = Constant::DISABLED;
102
                }
103
                $form = $builder->getForm();
104
                $form->setData($data);
105
106
                file_put_contents($configFile, sprintf('<?php return %s', var_export($config, true)).';');
107
108
                // ルーティングのキャッシュを削除
109
                $cacheDir = $this->appConfig['root_dir'].'/app/cache/routing';
110
                if (file_exists($cacheDir)) {
111
                    $finder = Finder::create()->in($cacheDir);
112
                    $filesystem = new Filesystem();
113
                    $filesystem->remove($finder);
114
                }
115
116
                if ($adminRoot != $data['admin_route_dir']) {
117
                    // admin_routeが変更されればpath.phpを更新
118
                    $pathFile = $this->appConfig['root_dir'].'/app/config/eccube/path.php';
119
                    $config = require $pathFile;
120
                    $config['admin_route'] = $data['admin_route_dir'];
121
122
                    file_put_contents($pathFile, sprintf('<?php return %s', var_export($config, true)).';');
123
124
                    $app->addSuccess('admin.system.security.route.dir.complete', 'admin');
125
126
                    // ログアウト
127
                    $this->getSecurity($app)->setToken(null);
128
129
                    // 管理者画面へ再ログイン
130
                    return $app->redirect($request->getBaseUrl().'/'.$config['admin_route']);
131
                }
132
133
                $app->addSuccess('admin.system.security.save.complete', 'admin');
134
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
135
            }
136
        } else {
137
            // セキュリティ情報の取得
138
            $form->get('admin_route_dir')->setData($this->appConfig['admin_route']);
139
            $allowHost = $this->appConfig['admin_allow_host'];
140
            if (count($allowHost) > 0) {
141
                $form->get('admin_allow_host')->setData(Str::convertLineFeed(implode("\n", $allowHost)));
142
            }
143
            $form->get('force_ssl')->setData((bool)$this->appConfig['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...
144
        }
145
146
        return [
147
            'form' => $form->createView(),
148
        ];
149
    }
150
}
151