Failed Conditions
Push — experimental/3.1 ( c6bbe9...5ca513 )
by Yangsin
235:43 queued 228:52
created

OwnerStoreController::search()   C

Complexity

Conditions 14
Paths 4

Size

Total Lines 78
Code Lines 47

Duplication

Lines 19
Ratio 24.36 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
cc 14
eloc 47
nc 4
nop 2
dl 19
loc 78
ccs 0
cts 43
cp 0
crap 210
rs 5.1581
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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