Failed Conditions
Push — 4.0 ( 31bfc5...86a776 )
by Kiyotaka
06:13
created

PluginApiService::setApiUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
namespace Eccube\Service;
13
14
15
use Eccube\Common\Constant;
16
use Eccube\Common\EccubeConfig;
17
use Symfony\Component\HttpFoundation\RequestStack;
18
19
class PluginApiService
20
{
21
    /**
22
     * Url for Api
23
     *
24
     * @var string
25
     */
26
    private $apiUrl;
27
28
    /**
29
     * @var EccubeConfig
30
     */
31
    private $eccubeConfig;
32
33
    /**
34
     * @var RequestStack
35
     */
36
    private $requestStack;
37
38
    /**
39
     * PluginApiService constructor.
40
     * @param EccubeConfig $eccubeConfig
41
     * @param RequestStack $requestStack
42
     */
43
    public function __construct(EccubeConfig $eccubeConfig, RequestStack $requestStack)
44
    {
45
        $this->eccubeConfig = $eccubeConfig;
46
        $this->requestStack = $requestStack;
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function getApiUrl()
53
    {
54
        if (empty($this->apiUrl)) {
55
            return $this->eccubeConfig->get('eccube_package_repo_url');
56
        }
57
58
        return $this->apiUrl;
59
    }
60
61
    /**
62
     * @param mixed $apiUrl
63
     */
64
    public function setApiUrl($apiUrl)
65
    {
66
        $this->apiUrl = $apiUrl;
67
    }
68
69
    /**
70
     * Get master data: category
71
     *
72
     * @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...
73
     */
74
    public function getCategory()
75
    {
76
        $urlCategory = $this->getApiUrl() . '/category';
77
78
        return $this->getRequestApi($urlCategory);
79
    }
80
81
    /**
82
     * Get plugins list
83
     *
84
     * @param array $data
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 getPlugins($data)
88
    {
89
        $url = $this->getApiUrl() . '/plugins';
90
        $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...
91
        $params['price_type'] = empty($data['price_type']) ? 'all' : $data['price_type'];
92
        $params['keyword'] = $data['keyword'];
93
        $params['sort'] = $data['sort'];
94
        $params['page'] = (isset($data['page_no']) && !empty($data['page_no'])) ? $data['page_no'] : 1;
95
        $params['per_page'] = (isset($data['page_count']) && !empty($data['page_count'])) ? $data['page_count'] : $this->eccubeConfig->get('eccube_default_page_count');
96
97
        return $this->getRequestApi($url, $params);
98
    }
99
100
    /**
101
     * Get captcha image
102
     *
103
     * @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...
104
     */
105
    public function getCaptcha()
106
    {
107
        $apiUrl = $this->getApiUrl().'/captcha';
108
109
        $requestApi = $this->getRequestApi($apiUrl);
110
111
        return $requestApi;
112
    }
113
114
    /**
115
     * Get api key from captcha image
116
     *
117
     * @param array $data
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 postApiKey($data)
121
    {
122
        $apiUrl = $this->getApiUrl().'/api_key';
123
124
        $baseUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost() . $this->requestStack->getCurrentRequest()->getBasePath();
125
        $data['eccube_url'] = $baseUrl;
126
        $data['eccube_version'] = Constant::VERSION;
127
128
        $requestApi = $this->postRequestApi($apiUrl, $data);
129
130
        return $requestApi;
131
    }
132
133
    /**
134
     * API post
135
     *
136
     * @param string  $url
137
     * @param array $data
138
     *
139
     * @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...
140
     */
141
    public function postRequestApi($url, $data = array())
142
    {
143
        $curl = curl_init($url);
144
        curl_setopt($curl, CURLOPT_POST, 1);
145
146
        if (count($data) > 0) {
147
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
148
        }
149
150
        // Todo: will implement after server worked
151
        $key = null;
152
        $baseUrl = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost() . $this->requestStack->getCurrentRequest()->getBasePath();
153
        // Option array
154
        $options = [
155
            // HEADER
156
            CURLOPT_HTTPHEADER => [
157
                'X-ECCUBE-KEY: '.base64_encode($key),
158
                'X-ECCUBE-URL: '.base64_encode($baseUrl),
159
                'X-ECCUBE-VERSION: '.base64_encode(Constant::VERSION),
160
            ],
161
            CURLOPT_HTTPGET => true,
162
            CURLOPT_SSL_VERIFYPEER => false,
163
            CURLOPT_RETURNTRANSFER => true,
164
            CURLOPT_FAILONERROR => true,
165
            CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
166
        ];
167
168
        // Set option value
169
        curl_setopt_array($curl, $options);
170
        $result = curl_exec($curl);
171
        $info = curl_getinfo($curl);
172
        $message = curl_error($curl);
173
        $info['message'] = $message;
174
        curl_close($curl);
175
176
        log_info('http get_info', $info);
177
178
        return [$result, $info];
179
    }
180
181
    /**
182
     * API request processing
183
     *
184
     * @param string  $url
185
     * @param array $data
186
     *
187
     * @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...
188
     */
189
    public function getRequestApi($url, $data = array())
190
    {
191
        if (count($data) > 0) {
192
            $url .=  '?' . http_build_query($data);
193
        }
194
195
        $curl = curl_init($url);
196
197
        // Option array
198
        $options = [
199
            // HEADER
200
            CURLOPT_HTTPGET => true,
201
            CURLOPT_SSL_VERIFYPEER => false,
202
            CURLOPT_RETURNTRANSFER => true,
203
            CURLOPT_FAILONERROR => true,
204
            CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
205
        ];
206
207
        // Set option value
208
        curl_setopt_array($curl, $options);
209
        $result = curl_exec($curl);
210
        $info = curl_getinfo($curl);
211
        $message = curl_error($curl);
212
        $info['message'] = $message;
213
        curl_close($curl);
214
215
        log_info('http get_info', $info);
216
217
        return [$result, $info];
218
    }
219
220
    /**
221
     * Get message
222
     *
223
     * @param $info
224
     *
225
     * @return string
226
     */
227 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...
228
    {
229
        if (!empty($info)) {
230
            $statusCode = $info['http_code'];
231
            $message = $info['message'];
232
            $message = $statusCode.' : '.$message;
233
        } else {
234
            $message = trans('ownerstore.text.error.timeout');
235
        }
236
237
        return $message;
238
    }
239
}
240