Failed Conditions
Push — experimental/3.1 ( 3d2ede...2919b9 )
by Yangsin
28:59
created

NewsController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 214
Duplicated Lines 38.32 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 82
loc 214
ccs 49
cts 69
cp 0.71
rs 10
c 1
b 0
f 0
wmc 13
lcom 1
cbo 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 22 22 1
B edit() 0 53 6
A up() 17 17 2
A down() 17 17 2
B delete() 26 26 2

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
namespace Eccube\Controller\Admin\Content;
25
26
use Eccube\Annotation\Component;
27
use Eccube\Annotation\Inject;
28
use Eccube\Application;
29
use Eccube\Common\Constant;
30
use Eccube\Controller\AbstractController;
31
use Eccube\Entity\News;
32
use Eccube\Event\EccubeEvents;
33
use Eccube\Event\EventArgs;
34
use Eccube\Form\Type\Admin\NewsType;
35
use Eccube\Repository\NewsRepository;
36
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
37
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
38
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
39
use Symfony\Component\EventDispatcher\EventDispatcher;
40
use Symfony\Component\Form\FormFactory;
41
use Symfony\Component\HttpFoundation\Request;
42
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
43
44
/**
45
 * 新着情報のコントローラクラス
46
 *
47
 * @Component
48
 * @Route(service=NewsController::class)
49
 */
50
class NewsController extends AbstractController
51
{
52
    /**
53
     * @Inject("form.factory")
54
     * @var FormFactory
55
     */
56
    protected $formFactory;
57
58
    /**
59
     * @Inject("eccube.event.dispatcher")
60
     * @var EventDispatcher
61
     */
62
    protected $eventDispatcher;
63
64
    /**
65
     * @Inject(NewsRepository::class)
66
     * @var NewsRepository
67
     */
68
    protected $newsRepository;
69
70
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
71
     * 新着情報一覧を表示する。
72
     *
73
     * @Route("/{_admin}/content/news", name="admin_content_news")
74
     * @Template("Content/news.twig")
75
     *
76
     * @param Application $app
77
     * @return \Symfony\Component\HttpFoundation\Response
78
     */
79 1 View Code Duplication
    public function index(Application $app, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
80
    {
81 1
        $NewsList = $this->newsRepository->findBy(array(), array('rank' => 'DESC'));
82
83 1
        $builder = $this->formFactory->createBuilder();
84
85 1
        $event = new EventArgs(
86
            array(
87 1
                'builder' => $builder,
88 1
                'NewsList' => $NewsList,
89
            ),
90 1
            $request
91
        );
92 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_INDEX_INITIALIZE, $event);
93
94 1
        $form = $builder->getForm();
95
96
        return [
97 1
            'form' => $form->createView(),
98 1
            'NewsList' => $NewsList,
99
        ];
100
    }
101
102
    /**
103
     * 新着情報を登録・編集する。
104
     *
105
     * @Route("/{_admin}/content/news/new", name="admin_content_news_new")
106
     * @Route("/{_admin}/content/news/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_news_edit")
107
     * @Template("Content/news_edit.twig")
108
     *
109
     * @param Application $app
110
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
111
     * @param null $id
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
112
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
113
     */
114 2
    public function edit(Application $app, Request $request, $id = null)
115
    {
116 2
        if ($id) {
117 1
            $News = $this->newsRepository->find($id);
118 1
            if (!$News) {
119 1
                throw new NotFoundHttpException();
120
            }
121
        } else {
122 1
            $News = new \Eccube\Entity\News();
123
        }
124
125 2
        $News->setLinkMethod((bool)$News->getLinkMethod());
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
126
127 2
        $builder = $this->formFactory
128 2
            ->createBuilder(NewsType::class, $News);
129
130 2
        $event = new EventArgs(
131
            array(
132 2
                'builder' => $builder,
133 2
                'News' => $News,
134
            ),
135 2
            $request
136
        );
137 2
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_INITIALIZE, $event);
138
139 2
        $form = $builder->getForm();
140 2
        $form->handleRequest($request);
141
142 2
        if ($form->isSubmitted() && $form->isValid()) {
143
            if (!$News->getUrl()) {
144
                $News->setLinkMethod(Constant::DISABLED);
145
            }
146
            $this->newsRepository->save($News);
147
148
            $event = new EventArgs(
149
                array(
150
                    'form' => $form,
151
                    'News' => $News,
152
                ),
153
                $request
154
            );
155
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_COMPLETE, $event);
156
157
            $app->addSuccess('admin.news.save.complete', 'admin');
158
159
            return $app->redirect($app->url('admin_content_news'));
160
        }
