Completed
Push — sf/composer-install-update ( a1e834 )
by Kiyotaka
10:44
created

PluginApiService::getPlugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Service;
15
16
use Eccube\Common\Constant;
17
use Eccube\Common\EccubeConfig;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
20
class PluginApiService
21
{
22
    /**
23
     * Url for Api
24
     *
25
     * @var string
26
     */
27
    private $apiUrl;
28
29
    /**
30
     * @var EccubeConfig
31
     */
32
    private $eccubeConfig;
33
34
    /**
35
     * @var RequestStack
36
     */
37
    private $requestStack;
38
39
    /**
40
     * PluginApiService constructor.
41
     *
42
     * @param EccubeConfig $eccubeConfig
43
     * @param RequestStack $requestStack
44
     */
45
    public function __construct(EccubeConfig $eccubeConfig, RequestStack $requestStack)
46
    {
47
        $this->eccubeConfig = $eccubeConfig;
48
        $this->requestStack = $requestStack;
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54
    public function getApiUrl()
55
    {
56
        if (empty($this->apiUrl)) {
57
            return $this->eccubeConfig->get('eccube_package_repo_url');
58
        }
59
60
        return $this->apiUrl;
61
    }
62
63
    /**
64
     * @param mixed $apiUrl
65
     */
66
    public function setApiUrl($apiUrl)
67
    {
68
        $this->apiUrl = $apiUrl;
69
    }
70
71
    /**
72
     * Get master data: category
73
     *
74
     * @return array($result, $info)
0 ignored issues
show
Documentation introduced by
The doc-type array($result, could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
75
     */
76
    public function getCategory()
77
    {
78
        $urlCategory = $this->getApiUrl().'/category';
79
80
        return $this->getRequestApi($urlCategory);
81
    }
82
83
    /**
84
     * Get plugins list
85
     *
86
     * @param array $data
87
     *
88
     * @return array($result, $info)
0 ignored issues
show
Documentation introduced by
The doc-type array($result, could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
89
     */
90
    public function getPlugins($data)
91
    {
92
        $url = $this->getApiUrl().'/plugins';
93
        $params['category_id'] = $data['category_id'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
94
        $params['price_type'] = empty($data['price_type']) ? 'all' : $data['price_type'];
95
        $params['keyword'] = $data['keyword'];
96
        $params['sort'] = $data['sort'];
97
        $params['page'] = (isset($data['page_no']) && !empty($data['page_no'])) ? $data['page_no'] : 1;
98
        $params['per_page'] = (isset($data['page_count']) && !empty($data['page_count'])) ? $data['page_count'] : $this->eccubeConfig->get('eccube_default_page_count');
99
100
        return $this->getRequestApi($url, $params);
101
    }
102
103
    /**
104
     * Get a plugin
105
     *
106
     * @param int|string $id Id or plugin code
107
     * @return array [$result, $info]
108
     */
109
    public function getPlugin($id)
110
    {
111
        $url = $this->getApiUrl() . '/plugin/'.$id;
112
113
        return $this->getRequestApi($url);
114
    }
115
116
    /**
117
     * Get captcha image
118
     *
119
     * @return array($result, $info)
0 ignored issues
show
Documentation introduced by
The doc-type array($result, could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
120
     */
121
    public function getCaptcha()
122
    {
123
        $apiUrl = $this->getApiUrl().'/captcha';
124
125
        $requestApi = $this->getRequestApi($apiUrl);
126
127
        return $requestApi;
128
    }
129
130
    /**
131
     * Get api key from captcha image
132
     *
133
     * @param array $data
134
     *
135
     * @return array($result, $info)
0 ignored issues
show
Documentation introduced by
The doc-type array($result, could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
136
     */
137
    public function postApiKey($data)
138
    {
139
        $apiUrl = $this->getApiUrl().'/api_key';
140
141
        $baseUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost().$this->requestStack->getCurrentRequest()->getBasePath();
142
        $data['eccube_url'] = $baseUrl;
143
        $data['eccube_version'] = Constant::VERSION;
144
145
        $requestApi = $this->postRequestApi($apiUrl, $data);
146
147
        return $requestApi;
148
    }
149
150
    /**
151
     * API post
152
     *
153
     * @param string  $url
154
     * @param array $data
155
     *
156
     * @return array($result, $info)
0 ignored issues
show
Documentation introduced by
The doc-type array($result, could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
157
     */
158
    public function postRequestApi($url, $data = [])
159
    {
160
        $curl = curl_init($url);
161
        curl_setopt($curl, CURLOPT_POST, 1);
162
163
        if (count($data) > 0) {
164
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
165
        }
166
167
        // Todo: will implement after server worked
168
        $key = null;
169
        $baseUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost().$this->requestStack->getCurrentRequest()->getBasePath();
170
        // Option array
171
        $options = [
172
            // HEADER
173
            CURLOPT_HTTPHEADER => [
174
                'X-ECCUBE-KEY: '.base64_encode($key),
175
                'X-ECCUBE-URL: '.base64_encode($baseUrl),
176
                'X-ECCUBE-VERSION: '.base64_encode(Constant::VERSION),
177
            ],
178
            CURLOPT_HTTPGET => true,
179
            CURLOPT_SSL_VERIFYPEER => false,
180
            CURLOPT_RETURNTRANSFER => true,
181
            CURLOPT_FAILONERROR => true,
182
            CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
183
        ];
184
185
        // Set option value
186
        curl_setopt_array($curl, $options);
187
        $result = curl_exec($curl);
188
        $info = curl_getinfo($curl);
189
        $message = curl_error($curl);
190
        $info['message'] = $message;
191
        curl_close($curl);
192
193
        log_info('http get_info', $info);
194
195
        return [$result, $info];
196
    }
197
198
    /**
199
     * API request processing
200
     *
201
     * @param string  $url
202
     * @param array $data
203
     *
204
     * @return array($result, $info)
0 ignored issues
show
Documentation introduced by
The doc-type array($result, could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
205
     */
206
    public function getRequestApi($url, $data = [])
207
    {
208
        if (count($data) > 0) {
209
            $url .= '?'.http_build_query($data);
210
        }
211
212
        $curl = curl_init($url);
213
214
        // Option array
215
        $options = [
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
        log_info('http get_info', $info);
233
234
        return [$result, $info];
235
    }
236
237
    /**
238
     * Get message
239
     *
240
     * @param $info
241
     *
242
     * @return string
243
     */
244 View Code Duplication
    public 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...
245
    {
246
        if (!empty($info)) {
247
            $statusCode = $info['http_code'];
248
            $message = $info['message'];
249
            $message = $statusCode.' : '.$message;
250
        } else {
251
            $message = trans('ownerstore.text.error.timeout');
252
        }
253
254
        return $message;
255
    }
256
}
257