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