Failed Conditions
Push — 4.0 ( 35f28e...14891d )
by Ryo
36:06 queued 29:41
created

PluginApiService::getResponseErrorMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

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