161
162
        return [
163 2
            'form' => $form->createView(),
164 2
            'News' => $News,
165
        ];
166
    }
167
168
    /**
169
     * 指定した新着情報の表示順を1つ上げる。
170
     *
171
     * @Method("PUT")
172
     * @Route("/{_admin}/content/news/{id}/up", requirements={"id" = "\d+"}, name="admin_content_news_up")
173
     *
174
     * @param Application $app
175
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
176
     * @param News $News
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
177
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
178
     */
179 1 View Code Duplication
    public function up(Application $app, Request $request, News $News)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
180
    {
181 1
        $this->isTokenValid($app);
182
183
        try {
184 1
            $this->newsRepository->up($News);
185
186 1
            $app->addSuccess('admin.news.up.complete', 'admin');
187
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
188
189
            log_error('新着情報表示順更新エラー', [$News->getId(), $e]);
190
191
            $app->addError('admin.news.up.error', 'admin');
192
        }
193
194 1
        return $app->redirect($app->url('admin_content_news'));
195
    }
196
197
    /**
198
     * 指定した新着情報の表示順を1つ下げる。
199
     *
200
     * @Method("PUT")
201
     * @Route("/{_admin}/content/news/{id}/down", requirements={"id" = "\d+"}, name="admin_content_news_down")
202
     *
203
     * @param Application $app
204
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
205
     * @param News $News
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
206
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
207
     */
208 1 View Code Duplication
    public function down(Application $app, Request $request, News $News)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
209
    {
210 1
        $this->isTokenValid($app);
211
212
        try {
213 1
            $this->newsRepository->down($News);
214
215 1
            $app->addSuccess('admin.news.down.complete', 'admin');
216
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
217
218
            log_error('新着情報表示順更新エラー', [$News->getId(), $e]);
219
220
            $app->addError('admin.news.down.error', 'admin');
221
        }
222
223 1
        return $app->redirect($app->url('admin_content_news'));
224
    }
225
226
    /**
227
     * 指定した新着情報を削除する。
228
     *
229
     * @Method("DELETE")
230
     * @Route("/{_admin}/content/news/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_news_delete")
231
     *
232
     * @param Application $app
233
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
234
     * @param News $News
0 ignored issues
show
introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
235
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
236
     */
237 1 View Code Duplication
    public function delete(Application $app, Request $request, News $News)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
238
    {
239 1
        $this->isTokenValid($app);
240
241 1
        log_info('新着情報削除開始', [$News->getId()]);
242
243
        try {
244 1
            $this->newsRepository->delete($News);
245
246 1
            $event = new EventArgs(['News' => $News], $request);
247 1
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_DELETE_COMPLETE, $event);
248
249 1
            $app->addSuccess('admin.news.delete.complete', 'admin');
250
251 1
            log_info('新着情報削除完了', [$News->getId()]);
252
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
253
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
254
255
            $message = $app->trans('admin.delete.failed.foreign_key', ['%name%' => '新着情報']);
256
            $app->addError($message, 'admin');
257
258
            log_error('新着情報削除エラー', [$News->getId(), $e]);
259
        }
260
261 1
        return $app->redirect($app->url('admin_content_news'));
262
    }
263
}
264