Completed
Pull Request — experimental/3.1 (#2523)
by
unknown
62:53 queued 31:02
created

OwnerStoreController::getResponseErrorMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 13
loc 13
rs 9.4285
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
        // TODO: need change real server
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 、2 : Installed、 3 : Update、4 : paid purchase
87
                    foreach ($data['item'] as $item) {
88
                        // Not install
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'];
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
    /**
146
     * API request processing
147
     * TODO: need change when has real server (url)
148
     *
149
     * @param string  $url
150
     * @param Application $app
151
     * @return array
152
     */
153
    private function getRequestApi($url, $app)
154
    {
155
        $curl = curl_init($url);
156
157
        // Option array
158
        $options = array(
159
            // HEADER
160
            CURLOPT_HTTPGET => true,
161
            CURLOPT_SSL_VERIFYPEER => false,
162
            CURLOPT_RETURNTRANSFER => true,
163
            CURLOPT_FAILONERROR => true,
164
            CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
165
        );
166
167
        // Set option value
168
        curl_setopt_array($curl, $options);
169
        $result = curl_exec($curl);
170
        $info = curl_getinfo($curl);
171
        $message = curl_error($curl);
172
        $info['message'] = $message;
173
        curl_close($curl);
174
175
        $app->log('http get_info', $info);
176
177
        return array($result, $info);
178
    }
179
180
    /**
181
     * Get message
182
     *
183
     * @param $info
184
     * @return string
185
     */
186 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...
187
    {
188
        if (!empty($info)) {
189
            $statusCode = $info['http_code'];
190
            $message = $info['message'];
191
192
            $message = $statusCode.' : '.$message;
193
        } else {
194
            $message = "タイムアウトエラーまたはURLの指定に誤りがあります。";
195
        }
196
197
        return $message;
198
    }
199
}
200