Failed Conditions
Pull Request — 3.0.9-dev (#1468)
by chihiro
587:07 queued 580:11
created

PluginController   C

Complexity

Total Complexity 72

Size/Duplication

Total Lines 662
Duplicated Lines 7.85 %

Coupling/Cohesion

Components 2
Dependencies 11
Metric Value
wmc 72
lcom 2
cbo 11
dl 52
loc 662
rs 5.2316

14 Methods

Rating   Name   Duplication   Size   Complexity  
D index() 0 92 13
B update() 0 59 7
A enable() 19 19 3
A disable() 19 19 3
A handler() 0 15 2
A handler_up() 7 7 1
A handler_down() 7 7 1
B install() 0 49 6
D ownersInstall() 0 109 16
C upgrade() 0 92 10
B authenticationSetting() 0 40 3
A getRequestApi() 0 21 1
B getResponseErrorMessage() 0 22 4
A uninstall() 0 17 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PluginController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PluginController, and based on these observations, apply Extract Interface, too.

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
25
namespace Eccube\Controller\Admin\Store;
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Controller\AbstractController;
30
use Eccube\Exception\PluginException;
31
use Eccube\Util\Cache;
32
use Eccube\Util\Str;
33
use Symfony\Component\Filesystem\Filesystem;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36
use Symfony\Component\Validator\Constraints as Assert;
37
38
class PluginController extends AbstractController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
39
{
40
41
    /**
42
     * インストール済プラグイン画面
43
     *
44
     * @param Application $app
45
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
46
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
47
    public function index(Application $app, Request $request)
48
    {
49
50
        $pluginForms = array();
51
        $configPages = array();
52
53
        $Plugins = $app['eccube.repository.plugin']->findBy(array(), array('name' => 'ASC'));
54
55
        $officialPlugins = array();
56
        $unofficialPlugins = array();
57
58
        foreach ($Plugins as $Plugin) {
59
60
            $form = $app['form.factory']
61
                ->createNamedBuilder('form' . $Plugin->getId(), 'plugin_management', null, array(
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
62
                    'plugin_id' => $Plugin->getId(),
63
                ))
64
                ->getForm();
65
66
            $pluginForms[$Plugin->getId()] = $form->createView();
67
68
            try {
69
                // プラグイン用設定画面があれば表示(プラグイン用のサービスプロバイダーに定義されているか)
70
                $configPages[$Plugin->getCode()] = $app->url('plugin_' . $Plugin->getCode() . '_config');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
71
            } catch (\Exception $e) {
72
                // プラグインで設定画面のルートが定義されていない場合は無視
73
            }
74
75
            if ($Plugin->getSource() == 0) {
76
                // 商品IDが設定されていない場合、非公式プラグイン
77
                $unofficialPlugins[] = $Plugin;
78
            } else {
79
                $officialPlugins[] = $Plugin;
80
            }
81
82
        }
83
84
        // オーナーズストアからダウンロード可能プラグイン情報を取得
85
        $BaseInfo = $app['eccube.repository.base_info']->get();
86
87
        $authKey = $BaseInfo->getAuthenticationKey();
88
89
        if (!is_null($authKey)) {
90
91
            // オーナーズストア通信
92
            $url = $app['config']['owners_store_url'] . '?method=list';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
93
            list($json, $httpHeader) = $this->getRequestApi($request, $authKey, $url);
0 ignored issues
show
Unused Code introduced by
The assignment to $httpHeader is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
94
95
            if ($json) {
96
97
                // 接続成功時
98
99
                $data = json_decode($json, true);
100
101
                if (isset($data['success'])) {
102
                    $success = $data['success'];
103
                    if ($success == '1') {
104
105
                        // 既にインストールされているかどうか確認
106
                        foreach ($data['item'] as $item) {
107
                            foreach ($officialPlugins as $plugin) {
108
                                if ($plugin->getSource() == $item['product_id']) {
109
                                    // 商品IDが同一の情報を設定
110
                                    $plugin->setNewVersion($item['version']);
111
                                    $plugin->setLastUpdateDate($item['last_update_date']);
112
                                    $plugin->setProductUrl($item['product_url']);
113
114
                                    if ($plugin->getVersion() != $item['version']) {
115
                                        // バージョンが異なる
116
                                        if (in_array(Constant::VERSION, $item['eccube_version'])) {
117
                                            // 対象バージョン
118
                                            $plugin->setUpdateStatus(3);
119
                                            break;
120
                                        }
121
                                    }
122
                                }
123
                            }
124
                        }
125
                    }
126
                }
127
            }
128
        }
129
130
131
        return $app->render('Store/plugin.twig', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
132
            'plugin_forms' => $pluginForms,
133
            'officialPlugins' => $officialPlugins,
134
            'unofficialPlugins' => $unofficialPlugins,
135
            'configPages' => $configPages
136
        ));
137
138
    }
139
140
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
141
     * インストール済プラグインからのアップデート
142
     *
143
     * @param Application $app
144
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
145
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
146
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
147
    public function update(Application $app, Request $request, $id)
148
    {
149
150
        $Plugin = $app['eccube.repository.plugin']->find($id);
151
152
        $form = $app['form.factory']
153
            ->createNamedBuilder('form' . $id, 'plugin_management', null, array(
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
154
                'plugin_id' => null, // placeHolder
155
            ))
156
            ->getForm();
157
158
        $message = '';
159
160
        if ('POST' === $request->getMethod()) {
161
            $form->handleRequest($request);
162
163
            if ($form->isValid()) {
164
165
                $tmpDir = null;
166
                try {
167
168
                    $formFile = $form['plugin_archive']->getData();
169
170
                    $tmpDir = $app['eccube.service.plugin']->createTempDir();
171
                    $tmpFile = sha1(Str::random(32)) . '.' . $formFile->getClientOriginalExtension();
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
172
173
                    $formFile->move($tmpDir, $tmpFile);
174
                    $app['eccube.service.plugin']->update($Plugin, $tmpDir . '/' . $tmpFile);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
175
176
                    $fs = new Filesystem();
177
                    $fs->remove($tmpDir);
178
179
                    $app->addSuccess('admin.plugin.update.complete', 'admin');
180
181
                    Cache::clear($app, false);
0 ignored issues
show
Documentation introduced by
$app is of type object<Eccube\Application>, but the function expects a object<Eccube\Util\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
182
183
                    return $app->redirect($app->url('admin_store_plugin'));
184
185
                } catch (PluginException $e) {
186
                    if (!empty($tmpDir) && file_exists($tmpDir)) {
187
                        $fs = new Filesystem();
188
                        $fs->remove($tmpDir);
189
                    }
190
                    $message = $e->getMessage();
191
                }
192
            } else {
193
                $errors = $form->getErrors(true);
194
                foreach ($errors as $error) {
195
                    $message = $error->getMessage();
196
                }
197
198
            }
199
200
        }
201
202
        $app->addError($message, 'admin');
203
204
        return $app->redirect($app->url('admin_store_plugin'));
205
    }
206
207
208
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
209
     * 対象のプラグインを有効にします。
210
     *
211
     * @param Application $app
212
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
213
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
214 View Code Duplication
    public function enable(Application $app, $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...
215
    {
216
        $this->isTokenValid($app);
217
218
        $Plugin = $app['eccube.repository.plugin']->find($id);
219
220
        if (!$Plugin) {
221
            throw new NotFoundHttpException();
222
        }
223
224
        if ($Plugin->getEnable() == Constant::ENABLED) {
225
            $app->addError('admin.plugin.already.enable', 'admin');
226
        } else {
227
            $app['eccube.service.plugin']->enable($Plugin);
228
            $app->addSuccess('admin.plugin.enable.complete', 'admin');
229
        }
230
231
        return $app->redirect($app->url('admin_store_plugin'));
232
    }
233
234
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
235
     * 対象のプラグインを無効にします。
236
     *
237
     * @param Application $app
238
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
239
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
240 View Code Duplication
    public function disable(Application $app, $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...
241
    {
242
        $this->isTokenValid($app);
243
244
        $Plugin = $app['eccube.repository.plugin']->find($id);
245
246
        if (!$Plugin) {
247
            throw new NotFoundHttpException();
248
        }
249
250
        if ($Plugin->getEnable() == Constant::ENABLED) {
251
            $app['eccube.service.plugin']->disable($Plugin);
252
            $app->addSuccess('admin.plugin.disable.complete', 'admin');
253
        } else {
254
            $app->addError('admin.plugin.already.disable', 'admin');
255
        }
256
257
        return $app->redirect($app->url('admin_store_plugin'));
258
    }
259
260
261
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
262
     * 対象のプラグインを削除します。
263
     *
264
     * @param Application $app
265
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
266
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
267
    public function uninstall(Application $app, $id)
268
    {
269
        $this->isTokenValid($app);
270
271
        $Plugin = $app['eccube.repository.plugin']->find($id);
272
273
        if (!$Plugin) {
274
            $app->deleteMessage();
275
            return $app->redirect($app->url('admin_store_plugin'));
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
276
        }
277
278
        $app['eccube.service.plugin']->uninstall($Plugin);
279
280
        $app->addSuccess('admin.plugin.uninstall.complete', 'admin');
281
282
        return $app->redirect($app->url('admin_store_plugin'));
283
    }
284
285
    public function handler(Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
286
    {
287
        $handlers = $app['eccube.repository.plugin_event_handler']->getHandlers();
288
289
        // 一次元配列からイベント毎の二次元配列に変換する
290
        $HandlersPerEvent = array();
291
        foreach ($handlers as $handler) {
292
            $HandlersPerEvent[$handler->getEvent()][$handler->getHandlerType()][] = $handler;
293
        }
294
295
        return $app->render('Store/plugin_handler.twig', array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
296
            'handlersPerEvent' => $HandlersPerEvent
297
        ));
298
299
    }
300
301 View Code Duplication
    public function handler_up(Application $app, $handlerId)
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...
introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Method name "PluginController::handler_up" is not in camel caps format
Loading history...
302
    {
303
        $repo = $app['eccube.repository.plugin_event_handler'];
304
        $repo->upPriority($repo->find($handlerId));
305
306
        return $app->redirect($app->url('admin_store_plugin_handler'));
307
    }
308
309 View Code Duplication
    public function handler_down(Application $app, $handlerId)
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...
introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Method name "PluginController::handler_down" is not in camel caps format
Loading history...
310
    {
311
        $repo = $app['eccube.repository.plugin_event_handler'];
312
        $repo->upPriority($repo->find($handlerId), false);
313
314
        return $app->redirect($app->url('admin_store_plugin_handler'));
315
    }
316
317
    /**
318
     * プラグインファイルアップロード画面
319
     *
320
     * @param Application $app
321
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
322
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
323
    public function install(Application $app, Request $request)
324
    {
325
        $form = $app['form.factory']
326
            ->createBuilder('plugin_local_install')
327
            ->getForm();
328
329
        $errors = array();
330
331
        if ('POST' === $request->getMethod()) {
332
            $form->handleRequest($request);
333
334
            if ($form->isValid()) {
335
336
                $tmpDir = null;
337
                try {
338
                    $service = $app['eccube.service.plugin'];
339
340
                    $formFile = $form['plugin_archive']->getData();
341
342
                    $tmpDir = $service->createTempDir();
343
                    $tmpFile = sha1(Str::random(32)) . '.' . $formFile->getClientOriginalExtension(); // 拡張子を付けないとpharが動かないので付ける
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
344
345
                    $formFile->move($tmpDir, $tmpFile);
346
347
                    $service->install($tmpDir . '/' . $tmpFile);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
348
349
                    $fs = new Filesystem();
350
                    $fs->remove($tmpDir);
351
352
                    $app->addSuccess('admin.plugin.install.complete', 'admin');
353
354
                    return $app->redirect($app->url('admin_store_plugin'));
355
356
                } catch (PluginException $e) {
357
                    if (!empty($tmpDir) && file_exists($tmpDir)) {
358
                        $fs = new Filesystem();
359
                        $fs->remove($tmpDir);
360
                    }
361
                    $errors[] = $e;
362
                }
363
            }
364
        }
365
366
        return $app->render('Store/plugin_install.twig', array(
367
            'form' => $form->createView(),
368
            'errors' => $errors,
369
        ));
370
371
    }
372
373
    /**
374
     * オーナーズストアプラグインインストール画面
375
     *
376
     * @param Application $app
377
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
378
     * @return \Symfony\Component\HttpFoundation\Response
379
     */
380
    public function ownersInstall(Application $app, Request $request)
381
    {
382
        // オーナーズストアからダウンロード可能プラグイン情報を取得
383
        $BaseInfo = $app['eccube.repository.base_info']->get();
384
385
        $authKey = $BaseInfo->getAuthenticationKey();
386
        $authResult = true;
387
        $success = 0;
388
        $items = array();
389
        $promotionItems = array();
390
        $message = '';
391
        if (!is_null($authKey)) {
392
393
            // オーナーズストア通信
394
            $url = $app['config']['owners_store_url'] . '?method=list';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
395
            list($json, $httpHeader) = $this->getRequestApi($request, $authKey, $url);
396
397
            if ($json === false) {
398
                // 接続失敗時
399
                $success = 0;
400
401
                $message = $this->getResponseErrorMessage($httpHeader);
402
403
            } else {
404
                // 接続成功時
405
406
                $data = json_decode($json, true);
407
408
                if (isset($data['success'])) {
409
                    $success = $data['success'];
410
                    if ($success == '1') {
411
                        $items = array();
412
413
                        // 既にインストールされているかどうか確認
414
                        $Plugins = $app['eccube.repository.plugin']->findAll();
415
                        $status = false;
416
                        // update_status 1 : 未インストール、2 : インストール済、 3 : 更新あり、4 : 有料購入
417
                        foreach ($data['item'] as $item) {
418
                            foreach ($Plugins as $plugin) {
419
                                if ($plugin->getSource() == $item['product_id']) {
420
                                    if ($plugin->getVersion() == $item['version']) {
421
                                        // バージョンが同じ
422
                                        $item['update_status'] = 2;
423
                                    } else {
424
                                        // バージョンが異なる
425
                                        $item['update_status'] = 3;
426
                                    }
427
                                    $items[] = $item;
428
                                    $status = true;
429
                                    break;
430
                                }
431
                            }
432
                            if (!$status) {
433
                                // 未インストール
434
                                $item['update_status'] = 1;
435
                                $items[] = $item;
436
                            }
437
                            $status = false;
438
                        }
439
440
                        // EC-CUBEのバージョンチェック
441
                        // 参照渡しをして値を追加
442
                        foreach ($items as &$item) {
443
                            if (in_array(Constant::VERSION, $item['eccube_version'])) {
444
                                // 対象バージョン
445
                                $item['version_check'] = 1;
446
                            } else {
447
                                // 未対象バージョン
448
                                $item['version_check'] = 0;
449
                            }
450
                            if ($item['price'] != '0' && $item['purchased'] == '0') {
451
                                // 有料商品で未購入
452
                                $item['update_status'] = 4;
453
                            }
454
                        }
455
                        unset($item);
456
457
                        // promotionアイテム
458
                        $i = 0;
459
                        foreach ($items as $item) {
460
                            if ($item['promotion'] == 1) {
461
                                $promotionItems[] = $item;
462
                                unset($items[$i]);
463
                            }
464
                            $i++;
465
                        }
466
467
                    } else {
468
                        $message = $data['error_code'] . ' : ' . $data['error_message'];
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
469
                    }
470
                } else {
471
                    $success = 0;
472
                    $message = "EC-CUBEオーナーズストアにエラーが発生しています。";
473
                }
474
            }
475
476
        } else {
477
            $authResult = false;
478
        }
479
480
        return $app->render('Store/plugin_owners_install.twig', array(
481
            'authResult' => $authResult,
482
            'success' => $success,
483
            'items' => $items,
484
            'promotionItems' => $promotionItems,
485
            'message' => $message,
486
        ));
487
488
    }
489
490
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$action" missing
Loading history...
introduced by
Doc comment for parameter "$id" missing
Loading history...
introduced by
Doc comment for parameter "$version" missing
Loading history...
491
     * オーナーズブラグインインストール、アップデート
492
     *
493
     * @param Application $app
494
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
495
     * @param $action
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
496
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
497
     * @param $version
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
498
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
499
    public function upgrade(Application $app, Request $request, $action, $id, $version)
500
    {
501
502
        $BaseInfo = $app['eccube.repository.base_info']->get();
503
504
        $authKey = $BaseInfo->getAuthenticationKey();
505
        $message = '';
506
507
        if (!is_null($authKey)) {
508
509
            // オーナーズストア通信
510
            $url = $app['config']['owners_store_url'] . '?method=download&product_id=' . $id;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
511
            list($json, $httpHeader) = $this->getRequestApi($request, $authKey, $url);
512
513
            if ($json === false) {
514
                // 接続失敗時
515
516
                $message = $this->getResponseErrorMessage($httpHeader);
517
518
            } else {
519
                // 接続成功時
520
521
                $data = json_decode($json, true);
522
523
                if (isset($data['success'])) {
524
                    $success = $data['success'];
525
                    if ($success == '1') {
526
                        $tmpDir = null;
527
                        try {
528
                            $service = $app['eccube.service.plugin'];
529
530
                            $item = $data['item'];
531
                            $file = base64_decode($item['data']);
532
                            $extension = pathinfo($item['file_name'], PATHINFO_EXTENSION);
533
534
                            $tmpDir = $service->createTempDir();
535
                            $tmpFile = sha1(Str::random(32)) . '.' . $extension;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
536
537
                            // ファイル作成
538
                            $fs = new Filesystem();
539
                            $fs->dumpFile($tmpDir . '/' . $tmpFile, $file);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
540
541
                            if ($action == 'install') {
542
543
                                $service->install($tmpDir . '/' . $tmpFile, $id);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
544
                                $app->addSuccess('admin.plugin.install.complete', 'admin');
545
546
                            } else if ($action == 'update') {
547
548
                                $Plugin = $app['eccube.repository.plugin']->findOneBy(array('source' => $id));
549
550
                                $service->update($Plugin, $tmpDir . '/' . $tmpFile);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
551
                                $app->addSuccess('admin.plugin.update.complete', 'admin');
552
553
                                Cache::clear($app, false);
0 ignored issues
show
Documentation introduced by
$app is of type object<Eccube\Application>, but the function expects a object<Eccube\Util\Application>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
554
555
                            }
556
557
                            $fs = new Filesystem();
558
                            $fs->remove($tmpDir);
559
560
                            // ダウンロード完了通知処理(正常終了時)
561
                            $url = $app['config']['owners_store_url'] . '?method=commit&product_id=' . $id . '&status=1&version=' . $version;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
562
                            $this->getRequestApi($request, $authKey, $url);
563
564
                            return $app->redirect($app->url('admin_store_plugin'));
565
566
                        } catch (PluginException $e) {
567
                            if (!empty($tmpDir) && file_exists($tmpDir)) {
568
                                $fs = new Filesystem();
569
                                $fs->remove($tmpDir);
570
                            }
571
                            $message = $e->getMessage();
572
                        }
573
574
                    } else {
575
                        $message = $data['error_code'] . ' : ' . $data['error_message'];
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
576
                    }
577
                } else {
578
                    $message = "EC-CUBEオーナーズストアにエラーが発生しています。";
579
                }
580
            }
581
        }
582
583
        // ダウンロード完了通知処理(エラー発生時)
584
        $url = $app['config']['owners_store_url'] . '?method=commit&product_id=' . $id . '&status=0&version=' . $version . '&message=' . urlencode($message);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
585
        $this->getRequestApi($request, $authKey, $url);
586
587
        $app->addError($message, 'admin');
588
589
        return $app->redirect($app->url('admin_store_plugin_owners_install'));
590
    }
591
592
    /**
593
     * 認証キー設定画面
594
     *
595
     * @param Application $app
596
     * @param Request $request
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
597
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
598
    public function authenticationSetting(Application $app, Request $request)
599
    {
600
601
        $form = $app->form()->getForm();
602
603
        $BaseInfo = $app['eccube.repository.base_info']->get();
604
605
        // 認証キーの取得
606
        $form->add(
607
            'authentication_key', 'text', array(
608
            'label' => '認証キー',
609
            'constraints' => array(
610
                new Assert\Regex(array(
611
                    'pattern' => "/^[0-9a-zA-Z]+$/",
612
                )),
613
            ),
614
            'data' => $BaseInfo->getAuthenticationKey(),
615
        ));
1 ignored issue
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 12 spaces, but found 8).
Loading history...
616
617
        if ('POST' === $request->getMethod()) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 8
Loading history...
618
            $form->handleRequest($request);
1 ignored issue
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 16 spaces, but found 12).
Loading history...
619
620
            if ($form->isValid()) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 12
Loading history...
621
                $data = $form->getData();
1 ignored issue
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 20 spaces, but found 16).
Loading history...
622
623
                // 認証キーの登録
624
                $BaseInfo->setAuthenticationKey($data['authentication_key']);
1 ignored issue
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 20 spaces, but found 16).
Loading history...
625
                $app['orm.em']->flush($BaseInfo);
1 ignored issue
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 20 spaces, but found 16).
Loading history...
626
627
                $app->addSuccess('admin.plugin.authentication.setting.complete', 'admin');
1 ignored issue
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 20 spaces, but found 16).
Loading history...
628
629
            }
630
        }
631
632
633
        return $app->render('Store/authentication_setting.twig', array(
634
            'form' => $form->createView(),
635
        ));
636
637
    }
638
639
640
    /**
641
     * APIリクエスト処理
642
     *
643
     * @param Request $request
644
     * @param $authKey
645
     * @param $url
646
     * @return array
647
     */
648
    private function getRequestApi(Request $request, $authKey, $url)
649
    {
650
        $opts = array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
651
            'http' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
652
                'method' => 'GET',
653
                'ignore_errors' => false,
654
                'timeout' => 60,
655
                'header' => array(
0 ignored issues
show
introduced by
Add a comma after each item in a multi-line array
Loading history...
656
                    'Authorization: ' . base64_encode($authKey),
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
657
                    'x-eccube-store-url: ' . base64_encode($request->getSchemeAndHttpHost() . $request->getBasePath()),
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
658
                    'x-eccube-store-version: ' . base64_encode(Constant::VERSION)
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
659
                )
660
            )
661
        );
662
663
        $context = stream_context_create($opts);
664
665
        $json = @file_get_contents($url, false, $context);
666
667
        return array($json, $http_response_header);
668
    }
669
670
    /**
671
     * レスポンスヘッダーのチェック
672
     *
673
     * @param $httpHeader
674
     * @return string
675
     */
676
    private function getResponseErrorMessage($httpHeader)
677
    {
678
        if (!empty($httpHeader)) {
679
            list($version, $statusCode, $message) = explode(' ', $httpHeader[0], 3);
0 ignored issues
show
Unused Code introduced by
The assignment to $version is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
680
681
            switch ($statusCode) {
682
                case '404':
683
                    $message = $statusCode . ' : ' . $message;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
684
                    break;
685
                case '500':
686
                    $message = $statusCode . ' : ' . $message;
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
687
                    break;
688
                default:
689
                    $message = "EC-CUBEオーナーズストアにエラーが発生しています。";
690
                    break;
691
            }
692
        } else {
693
            $message = "タイムアウトエラーまたはURLの指定に誤りがあります。";
694
        }
695
696
        return $message;
697
    }
698
699
}
700