Failed Conditions
Push — master ( 36ccc8...f8bd2f )
by Kentaro
34:47
created

NewsController::delete()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
ccs 0
cts 0
cp 0
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 18
nc 3
nop 3
crap 12
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 1
     * @return \Symfony\Component\HttpFoundation\Response
44
     */
45 View Code Duplication
    public function index(Application $app, Request $request)
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...
46
    {
47
        $NewsList = $app['eccube.repository.news']->findBy(array(), array('rank' => 'DESC'));
48
49 1
        $builder = $app->form();
50 1
51
        $event = new EventArgs(
52
            array(
53 1
                'builder' => $builder,
54
                'NewsList' => $NewsList,
55
            ),
56
            $request
57
        );
58
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_INDEX_INITIALIZE, $event);
59
60
        $form = $builder->getForm();
61
62
        return $app->render('Content/news.twig', array(
63
            'form' => $form->createView(),
64 2
            'NewsList' => $NewsList,
65
        ));
66 2
    }
67
68 1
    /**
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 1
     * @throws NotFoundHttpException
75
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
76
     */
77
    public function edit(Application $app, Request $request, $id = null)
78
    {
79
        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
            $News = $app['eccube.repository.news']->find($id);
81
            if (!$News) {
82
                throw new NotFoundHttpException();
83
            }
84
            $News->setLinkMethod((bool) $News->getLinkMethod());
85
        } else {
86
            $News = new \Eccube\Entity\News();
87
        }
88
89
        $builder = $app['form.factory']
90
            ->createBuilder('admin_news', $News);
91
92
        $event = new EventArgs(
93
            array(
94
                'builder' => $builder,
95
                'News' => $News,
96
            ),
97
            $request
98
        );
99 2
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_INITIALIZE, $event);
100 2
101
        $form = $builder->getForm();
102
103 2
        if ('POST' === $request->getMethod()) {
104
            $form->handleRequest($request);
105 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
                $data = $form->getData();
107
                if (empty($data['url'])) {
108
                    $News->setLinkMethod(Constant::DISABLED);
109
                }
110
111
                $status = $app['eccube.repository.news']->save($News);
112
113
                if ($status) {
114 1
115
                    $event = new EventArgs(
116
                        array(
117
                            'form' => $form,
118
                            'News' => $News,
119 1
                        ),
120
                        $request
121
                    );
122
                    $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_COMPLETE, $event);
123
124
                    $app->addSuccess('admin.news.save.complete', 'admin');
125 1
126
                    return $app->redirect($app->url('admin_content_news'));
127
                }
128
                $app->addError('admin.news.save.error', 'admin');
129 1
            }
130
        }
131
132 1
        return $app->render('Content/news_edit.twig', array(
133
            'form' => $form->createView(),
134
            '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 1
     * @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 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 1
    {
149
        $this->isTokenValid($app);
150
151
        $TargetNews = $app['eccube.repository.news']->find($id);
152
        if (!$TargetNews) {
153
            throw new NotFoundHttpException();
154 1
        }
155
156
        $status = $app['eccube.repository.news']->up($TargetNews);
157
158 1
        if ($status) {
159
            $app->addSuccess('admin.news.up.complete', 'admin');
160
        } else {
161 1
            $app->addError('admin.news.up.error', 'admin');
162
        }
163
164
        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 1
     * @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 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 1
    {
178
        $this->isTokenValid($app);
179
180
        $TargetNews = $app['eccube.repository.news']->find($id);
181
        if (!$TargetNews) {
182
            throw new NotFoundHttpException();
183 1
        }
184
185
        $status = $app['eccube.repository.news']->down($TargetNews);
186
187 1
        if ($status) {
188
            $app->addSuccess('admin.news.down.complete', 'admin');
189
        } else {
190 1
            $app->addError('admin.news.down.error', 'admin');
191
        }
192
193
        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
    public function delete(Application $app, Request $request, $id)
206
    {
207
        $this->isTokenValid($app);
208
209
        $TargetNews = $app['eccube.repository.news']->find($id);
210
        if (!$TargetNews) {
211
            throw new NotFoundHttpException();
212
        }
213
214
        $status = $app['eccube.repository.news']->delete($TargetNews);
215
216
        $event = new EventArgs(
217
            array(
218
                'TargetNews' => $TargetNews,
219
                'status' => $status,
220
            ),
221
            $request
222
        );
223
        $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_DELETE_COMPLETE, $event);
224
        $status = $event->getArgument('status');
225
226
        if ($status) {
227
            $app->addSuccess('admin.news.delete.complete', 'admin');
228
        } else {
229
            $app->addSuccess('admin.news.delete.error', 'admin');
230
        }
231
232
        return $app->redirect($app->url('admin_content_news'));
233
    }
234
}