NewsController   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 198
Duplicated Lines 31.82 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
dl 63
loc 198
ccs 69
cts 78
cp 0.8846
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
B edit() 25 60 7
A up() 19 19 3
A down() 19 19 3
B delete() 0 29 3
A index() 0 22 1

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\Application;
27
use Eccube\Common\Constant;
28
use Eccube\Controller\AbstractController;
29
use Eccube\Event\EccubeEvents;
30
use Eccube\Event\EventArgs;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
34
/**
35
 * 新着情報のコントローラクラス
36
 */
37
class NewsController extends AbstractController
38
{
39
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$request" missing
Loading history...
40
     * 新着情報一覧を表示する。
41
     *
42
     * @param Application $app
43
     * @return \Symfony\Component\HttpFoundation\Response
44
     */
45 2
    public function index(Application $app, Request $request)
46
    {
47 2
        $NewsList = $app['eccube.repository.news']->findBy(array(), array('rank' => 'DESC'));
48
49 2
        $builder = $app->form();
50
51 2
        $event = new EventArgs(
52
            array(
53 2
                'builder' => $builder,
54 2
                'NewsList' => $NewsList,
55
            ),
56
            $request
57
        );
58 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_INDEX_INITIALIZE, $event);
59
60 2
        $form = $builder->getForm();
61
62 2
        return $app->render('Content/news.twig', array(
63 2
            'form' => $form->createView(),
64 2
            'NewsList' => $NewsList,
65
        ));
66
    }
67
68
    /**
69
     * 新着情報を登録・編集する。
70
     *
71
     * @param Application $app
72
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
73
     * @param integer $id
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
74
     * @throws NotFoundHttpException
75
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
76
     */
77 5
    public function edit(Application $app, Request $request, $id = null)
78
    {
79 5
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
80 2
            $News = $app['eccube.repository.news']->find($id);
81 2
            if (!$News) {
82
                throw new NotFoundHttpException();
83
            }
84 2
            $News->setLinkMethod((bool) $News->getLinkMethod());
85
        } else {
86 3
            $News = new \Eccube\Entity\News();
87
        }
88
89 5
        $builder = $app['form.factory']
90 5
            ->createBuilder('admin_news', $News);
91
92 5
        $event = new EventArgs(
93
            array(
94 5
                'builder' => $builder,
95 5
                'News' => $News,
96
            ),
97
            $request
98
        );
99 5
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_INITIALIZE, $event);
100
101 5
        $form = $builder->getForm();
102
103 5
        if ('POST' === $request->getMethod()) {
104 1
            $form->handleRequest($request);
105 1 View Code Duplication
            if ($form->isValid()) {
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...
106 1
                $data = $form->getData();
107 1
                if (empty($data['url'])) {
108
                    $News->setLinkMethod(Constant::DISABLED);
109
                }
110
111 1
                $status = $app['eccube.repository.news']->save($News);
112
113 1
                if ($status) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
114
115 1
                    $event = new EventArgs(
116
                        array(
117 1
                            'form' => $form,
118 1
                            'News' => $News,
119
                        ),
120
                        $request
121
                    );
122 1
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_COMPLETE, $event);
123
124 1
                    $app->addSuccess('admin.news.save.complete', 'admin');
125
126 1
                    return $app->redirect($app->url('admin_content_news'));
127
                }
128
                $app->addError('admin.news.save.error', 'admin');
129
            }
130
        }
131
132 4
        return $app->render('Content/news_edit.twig', array(
133 4
            'form' => $form->createView(),
134 4
            'News' => $News,
135
        ));
136
    }
137
138
    /**
139
     * 指定した新着情報の表示順を1つ上げる。
140
     *
141
     * @param Application $app
142
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
143
     * @param integer $id
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
144
     * @throws NotFoundHttpException
145
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
146
     */
147 2 View Code Duplication
    public function up(Application $app, Request $request, $id)
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...
148
    {
149 2
        $this->isTokenValid($app);
150
151 2
        $TargetNews = $app['eccube.repository.news']->find($id);
152 2
        if (!$TargetNews) {
153
            throw new NotFoundHttpException();
154
        }
155
156 2
        $status = $app['eccube.repository.news']->up($TargetNews);
157
158 2
        if ($status) {
159 2
            $app->addSuccess('admin.news.up.complete', 'admin');
160
        } else {
161
            $app->addError('admin.news.up.error', 'admin');
162
        }
163
164 2
        return $app->redirect($app->url('admin_content_news'));
165
    }
166
167
    /**
168
     * 指定した新着情報の表示順を1つ下げる。
169
     *
170
     * @param Application $app
171
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
172
     * @param integer $id
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
173
     * @throws NotFoundHttpException
174
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
175
     */
176 2 View Code Duplication
    public function down(Application $app, Request $request, $id)
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...
177
    {
178 2
        $this->isTokenValid($app);
179
180 2
        $TargetNews = $app['eccube.repository.news']->find($id);
181 2
        if (!$TargetNews) {
182
            throw new NotFoundHttpException();
183
        }
184
185 2
        $status = $app['eccube.repository.news']->down($TargetNews);
186
187 2
        if ($status) {
188 2
            $app->addSuccess('admin.news.down.complete', 'admin');
189
        } else {
190
            $app->addError('admin.news.down.error', 'admin');
191
        }
192
193 2
        return $app->redirect($app->url('admin_content_news'));
194
    }
195
196
    /**
197
     * 指定した新着情報を削除する。
198
     *
199
     * @param Application $app
200
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
201
     * @param integer $id
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
202
     * @throws NotFoundHttpException
203
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
204
     */
205 2
    public function delete(Application $app, Request $request, $id)
206
    {
207 2
        $this->isTokenValid($app);
208
209 2
        $TargetNews = $app['eccube.repository.news']->find($id);
210 2
        if (!$TargetNews) {
211
            throw new NotFoundHttpException();
212
        }
213
214 2
        $status = $app['eccube.repository.news']->delete($TargetNews);
215
216 2
        $event = new EventArgs(
217
            array(
218 2
                'TargetNews' => $TargetNews,
219 2
                'status' => $status,
220
            ),
221
            $request
222
        );
223 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_DELETE_COMPLETE, $event);
224 2
        $status = $event->getArgument('status');
225
226 2
        if ($status) {
227 2
            $app->addSuccess('admin.news.delete.complete', 'admin');
228
        } else {
229
            $app->addSuccess('admin.news.delete.error', 'admin');
230
        }
231
232 2
        return $app->redirect($app->url('admin_content_news'));
233
    }
234
}