Completed
Pull Request — 4.0 (#4000)
by
unknown
06:58
created

OwnerStoreController::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 8
dl 0
loc 25
ccs 0
cts 12
cp 0
crap 12
rs 9.52
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Store;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Entity\BaseInfo;
18
use Eccube\Entity\Master\PageMax;
19
use Eccube\Entity\Plugin;
20
use Eccube\Exception\PluginApiException;
21
use Eccube\Form\Type\Admin\SearchPluginApiType;
22
use Eccube\Repository\BaseInfoRepository;
23
use Eccube\Repository\PluginRepository;
24
use Eccube\Service\Composer\ComposerProcessService;
25
use Eccube\Service\Composer\ComposerServiceInterface;
26
use Eccube\Service\PluginApiService;
27
use Eccube\Service\PluginService;
28
use Eccube\Service\SystemService;
29
use Eccube\Util\CacheUtil;
30
use Eccube\Util\FormUtil;
31
use Knp\Component\Pager\Paginator;
32
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpKernel\KernelEvents;
35
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
36
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
37
use Symfony\Component\Routing\Annotation\Route;
38
39
/**
40
 * @Route("/%eccube_admin_route%/store/plugin/api")
41
 */
42
class OwnerStoreController extends AbstractController
43
{
44
    /**
45
     * @var PluginRepository
46
     */
47
    protected $pluginRepository;
48
49
    /**
50
     * @var PluginService
51
     */
52
    protected $pluginService;
53
54
    /**
55
     * @var ComposerServiceInterface
56
     */
57
    protected $composerService;
58
59
    /**
60
     * @var SystemService
61
     */
62
    protected $systemService;
63
64
    /**
65
     * @var PluginApiService
66
     */
67
    protected $pluginApiService;
68
69
    private static $vendorName = 'ec-cube';
70
71
    /** @var BaseInfo */
72
    private $BaseInfo;
73
74
    /** @var CacheUtil */
75
    private $cacheUtil;
76
77
    /**
78
     * OwnerStoreController constructor.
79
     *
80
     * @param PluginRepository $pluginRepository
81
     * @param PluginService $pluginService
82
     * @param ComposerProcessService $composerProcessService
83
     * @param ComposerServiceInterface $composerService
84
     * @param SystemService $systemService
85
     * @param PluginApiService $pluginApiService
86
     * @param BaseInfoRepository $baseInfoRepository
87
     * @param CacheUtil $cacheUtil
88
     *
89
     * @throws \Doctrine\ORM\NoResultException
90
     * @throws \Doctrine\ORM\NonUniqueResultException
91
     */
92
    public function __construct(
93
        PluginRepository $pluginRepository,
94
        PluginService $pluginService,
95
        ComposerProcessService $composerProcessService,
96
        ComposerServiceInterface $composerService,
97
        SystemService $systemService,
98
        PluginApiService $pluginApiService,
99
        BaseInfoRepository $baseInfoRepository,
100
        CacheUtil $cacheUtil
101
    ) {
102
        $this->pluginRepository = $pluginRepository;
103
        $this->pluginService = $pluginService;
104
        $this->systemService = $systemService;
105
        $this->pluginApiService = $pluginApiService;
106
        $this->BaseInfo = $baseInfoRepository->get();
107
        $this->cacheUtil = $cacheUtil;
108
109
        // TODO: Check the flow of the composer service below
110
        $memoryLimit = $this->systemService->getMemoryLimit();
111
        if ($memoryLimit == -1 or $memoryLimit >= $this->eccubeConfig['eccube_composer_memory_limit']) {
112
            $this->composerService = $composerService;
113
        } else {
114
            $this->composerService = $composerProcessService;
115
        }
116
    }
117
118
    /**
119
     * Owner's Store Plugin Installation Screen - Search function
120
     *
121
     * @Route("/search", name="admin_store_plugin_owners_search")
122
     * @Route("/search/page/{page_no}", name="admin_store_plugin_owners_search_page", requirements={"page_no" = "\d+"})
123
     * @Template("@admin/Store/plugin_search.twig")
124
     *
125
     * @param Request     $request
126
     * @param int $page_no
127
     * @param Paginator $paginator
128
     *
129
     * @return array
130
     */
131
    public function search(Request $request, $page_no = null, Paginator $paginator)
132
    {
133
        if (empty($this->BaseInfo->getAuthenticationKey())) {
134
            $this->addWarning('認証キーを設定してください。', 'admin');
135
136
            return $this->redirectToRoute('admin_store_authentication_setting');
137
        }
138
139
        // Acquire downloadable plug-in information from owners store
140
        $category = [];
141
142
        $json = $this->pluginApiService->getCategory();
143
        if (!empty($json)) {
144
            $data = json_decode($json, true);
145
            $category = array_column($data, 'name', 'id');
146
        }
147
148
        // build form with master data
149
        $builder = $this->formFactory
150
            ->createBuilder(SearchPluginApiType::class, null, ['category' => $category]);
151
        $searchForm = $builder->getForm();
152
153
        $searchForm->handleRequest($request);
154
        $searchData = $searchForm->getData();
155
        if ($searchForm->isSubmitted()) {
156
            if ($searchForm->isValid()) {
157
                $page_no = 1;
158
                $searchData = $searchForm->getData();
159
                $this->session->set('eccube.admin.plugin_api.search', FormUtil::getViewData($searchForm));
160
                $this->session->set('eccube.admin.plugin_api.search.page_no', $page_no);
161
            }
162
        } else {
163
            // quick search
164
            if (is_numeric($categoryId = $request->get('category_id')) && array_key_exists($categoryId, $category)) {
165
                $searchForm['category_id']->setData($categoryId);
166
            }
167
            // reset page count
168
            $this->session->set('eccube.admin.plugin_api.search.page_count', $this->eccubeConfig->get('eccube_default_page_count'));
169 View Code Duplication
            if (null !== $page_no || $request->get('resume')) {
170
                if ($page_no) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $page_no 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...
171
                    $this->session->set('eccube.admin.plugin_api.search.page_no', (int) $page_no);
172
                } else {
173
                    $page_no = $this->session->get('eccube.admin.plugin_api.search.page_no', 1);
174
                }
175
                $viewData = $this->session->get('eccube.admin.plugin_api.search', []);
176
                $searchData = FormUtil::submitAndGetData($searchForm, $viewData);
177
            } else {
178
                $page_no = 1;
179
                // submit default value
180
                $viewData = FormUtil::getViewData($searchForm);
181
                $searchData = FormUtil::submitAndGetData($searchForm, $viewData);
182
                $this->session->set('eccube.admin.plugin_api.search', $searchData);
183
                $this->session->set('eccube.admin.plugin_api.search.page_no', $page_no);
184
            }
185
        }
186
187
        // set page count
188
        $pageCount = $this->session->get('eccube.admin.plugin_api.search.page_count', $this->eccubeConfig->get('eccube_default_page_count'));
189
        if (($PageMax = $searchForm['page_count']->getData()) instanceof PageMax) {
190
            $pageCount = $PageMax->getId();
191
            $this->session->set('eccube.admin.plugin_api.search.page_count', $pageCount);
192
        }
193
194
        // Owner's store communication
195
        $searchData['page_no'] = $page_no;
196
        $searchData['page_count'] = $pageCount;
197
198
        $total = 0;
199
        $items = [];
200
201
        try {
202
            $data = $this->pluginApiService->getPlugins($searchData);
203
            $total = $data['total'];
204
            $items = $data['plugins'];
205
        } catch (PluginApiException $e) {
206
            $this->addError($e->getMessage(), 'admin');
207
        }
208
209
        // The usage is set because `$items` are already paged.
210
        // virtual paging
211
        $pagination = $paginator->paginate($items, 1, $pageCount);
212
        $pagination->setTotalItemCount($total);
213
        $pagination->setCurrentPageNumber($page_no);
214
        $pagination->setItemNumberPerPage($pageCount);
215
216
        return [
217
            'pagination' => $pagination,
218
            'total' => $total,
219
            'searchForm' => $searchForm->createView(),
220
            'page_no' => $page_no,
221
            'Categories' => $category,
222
        ];
223
    }
224
225
    /**
226
     * Do confirm page
227
     *
228
     * @Route("/install/{id}/confirm", requirements={"id" = "\d+"}, name="admin_store_plugin_install_confirm")
229
     * @Template("@admin/Store/plugin_confirm.twig")
230
     *
231
     * @param Request $request
232
     *
233
     * @return array
234
     *
235
     * @throws \Eccube\Exception\PluginException
236
     */
237
    public function doConfirm(Request $request, $id)
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...
238
    {
239
        try {
240
            $item = $this->pluginApiService->getPlugin($id);
241
            // Todo: need define item's dependency mechanism
242
            $requires = $this->pluginService->getPluginRequired($item);
243
244
            return [
245
                'item' => $item,
246
                'requires' => $requires,
247
                'is_update' => false,
248
            ];
249
        } catch (PluginApiException $e) {
250
            $this->addError($e->getMessage(), 'admin');
251
252
            return $this->redirectToRoute('admin_store_authentication_setting');
253
        }
254
    }
255
256
    /**
257
     * Api Install plugin by composer connect with package repo
258
     *
259
     * @Route("/install", name="admin_store_plugin_api_install", methods={"POST"})
260
     *
261
     * @param Request $request
262
     *
263
     * @return \Symfony\Component\HttpFoundation\JsonResponse
264
     */
265
    public function apiInstall(Request $request, EventDispatcherInterface $dispatcher)
266
    {
267
        // .maintenanceファイルを設置
268
        $this->systemService->switchMaintenance(true);
269
270
        // TERMINATE時のイベントを設定
271
        $dispatcher->addListener(KernelEvents::TERMINATE, function () {
272
            // .maintenanceファイルを削除
273
            $this->systemService->switchMaintenance();
274
        });
275
        $this->isTokenValid();
276
277
        $this->cacheUtil->clearCache();
278
279
        $pluginCode = $request->get('pluginCode');
280
281
        $log = null;
282
        try {
283
            $log = $this->composerService->execRequire('ec-cube/'.$pluginCode);
284
285
            return $this->json(['success' => true, 'log' => $log]);
286
        } catch (\Exception $e) {
287
            $log = $e->getMessage();
288
            log_error($e);
289
        }
290
291
        return $this->json(['success' => false, 'log' => $log], 500);
292
    }
293
294
    /**
295
     * New ways to remove plugin: using composer command
296
     *
297
     * @Route("/delete/{id}/uninstall", requirements={"id" = "\d+"}, name="admin_store_plugin_api_uninstall", methods={"DELETE"})
298
     *
299
     * @param Plugin $Plugin
300
     *
301
     * @return \Symfony\Component\HttpFoundation\JsonResponse
302
     */
303
    public function apiUninstall(Plugin $Plugin, EventDispatcherInterface $dispatcher)
304
    {
305
        // .maintenanceファイルを設置
306
        $this->systemService->switchMaintenance(true);
307
308
        // TERMINATE時のイベントを設定
309
        $dispatcher->addListener(KernelEvents::TERMINATE, function () {
310
            // .maintenanceファイルを削除
311
            $this->systemService->switchMaintenance();
312
        });
313
314
        $this->isTokenValid();
315
316
        $this->cacheUtil->clearCache();
317
318
        if ($Plugin->isEnabled()) {
319
            return $this->json(['success' => false, 'message' => trans('admin.plugin.uninstall.error.not_disable')], 400);
320
        }
321
322
        $pluginCode = $Plugin->getCode();
323
        $otherDepend = $this->pluginService->findDependentPlugin($pluginCode);
324
325
        if (!empty($otherDepend)) {
326
            $DependPlugin = $this->pluginRepository->findOneBy(['code' => $otherDepend[0]]);
327
            $dependName = $otherDepend[0];
328
            if ($DependPlugin) {
329
                $dependName = $DependPlugin->getName();
330
            }
331
            $message = trans('admin.plugin.uninstall.depend', ['%name%' => $Plugin->getName(), '%depend_name%' => $dependName]);
332
333
            return $this->json(['success' => false, 'message' => $message], 400);
334
        }
335
336
        $pluginCode = $Plugin->getCode();
337
        $packageName = self::$vendorName.'/'.$pluginCode;
338
339
        try {
340
            $log = $this->composerService->execRemove($packageName);
341
342
            return $this->json(['success' => false, 'log' => $log]);
343
        } catch (\Exception $e) {
344
            log_error($e);
345
346
            return $this->json(['success' => false, 'log' => $e->getMessage()], 500);
347
        }
348
    }
349
350
    /**
351
     * オーナーズブラグインインストール、アップデート
352
     *
353
     * @Route("/upgrade", name="admin_store_plugin_api_upgrade", methods={"POST"})
354
     *
355
     * @param Request $request
356
     *
357
     * @return \Symfony\Component\HttpFoundation\JsonResponse
358
     */
359
    public function apiUpgrade(Request $request, EventDispatcherInterface $dispatcher)
360
    {
361
        // .maintenanceファイルを設置
362
        $this->systemService->switchMaintenance(true);
363
364
        // TERMINATE時のイベントを設定
365
        $dispatcher->addListener(KernelEvents::TERMINATE, function () {
366
            // .maintenanceファイルを削除
367
            $this->systemService->switchMaintenance();
368
        });
369
        
370
        $this->isTokenValid();
371
372
        $this->cacheUtil->clearCache();
373
374
        $pluginCode = $request->get('pluginCode');
375
        $version = $request->get('version');
376
377
        $log = null;
378
        try {
379
            $log = $this->composerService->execRequire('ec-cube/'.$pluginCode.':'.$version);
380
            return $this->json(['success' => true, 'log' => $log]);
381
        } catch (\Exception $e) {
382
            $log = $e->getMessage();
383
            log_error($e);
384
        }
385
386
        return $this->json(['success' => false, 'log' => $log], 500);
387
    }
388
389
    /**
390
     * オーナーズブラグインインストール、スキーマ更新
391
     *
392
     * @Route("/schema_update", name="admin_store_plugin_api_schema_update", methods={"POST"})
393
     *
394
     * @param Request $request
395
     *
396
     * @return \Symfony\Component\HttpFoundation\JsonResponse
397
     */
398
    public function apiSchemaUpdate(Request $request)
399
    {
400
        $this->isTokenValid();
401
402
        $this->cacheUtil->clearCache();
403
404
        $pluginCode = $request->get('pluginCode');
405
406
        try {
407
            $Plugin = $this->pluginRepository->findByCode($pluginCode);
408
409
            if (!$Plugin) {
410
                throw new NotFoundHttpException();
411
            }
412
413
            $config = $this->pluginService->readConfig($this->pluginService->calcPluginDir($Plugin->getCode()));
414
415
            ob_start();
416
            $this->pluginService->generateProxyAndUpdateSchema($Plugin, $config);
417
418
            // 初期化されていなければインストール処理を実行する
419
            if (!$Plugin->isInitialized()) {
420
                $this->pluginService->callPluginManagerMethod($config, 'install');
421
                $Plugin->setInitialized(true);
422
                $this->entityManager->persist($Plugin);
423
                $this->entityManager->flush();
424
            }
425
426
            $log = ob_get_clean();
427
            while (ob_get_level() > 0) {
428
                ob_end_flush();
429
            }
430
431
            return $this->json(['success' => true, 'log' => $log]);
432
        } catch (\Exception $e) {
433
            $log = $e->getMessage();
434
            log_error($e);
435
436
            return $this->json(['success' => false, 'log' => $log], 500);
437
        }
438
    }
439
440
    /**
441
     * オーナーズブラグインインストール、更新処理
442
     *
443
     * @Route("/update", name="admin_store_plugin_api_update", methods={"POST"})
444
     *
445
     * @param Request $request
446
     *
447
     * @return \Symfony\Component\HttpFoundation\JsonResponse
448
     */
449
    public function apiUpdate(Request $request)
450
    {
451
        $this->isTokenValid();
452
453
        $this->cacheUtil->clearCache();
454
455
        $pluginCode = $request->get('pluginCode');
456
457
        $log = null;
458
        try {
459
            $Plugin = $this->pluginRepository->findByCode($pluginCode);
460
            if (!$Plugin) {
461
                throw new NotFoundHttpException();
462
            }
463
464
            $config = $this->pluginService->readConfig($this->pluginService->calcPluginDir($Plugin->getCode()));
465
            ob_start();
466
            $this->pluginService->updatePlugin($Plugin, $config);
467
            $log = ob_get_clean();
468
            while (ob_get_level() > 0) {
469
                ob_end_flush();
470
            }
471
472
            return $this->json(['success' => true, 'log' => $log]);
473
        } catch (\Exception $e) {
474
            $log = $e->getMessage();
475
            log_error($e);
476
        }
477
478
        return $this->json(['success' => false, 'log' => $log], 500);
479
    }
480
481
    /**
482
     * Do confirm update page
483
     *
484
     * @Route("/upgrade/{id}/confirm", requirements={"id" = "\d+"}, name="admin_store_plugin_update_confirm")
485
     * @Template("@admin/Store/plugin_confirm.twig")
486
     *
487
     * @param Plugin $Plugin
488
     *
489
     * @return array
490
     */
491
    public function doUpdateConfirm(Plugin $Plugin)
492
    {
493
        try {
494
            $item = $this->pluginApiService->getPlugin($Plugin->getSource());
495
496
            return [
497
                'item' => $item,
498
                'requires' => [],
499
                'is_update' => true,
500
                'Plugin' => $Plugin,
501
            ];
502
        } catch (PluginApiException $e) {
503
            $this->addError($e->getMessage(), 'admin');
504
505
            return $this->redirectToRoute('admin_store_authentication_setting');
506
        }
507
    }
508
}
509