Failed Conditions
Pull Request — 4.0 (#3723)
by Kiyotaka
07:29
created

PluginApiService::getPurchased()   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 0
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 Eccube\Entity\BaseInfo;
19
use Eccube\Repository\BaseInfoRepository;
20
use Symfony\Component\HttpFoundation\RequestStack;
21
22
class PluginApiService
23
{
24
    /**
25
     * Url for Api
26
     *
27
     * @var string
28
     */
29
    private $apiUrl;
30
31
    /**
32
     * @var EccubeConfig
33
     */
34
    private $eccubeConfig;
35
36
    /**
37
     * @var RequestStack
38
     */
39
    private $requestStack;
40
41
    /**
42
     * @var BaseInfo
43
     */
44
    private $BaseInfo;
45
46
    /**
47
     * PluginApiService constructor.
48
     *
49
     * @param EccubeConfig $eccubeConfig
50
     * @param RequestStack $requestStack
51
     * @param BaseInfoRepository $baseInfoRepository
52
     *
53
     * @throws \Doctrine\ORM\NoResultException
54
     * @throws \Doctrine\ORM\NonUniqueResultException
55
     */
56
    public function __construct(EccubeConfig $eccubeConfig, RequestStack $requestStack, BaseInfoRepository $baseInfoRepository)
57
    {
58
        $this->eccubeConfig = $eccubeConfig;
59
        $this->requestStack = $requestStack;
60
        $this->BaseInfo = $baseInfoRepository->get();
61
    }
62
63
    /**
64
     * @return mixed
65
     */
66
    public function getApiUrl()
67
    {
68
        if (empty($this->apiUrl)) {
69
            return $this->eccubeConfig->get('eccube_package_api_url');
70
        }
71
72
        return $this->apiUrl;
73
    }
74
75
    /**
76
     * @param mixed $apiUrl
77
     */
78
    public function setApiUrl($apiUrl)
79
    {
80
        $this->apiUrl = $apiUrl;
81
    }
82
83
    /**
84
     * Get master data: category
85
     *
86
     * @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...
87
     */
88
    public function getCategory()
89
    {
90
        $urlCategory = $this->getApiUrl().'/category';
91
92
        return $this->getRequestApi($urlCategory);
93
    }
94
95
    /**
96
     * Get plugins list
97
     *
98
     * @param array $data
99
     *
100
     * @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...
101
     */
102
    public function getPlugins($data)
103
    {
104
        $url = $this->getApiUrl().'/plugins';
105
        $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...
106
        $params['price_type'] = empty($data['price_type']) ? 'all' : $data['price_type'];
107
        $params['keyword'] = $data['keyword'];
108
        $params['sort'] = $data['sort'];
109
        $params['page'] = (isset($data['page_no']) && !empty($data['page_no'])) ? $data['page_no'] : 1;
110
        $params['per_page'] = (isset($data['page_count']) && !empty($data['page_count'])) ? $data['page_count'] : $this->eccubeConfig->get('eccube_default_page_count');
111
112
        return $this->getRequestApi($url, $params);
113
    }
114
115
    /**
116
     * Get purchased plugins list
117
     *
118
     * @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...
119
     */
120
    public function getPurchased()
121
    {
122
        $url = $this->getApiUrl().'/plugins/purchased';
123
124
        return $this->getRequestApi($url);
125
    }
126
127
    /**
128
     * Get a plugin
129
     *
130
     * @param int|string $id Id or plugin code
131
     *
132
     * @return array [$result, $info]
133
     */
134
    public function getPlugin($id)
135
    {
136
        $url = $this->getApiUrl().'/plugin/'.$id;
137
138
        return $this->getRequestApi($url);
139
    }
140
141
    /**
142
     * Get captcha image
143
     *
144
     * @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...
145
     */
146
    public function getCaptcha()
147
    {
148
        $apiUrl = $this->getApiUrl().'/captcha';
149
150
        $requestApi = $this->getRequestApi($apiUrl);
151
152
        return $requestApi;
153
    }
154
155
    /**
156
     * Get api key from captcha image
157
     *
158
     * @param array $data
159
     *
160
     * @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...
161
     */
162
    public function postApiKey($data)
163
    {
164
        $apiUrl = $this->getApiUrl().'/api_key';
165
166
        $baseUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost().$this->requestStack->getCurrentRequest()->getBasePath();
167
        $data['eccube_url'] = $baseUrl;
168
        $data['eccube_version'] = Constant::VERSION;
169
170
        $requestApi = $this->postRequestApi($apiUrl, $data);
171
172
        return $requestApi;
173
    }
174
175
    /**
176
     * API post
177
     *
178
     * @param string  $url
179
     * @param array $data
180
     *
181
     * @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...
182
     */
183
    public function postRequestApi($url, $data = [])
184
    {
185
        $curl = curl_init($url);
186
        curl_setopt($curl, CURLOPT_POST, 1);
187
188
        if (count($data) > 0) {
189
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
190
        }
191
192
        // Todo: will implement after server worked
193
        $key = null;
194
        $baseUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost().$this->requestStack->getCurrentRequest()->getBasePath();
195
        // Option array
196
        $options = [
197
            // HEADER
198
            CURLOPT_HTTPHEADER => [
199
                'X-ECCUBE-KEY: '.base64_encode($key),
200
                'X-ECCUBE-URL: '.base64_encode($baseUrl),
201
                'X-ECCUBE-VERSION: '.base64_encode(Constant::VERSION),
202
            ],
203
            CURLOPT_HTTPGET => true,
204
            CURLOPT_SSL_VERIFYPEER => false,
205
            CURLOPT_RETURNTRANSFER => true,
206
            CURLOPT_FAILONERROR => true,
207
            CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
208
        ];
209
210
        // Set option value
211
        curl_setopt_array($curl, $options);
212
        $result = curl_exec($curl);
213
        $info = curl_getinfo($curl);
214
        $message = curl_error($curl);
215
        $info['message'] = $message;
216
        curl_close($curl);
217
218
        log_info('http get_info', $info);
219
220
        return [$result, $info];
221
    }
222
223
    /**
224
     * API request processing
225
     *
226
     * @param string  $url
227
     * @param array $data
228
     *
229
     * @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...
230
     */
231
    public function getRequestApi($url, $data = [])
232
    {
233
        if (count($data) > 0) {
234
            $url .= '?'.http_build_query($data);
235
        }
236
237
        $curl = curl_init($url);
238
239
        $key = $this->BaseInfo->getAuthenticationKey();
240
        $baseUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost().$this->requestStack->getCurrentRequest()->getBasePath();
241
242
        // Option array
243
        $options = [
244
            // HEADER
245
            CURLOPT_HTTPHEADER => [
246
                'X-ECCUBE-KEY: '.$key,
247
                'X-ECCUBE-URL: '.$baseUrl,
248
                'X-ECCUBE-VERSION: '.Constant::VERSION,
249
            ],
250
            CURLOPT_HTTPGET => true,
251
            CURLOPT_SSL_VERIFYPEER => false,
252
            CURLOPT_RETURNTRANSFER => true,
253
            CURLOPT_FAILONERROR => true,
254
            CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
255
        ];
256
257
        // Set option value
258
        curl_setopt_array($curl, $options);
259
        $result = curl_exec($curl);
260
        $info = curl_getinfo($curl);
261
        $message = curl_error($curl);
262
        $info['message'] = $message;
263
        curl_close($curl);
264
265
        log_info('http get_info', $info);
266
267
        return [$result, $info];
268
    }
269
270
    /**
271
     * Get message
272
     *
273
     * @param $info
274
     *
275
     * @return string
276
     */
277
    public function getResponseErrorMessage($info)
278
    {
279
        if (!empty($info)) {
280
            $statusCode = $info['http_code'];
281
            $message = $info['message'];
282
            $message = $statusCode.' : '.$message;
283
        } else {
284
            $message = trans('ownerstore.text.error.timeout');
285
        }
286
287
        return $message;
288
    }
289
}
290