Failed Conditions
Pull Request — experimental/3.1 (#2583)
by
unknown
38:12
created

OwnerStoreController::getRequestApi()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 2
dl 0
loc 26
ccs 0
cts 0
cp 0
crap 2
rs 8.8571
c 0
b 0
f 0
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\Annotation\Inject;
28
use Eccube\Application;
29
use Eccube\Common\Constant;
30
use Eccube\Controller\AbstractController;
31
use Eccube\Entity\Plugin;
32
use Eccube\Repository\PluginRepository;
33
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
34
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
35
use Symfony\Component\Config\Definition\Exception\Exception;
36
use Symfony\Component\HttpFoundation\RedirectResponse;
37
use Symfony\Component\HttpFoundation\Request;
38
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
39
use Symfony\Component\Process\Process;
40
41
/**
42
 * @Route(service=OwnerStoreController::class)
43
 */
44
class OwnerStoreController extends AbstractController
45
{
46
    /**
47
     * @Inject("config")
48
     * @var array
49
     */
50
    protected $appConfig;
51
52
    /**
53
     * @Inject(PluginRepository::class)
54
     * @var PluginRepository
55
     */
56
    protected $pluginRepository;
57
58
    /**
59
     * Owner's Store Plugin Installation Screen - Search function
60
     *
61
     * @Route("/{_admin}/store/plugin/search", name="admin_store_plugin_owners_search")
62
     * @Template("Store/plugin_search.twig")
63
     * @param Application $app
64
     * @param Request     $request
65
     * @return array
66
     */
67
    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...
68
    {
69
        // Acquire downloadable plug-in information from owners store
70
        $success = 0;
71
        $items = array();
72
        $promotionItems = array();
73
        $message = '';
74
        // Owner's store communication
75
        $url = $this->appConfig['owners_store_url'].'?method=list';
76
        list($json, $info) = $this->getRequestApi($url, $app);
77
        if ($json === false) {
78
            $message = $this->getResponseErrorMessage($info);
79
        } else {
80
            $data = json_decode($json, true);
81
            if (isset($data['success'])) {
82
                $success = $data['success'];
83
                if ($success == '1') {
84
                    $items = array();
85
                    // Check plugin installed
86
                    $arrPluginInstalled = $this->pluginRepository->findAll();
87
                    // Update_status 1 : not install/purchased 、2 : Installed、 3 : Update、4 : paid purchase
88
                    foreach ($data['item'] as $item) {
89
                        // Not install/purchased
90
                        $item['update_status'] = 1;
91
                        /** @var Plugin $plugin */
92
                        foreach ($arrPluginInstalled as $plugin) {
93
                            if ($plugin->getSource() == $item['product_id']) {
94
                                // Need update
95
                                $item['update_status'] = 3;
96
                                if ($plugin->getVersion() == $item['version']) {
97
                                    // Installed
98
                                    $item['update_status'] = 2;
99
                                }
100
                            }
101
                        }
102
                        $items[] = $item;
103
                    }
104
105
                    // EC-CUBE version check
106
                    $arrDependency = [];
107
                    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
                        // Add plugin dependency
119
                        $app['eccube.service.plugin']->getDependency($item, $items, $arrDependency);
120
                    }
121
                    unset($item);
122
                    unset($arrDependency);
123
124
                    // Promotion item
125
                    $i = 0;
126 View Code Duplication
                    foreach ($items as $item) {
127
                        if ($item['promotion'] == 1) {
128
                            $promotionItems[] = $item;
129
                            unset($items[$i]);
130
                        }
131
                        $i++;
132
                    }
133
                } else {
134
                    $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...
135
                }
136
            } else {
137
                $success = 0;
138
                $message = "EC-CUBEオーナーズストアにエラーが発生しています。";
139
            }
140
        }
141
142
        return [
143
            'success' => $success,
144
            'items' => $items,
145
            'promotionItems' => $promotionItems,
146
            'message' => $message,
147
        ];
148
    }
149
150
    /**
151
     * Do confirm page
152
     *
153
     * @Route("/{_admin}/store/plugin/confirm/{pluginId}" , name="admin_store_plugin_install_confirm")
154
     * @Template("Store/plugin_confirm.twig")
155
     * @param Application $app
156
     * @param Request     $request
157
     * @param string      $pluginId
158
     * @return array
159
     */
160
    public function doConfirm(Application $app, Request $request, $pluginId)
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...
161
    {
162
        // Owner's store communication
163
        $url = $this->appConfig['owners_store_url'].'?method=list';
164
        list($json, $info) = $this->getRequestApi($url, $app);
0 ignored issues
show
Unused Code introduced by
The assignment to $info 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...
165
        $data = json_decode($json, true);
166
        $items = $data['item'];
167
168
        // Find plugin in api
169
        $index = array_search($pluginId, array_column($items, 'product_id'));
170
        if ($index === false) {
171
            throw new NotFoundHttpException();
172
        }
173
        // Get target plugin in return of api
174
        $plugin = $items[$index];
175
176
        // Check the eccube version that the plugin supports.
177
        $plugin['is_supported_eccube_version'] = 0;
178
        if (in_array(Constant::VERSION, $plugin['eccube_version'])) {
179
            // Match version
180
            $plugin['is_supported_eccube_version'] = 1;
181
        }
182
183
        $arrDependency = [];
184
        $app['eccube.service.plugin']->getDependency($plugin, $items, $arrDependency);
185
186
        return [
187
            'item' => $plugin,
188
            'arrDependency' => $arrDependency,
189
        ];
190
    }
191
192
    /**
193
     * Api Install plugin by composer connect with package repo
194
     *
195
     * @Route("/{_admin}/store/plugin/api/{pluginCode}/{eccubeVersion}/{version}" , name="admin_store_plugin_api_install")
196
     *
197
     * @param Application $app
198
     * @param Request     $request
199
     * @param string      $pluginCode
200
     * @param string      $eccubeVersion
201
     * @param string      $version
202
     * @return RedirectResponse
203
     */
204
    public function apiInstall(Application $app, Request $request, $pluginCode, $eccubeVersion, $version)
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...
205
    {
206
        // Check plugin code
207
        $url = $this->appConfig['owners_store_url'].'?eccube_version='.$eccubeVersion.'&plugin_code='.$pluginCode.'&version='.$version;
208
        list($json, $info) = $this->getRequestApi($url, $app);
0 ignored issues
show
Unused Code introduced by
The assignment to $info 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...
209
        $existFlg = false;
210
        $data = json_decode($json, true);
211
        if ($data && isset($data['success'])) {
212
            $success = $data['success'];
213
            if ($success == '1') {
214
                foreach ($data['item'] as $item) {
215
                    if ($item['product_code'] == $pluginCode) {
216
                        $existFlg = true;
217
                        break;
218
                    }
219
                }
220
            }
221
        }
222
        if ($existFlg === false) {
223
            $app->log(sprintf('%s plugin not found!', $pluginCode));
224
            $app->addError('admin.plugin.not.found', 'admin');
225
226
            return $app->redirect($app->url('admin_store_plugin_owners_search'));
227
        }
228
229
        try {
230
            $execute = sprintf('cd %s &&', $this->appConfig['root_dir']);
231
            $execute .= sprintf(' composer require ec-cube/%s', $pluginCode);
232
233
            $install = new Process($execute);
234
            $install->setTimeout(null);
235
            $install->run();
236
            if ($install->isSuccessful()) {
237
                $app->addSuccess('admin.plugin.install.complete', 'admin');
238
                $app->log(sprintf('Install %s plugin successful!', $pluginCode));
239
240
                return $app->redirect($app->url('admin_store_plugin'));
241
            }
242
            $app->addError('admin.plugin.install.fail', 'admin');
243
        } catch (Exception $exception) {
244
            $app->addError($exception->getMessage(), 'admin');
245
            $app->log($exception->getCode().' : '.$exception->getMessage());
246
        }
247
        $app->log(sprintf('Install %s plugin fail!', $pluginCode));
248
249
        return $app->redirect($app->url('admin_store_plugin_owners_search'));
250
    }
251
252
    /**
253
     * API request processing
254
     *
255
     * @param string  $url
256
     * @param Application $app
257
     * @return array
258
     */
259
    private function getRequestApi($url, $app)
260
    {
261
        $curl = curl_init($url);
262
263
        // Option array
264
        $options = array(
265
            // HEADER
266
            CURLOPT_HTTPGET => true,
267
            CURLOPT_SSL_VERIFYPEER => false,
268
            CURLOPT_RETURNTRANSFER => true,
269
            CURLOPT_FAILONERROR => true,
270
            CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
271
        );
272
273
        // Set option value
274
        curl_setopt_array($curl, $options);
275
        $result = curl_exec($curl);
276
        $info = curl_getinfo($curl);
277
        $message = curl_error($curl);
278
        $info['message'] = $message;
279
        curl_close($curl);
280
281
        $app->log('http get_info', $info);
282
283
        return array($result, $info);
284
    }
285
286
    /**
287
     * Get message
288
     *
289
     * @param $info
290
     * @return string
291
     */
292 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...
293
    {
294
        if (!empty($info)) {
295
            $statusCode = $info['http_code'];
296
            $message = $info['message'];
297
298
            $message = $statusCode.' : '.$message;
299
        } else {
300
            $message = "タイムアウトエラーまたはURLの指定に誤りがあります。";
301
        }
302
303
        return $message;
304
    }
305
}
306