Failed Conditions
Push — pr/3408 ( 278c84 )
by Kiyotaka
06:25
created

PluginApiService::__construct()   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
/*
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\EccubeConfig;
17
18
class PluginApiService
19
{
20
    /**
21
     * Url for Api
22
     *
23
     * @var string
24
     */
25
    private $apiUrl;
26
27
    /**
28
     * @var EccubeConfig
29
     */
30
    private $eccubeConfig;
31
32
    /**
33
     * PluginApiService constructor.
34
     *
35
     * @param EccubeConfig $eccubeConfig
36
     */
37
    public function __construct(EccubeConfig $eccubeConfig)
38
    {
39
        $this->eccubeConfig = $eccubeConfig;
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45
    public function getApiUrl()
46
    {
47
        if (empty($this->apiUrl)) {
48
            return $this->eccubeConfig->get('eccube_package_repo_url');
49
        }
50
51
        return $this->apiUrl;
52
    }
53
54
    /**
55
     * @param mixed $apiUrl
56
     */
57
    public function setApiUrl($apiUrl)
58
    {
59
        $this->apiUrl = $apiUrl;
60
    }
61
62
    public function getCategory()
63
    {
64
        $urlCategory = $this->getApiUrl().'/category';
65
66
        return $this->getRequestApi($urlCategory);
67
    }
68
69
    public function getPlugins($data = [])
70
    {
71
        $url = $this->getApiUrl().'/plugins';
72
        $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...
73
        $params['price_type'] = empty($data['price_type']) ? 'all' : $data['price_type'];
74
        $params['keyword'] = $data['keyword'];
75
        $params['sort'] = $data['sort'];
76
        $params['page'] = (isset($data['page_no']) && !empty($data['page_no'])) ? $data['page_no'] : 1;
77
        $params['per_page'] = (isset($data['page_count']) && !empty($data['page_count'])) ? $data['page_count'] : $this->eccubeConfig->get('eccube_default_page_count');
78
79
        return $this->getRequestApi($url, $params);
80
    }
81
82
    /**
83
     * API request processing
84
     *
85
     * @param string  $url
86
     * @param array $data
87
     *
88
     * @return array
89
     */
90
    public function getRequestApi($url, $data = [])
91
    {
92
        if (count($data) > 0) {
93
            $url .= '?'.http_build_query($data);
94
        }
95
96
        $curl = curl_init($url);
97
98
        // Option array
99
        $options = [
100
            // HEADER
101
            CURLOPT_HTTPGET => true,
102
            CURLOPT_SSL_VERIFYPEER => false,
103
            CURLOPT_RETURNTRANSFER => true,
104
            CURLOPT_FAILONERROR => true,
105
            CURLOPT_CAINFO => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(),
106
        ];
107
108
        // Set option value
109
        curl_setopt_array($curl, $options);
110
        $result = curl_exec($curl);
111
        $info = curl_getinfo($curl);
112
        $message = curl_error($curl);
113
        $info['message'] = $message;
114
        curl_close($curl);
115
116
        log_info('http get_info', $info);
117
118
        return [$result, $info];
119
    }
120
121
    /**
122
     * Get message
123
     *
124
     * @param $info
125
     *
126
     * @return string
127
     */
128 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...
129
    {
130
        if (!empty($info)) {
131
            $statusCode = $info['http_code'];
132
            $message = $info['message'];
133
            $message = $statusCode.' : '.$message;
134
        } else {
135
            $message = trans('ownerstore.text.error.timeout');
136
        }
137
138
        return $message;
139
    }
140
}
141