Failed Conditions
Push — experimental/3.1 ( 3d2ede...2919b9 )
by Yangsin
28:59
created

OwnerStoreController::search()   C

Complexity

Conditions 14
Paths 4

Size

Total Lines 78
Code Lines 47

Duplication

Lines 19
Ratio 24.36 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 47
c 1
b 0
f 0
nc 4
nop 2
dl 19
loc 78
rs 5.1581

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 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\HttpFoundation\Request;
37
38
/**
39
 * @Component
40
 * @Route(service=OwnerStoreController::class)
41
 */
42
class OwnerStoreController extends AbstractController
43
{
44
    /**
45
     * @Inject("config")
46
     * @var array
47
     */
48
    protected $appConfig;
49
50
    /**
51
     * @Inject(PluginRepository::class)
52
     * @var PluginRepository
53
     */
54
    protected $pluginRepository;
55
56
    /**
57
     * Owner's Store Plugin Installation Screen - Search function
58
     *
59
     * @Route("/{_admin}/store/plugin/search", name="admin_store_plugin_owners_search")
60
     * @Template("Store/plugin_search.twig")
61
     * @param Application $app
62
     * @param Request     $request
63
     * @return array
64
     */
65
    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...
66
    {
67
        // Acquire downloadable plug-in information from owners store
68
        $success = 0;
69
        $items = array();
70
        $promotionItems = array();
71
        $message = '';
72
        // Owner's store communication
73
        $url = $this->appConfig['owners_store_url'].'?method=list';
74
        list($json, $info) = $this->getRequestApi($url, $app);
75
        if ($json === false) {
76
            $message = $this->getResponseErrorMessage($info);
77
        } else {
78
            $data = json_decode($json, true);
79
            if (isset($data['success'])) {
80
                $success = $data['success'];
81
                if ($success == '1') {
82
                    $items = array();
83
                    // Check plugin installed
84
                    $arrPluginInstalled = $this->pluginRepository->findAll();
85
                    // Update_status 1 : not install/purchased 、2 : Installed、 3 : Update、4 : paid purchase
86
                    foreach ($data['item'] as $item) {
87
                        // Not install/purchased
88
                        $item['update_status'] = 1;
89
                        /** @var Plugin $plugin */
90
                        foreach ($arrPluginInstalled as $plugin) {
91
                            if ($plugin->getSource() == $item['product_id']) {
92
                                // Need update
93
                                $item['update_status'] = 3;
94
                                if ($plugin->getVersion() == $item['version']) {
95
                                    // Installed
96
                                    $item['update_status'] = 2;
97
                                }
98
                            }
99
                        }
100
                        $items[] = $item;
101
                    }
102
103
                    // EC-CUBE version check
104 View Code Duplication
                    foreach ($items as &$item) {
105
                        // Not applicable version
106
                        $item['version_check'] = 0;
107
                        if (in_array(Constant::VERSION, $item['eccube_version'])) {
108
                            // Match version
109
                            $item['version_check'] = 1;
110
                        }
111
                        if ($item['price'] != '0' && $item['purchased'] == '0') {
112
                            // Not purchased with paid items
113
                            $item['update_status'] = 4;
114
                        }
115
                    }
116
                    unset($item);
117
118
                    // Promotion item
119
                    $i = 0;
120 View Code Duplication
                    foreach ($items as $item) {
121
                        if ($item['promotion'] == 1) {
122
                            $promotionItems[] = $item;
123
                            unset($items[$i]);
124
                        }
125
                        $i++;
126
                    }
127
                } else {
128
                    $message = $data['error_code'].' : '.$data['error_message'];
129
                }
130
            } else {
131
                $success = 0;
132
                $message = "EC-CUBEオーナーズストアにエラーが発生しています。";
133
            }
134
        }
135
136
        return [
137
            'success' => $success,
138
            'items' => $items,
139
            'promotionItems' => $promotionItems,
140
            'message' => $message,
141
        ];
142
    }
143
144
    /**
145
     * API request processing
146
     *
147
     * @param string  $url
148
     * @param Application $app
149
     * @return array
150
     */
151
    private function getRequestApi($url, $app)
152
    {
153
        $curl = curl_init($url);
154
155
        // Option array
156
        $options = array(
157
            // HEADER
158
            CURLOPT_HTTPGET => true,
159
            CURLOPT_SSL_VERIFYPEER => false,
160
            CURLOPT_RETURNTRANSFER => true,
161
            CURLOPT_FAILONERROR => true,
162
            CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
163
        );
164
165
        // Set option value
166
        curl_setopt_array($curl, $options);
167
        $result = curl_exec($curl);
168
        $info = curl_getinfo($curl);
169
        $message = curl_error($curl);
170
        $info['message'] = $message;
171
        curl_close($curl);
172
173
        $app->log('http get_info', $info);
174
175
        return array($result, $info);
176
    }
177
178
    /**
179
     * Get message
180
     *
181
     * @param $info
182
     * @return string
183
     */
184 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...
185
    {
186
        if (!empty($info)) {
187
            $statusCode = $info['http_code'];
188
            $message = $info['message'];
189
190
            $message = $statusCode.' : '.$message;
191
        } else {
192
            $message = "タイムアウトエラーまたはURLの指定に誤りがあります。";
193
        }
194
195
        return $message;
196
    }
197
}
198