Failed Conditions
Push — sf/last-boss ( 98c677...e157b8 )
by Kiyotaka
05:51
created

NewsController::up()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 3
nop 1
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Content;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Entity\News;
18
use Eccube\Event\EccubeEvents;
19
use Eccube\Event\EventArgs;
20
use Eccube\Form\Type\Admin\NewsType;
21
use Eccube\Repository\NewsRepository;
22
use Knp\Component\Pager\Paginator;
23
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
use Symfony\Component\Routing\Annotation\Route;
27
28
class NewsController extends AbstractController
29
{
30
    /**
31
     * @var NewsRepository
32
     */
33
    protected $newsRepository;
34
35
    /**
36
     * NewsController constructor.
37
     *
38
     * @param NewsRepository $newsRepository
39
     */
40
    public function __construct(NewsRepository $newsRepository)
41
    {
42
        $this->newsRepository = $newsRepository;
43
    }
44
45
    /**
46
     * 新着情報一覧を表示する。
47
     *
48
     * @Route("/%eccube_admin_route%/content/news", name="admin_content_news")
49
     * @Route("/%eccube_admin_route%/content/news/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_content_news_page")
50
     * @Template("@admin/Content/news.twig")
51
     *
52
     * @param Request $request
53
     * @param int $page_no
54
     * @param Paginator $paginator
55
     *
56
     * @return array
57
     */
58 View Code Duplication
    public function index(Request $request, $page_no = 1, Paginator $paginator)
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...
59
    {
60
        $qb = $this->newsRepository->getQueryBuilderAll();
61
62
        $event = new EventArgs(
63
            [
64
                'qb' => $qb,
65
            ],
66
            $request
67
        );
68
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_INDEX_INITIALIZE, $event);
69
70
        $pagination = $paginator->paginate(
71
            $qb,
72
            $page_no,
73
            $this->eccubeConfig->get('eccube_default_page_count')
74
        );
75
76
        return [
77
            'pagination' => $pagination,
78
        ];
79
    }
80
81
    /**
82
     * 新着情報を登録・編集する。
83
     *
84
     * @Route("/%eccube_admin_route%/content/news/new", name="admin_content_news_new")
85
     * @Route("/%eccube_admin_route%/content/news/{id}/edit", requirements={"id" = "\d+"}, name="admin_content_news_edit")
86
     * @Template("@admin/Content/news_edit.twig")
87
     *
88
     * @param Request $request
89
     * @param null $id
90
     *
91
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
92
     */
93
    public function edit(Request $request, $id = null)
94
    {
95
        if ($id) {
96
            $News = $this->newsRepository->find($id);
97
            if (!$News) {
98
                throw new NotFoundHttpException();
99
            }
100
        } else {
101
            $News = new \Eccube\Entity\News();
102
            $News->setPublishDate(new \DateTime());
103
        }
104
105
        $builder = $this->formFactory
106
            ->createBuilder(NewsType::class, $News);
107
108
        $event = new EventArgs(
109
            [
110
                'builder' => $builder,
111
                'News' => $News,
112
            ],
113
            $request
114
        );
115
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_INITIALIZE, $event);
116
117
        $form = $builder->getForm();
118
        $form->handleRequest($request);
119
120
        if ($form->isSubmitted() && $form->isValid()) {
121
            if (!$News->getUrl()) {
122
                $News->setLinkMethod(false);
123
            }
124
            $this->newsRepository->save($News);
125
126
            $event = new EventArgs(
127
                [
128
                    'form' => $form,
129
                    'News' => $News,
130
                ],
131
                $request
132
            );
133
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_EDIT_COMPLETE, $event);
134
135
            $this->addSuccess('admin.common.save_complete', 'admin');
136
137
            return $this->redirectToRoute('admin_content_news_edit', ['id' => $News->getId()]);
138
        }
139
140
        return [
141
            'form' => $form->createView(),
142
            'News' => $News,
143
        ];
144
    }
145
146
    /**
147
     * 指定した新着情報を削除する。
148
     *
149
     * @Route("/%eccube_admin_route%/content/news/{id}/delete", requirements={"id" = "\d+"}, name="admin_content_news_delete", methods={"DELETE"})
150
     *
151
     * @param Request $request
152
     * @param News $News
153
     *
154
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
155
     */
156 View Code Duplication
    public function delete(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...
157
    {
158
        $this->isTokenValid();
159
160
        log_info('新着情報削除開始', [$News->getId()]);
161
162
        try {
163
            $this->newsRepository->delete($News);
164
165
            $event = new EventArgs(['News' => $News], $request);
166
            $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CONTENT_NEWS_DELETE_COMPLETE, $event);
167
168
            $this->addSuccess('admin.common.delete_complete', 'admin');
169
170
            log_info('新着情報削除完了', [$News->getId()]);
171
        } catch (\Exception $e) {
172
            $message = trans('admin.common.delete_error_foreign_key', ['%name%' => $News->getTitle()]);
173
            $this->addError($message, 'admin');
174
175
            log_error('新着情報削除エラー', [$News->getId(), $e]);
176
        }
177
178
        return $this->redirectToRoute('admin_content_news');
179
    }
180
}
181