Failed Conditions
Push — sf/package-api ( 831849...a77dd1 )
by Kiyotaka
06:40
created

PluginApiService::getPurchased()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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