Completed
Pull Request — experimental/3.1 (#2537)
by
unknown
36:24 queued 04:38
created

OwnerStoreController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 188
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 32
loc 188
rs 10
c 1
b 0
f 0
wmc 20
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
C search() 19 78 14
A apiInstall() 0 21 3
B getRequestApi() 0 26 1
A getResponseErrorMessage() 13 13 2

How to fix   Duplicated Code   

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:

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 Acme\Controller\Admin\Store;
26
27
use Eccube\Annotation\Component;
28
use Eccube\Annotation\Inject;
29
use Eccube\Application;
30
use Eccube\Common\Constant;
31
use Eccube\Controller\AbstractController;
32
use Eccube\Entity\Plugin;
33
use Eccube\Repository\PluginRepository;
34
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
35
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
36
use Symfony\Component\Config\Definition\Exception\Exception;
37
use Symfony\Component\HttpFoundation\RedirectResponse;
38
use Symfony\Component\HttpFoundation\Request;
39
use Symfony\Component\Process\Process;
40
41
/**
42
 * @Component
43
 * @Route(service=OwnerStoreController::class)
44
 */
45
class OwnerStoreController extends AbstractController
46
{
47
    /**
48
     * @Inject("config")
49
     * @var array
50
     */
51
    protected $appConfig;
52
53
    /**
54
     * @Inject(PluginRepository::class)
55
     * @var PluginRepository
56
     */
57
    protected $pluginRepository;
58
59
    /**
60
     * Owner's Store Plugin Installation Screen - Search function
61
     *
62
     * @Route("/{_admin}/store/plugin/search", name="admin_store_plugin_owners_search")
63
     * @Template("Store/plugin_search.twig")
64
     * @param Application $app
65
     * @param Request     $request
66
     * @return array
67
     */
68
    public function search(Application $app, Request $request)
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...
69
    {
70
        // Acquire downloadable plug-in information from owners store
71
        $success = 0;
72
        $items = array();
73
        $promotionItems = array();
74
        $message = '';
75
        // Owner's store communication
76
        $url = $this->appConfig['owners_store_url'].'?method=list';
77
        list($json, $info) = $this->getRequestApi($url, $app);
78
        if ($json === false) {
79
            $message = $this->getResponseErrorMessage($info);
80
        } else {
81
            $data = json_decode($json, true);
82
            if (isset($data['success'])) {
83
                $success = $data['success'];
84
                if ($success == '1') {
85
                    $items = array();
86
                    // Check plugin installed
87
                    $arrPluginInstalled = $this->pluginRepository->findAll();
88
                    // Update_status 1 : not install/purchased 、2 : Installed、 3 : Update、4 : paid purchase
89
                    foreach ($data['item'] as $item) {
90
                        // Not install/purchased
91
                        $item['update_status'] = 1;
92
                        /** @var Plugin $plugin */
93
                        foreach ($arrPluginInstalled as $plugin) {
94
                            if ($plugin->getSource() == $item['product_id']) {
95
                                // Need update
96
                                $item['update_status'] = 3;
97
                                if ($plugin->getVersion() == $item['version']) {
98
                                    // Installed
99
                                    $item['update_status'] = 2;
100
                                }
101
                            }
102
                        }
103
                        $items[] = $item;
104
                    }
105
106
                    // EC-CUBE version check
107 View Code Duplication
                    foreach ($items as &$item) {
108
                        // Not applicable version
109
                        $item['version_check'] = 0;
110
                        if (in_array(Constant::VERSION, $item['eccube_version'])) {
111
                            // Match version
112
                            $item['version_check'] = 1;
113
                        }
114
                        if ($item['price'] != '0' && $item['purchased'] == '0') {
115
                            // Not purchased with paid items
116
                            $item['update_status'] = 4;
117
                        }
118
                    }
119
                    unset($item);
120
121
                    // Promotion item
122
                    $i = 0;
123 View Code Duplication
                    foreach ($items as $item) {
124
                        if ($item['promotion'] == 1) {
125
                            $promotionItems[] = $item;
126
                            unset($items[$i]);
127
                        }
128
                        $i++;
129
                    }
130
                } else {
131
                    $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...
132
                }
133
            } else {
134
                $success = 0;
135
                $message = "EC-CUBEオーナーズストアにエラーが発生しています。";
136
            }
137
        }
138
139
        return [
140
            'success' => $success,
141
            'items' => $items,
142
            'promotionItems' => $promotionItems,
143
            'message' => $message,
144
        ];
145
    }
146
147
    /**
148
     * Api Install plugin by composer connect with packagist
149
     *
150
     * @Route("/{_admin}/store/plugin/api/{pluginCode}" , name="admin_store_plugin_api_install")
151
     *
152
     * @param Application $app
153
     * @param Request     $request
154
     * @param string      $pluginCode
155
     * @return RedirectResponse
156
     */
157
    public function apiInstall(Application $app, Request $request, $pluginCode)
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...
158
    {
159
        try {
160
            $install = new Process(sprintf("cd %s && composer require ec-cube/{$pluginCode}", $this->appConfig['root_dir']));
161
            $install->setTimeout(null);
162
            $install->run();
163
            if ($install->isSuccessful()) {
164
                $app->addSuccess('admin.plugin.install.complete', 'admin');
165
                $app->log("Install $pluginCode successful!");
166
167
                return $app->redirect($app->url('admin_store_plugin'));
168
            }
169
            $app->addError("Install $pluginCode fail!", 'admin');
170
        } catch (Exception $exception) {
171
            $app->addError($exception->getMessage(), 'admin');
172
            $app->log($exception->getCode().' : '.$exception->getMessage());
173
        }
174
        $app->log("Install $pluginCode fail!");
175
176
        return $app->redirect($app->url('admin_store_plugin_owners_search'));
177
    }
178
179
    /**
180
     * API request processing
181
     *
182
     * @param string  $url
183
     * @param Application $app
184
     * @return array
185
     */
186
    private function getRequestApi($url, $app)
187
    {
188
        $curl = curl_init($url);
189
190
        // Option array
191
        $options = array(
192
            // HEADER
193
            CURLOPT_HTTPGET => true,
194
            CURLOPT_SSL_VERIFYPEER => false,
195
            CURLOPT_RETURNTRANSFER => true,
196
            CURLOPT_FAILONERROR => true,
197
            CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
198
        );
199
200
        // Set option value
201
        curl_setopt_array($curl, $options);
202
        $result = curl_exec($curl);
203
        $info = curl_getinfo($curl);
204
        $message = curl_error($curl);
205
        $info['message'] = $message;
206
        curl_close($curl);
207
208
        $app->log('http get_info', $info);
209
210
        return array($result, $info);
211
    }
212
213
    /**
214
     * Get message
215
     *
216
     * @param $info
217
     * @return string
218
     */
219 View Code Duplication
    private function getResponseErrorMessage($info)
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...
220
    {
221
        if (!empty($info)) {
222
            $statusCode = $info['http_code'];
223
            $message = $info['message'];
224
225
            $message = $statusCode.' : '.$message;
226
        } else {
227
            $message = "タイムアウトエラーまたはURLの指定に誤りがあります。";
228
        }
229
230
        return $message;
231
    }
232
}
233