Completed
Push — master ( e33cc1...159cde )
by Vragov
15:48 queued 07:23
created

RequestService::postMultipart()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 6
nop 3
1
<?php
2
3
namespace OmnideskBundle\Service;
4
5
use GuzzleHttp\Client;
6
7
/**
8
 * Class RequestService
9
 * @package OmnideskBundle\Service
10
 */
11
class RequestService
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $config;
17
18
    /**
19
     * @var Client
20
     */
21
    protected $client;
22
23
    /**
24
     * RequestService constructor.
25
     * @param array  $config
26
     * @param Client $client
27
     */
28
    public function __construct(array $config, Client $client)
29
    {
30
        $this->config = $config;
31
        $this->client = $client;
32
    }
33
34
    /**
35
     * @param string $url
36
     * @param array  $params
37
     * @return array
38
     */
39 View Code Duplication
    public function post($url, array $params = [])
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...
40
    {
41
        $response = $this->client->post($this->getUrl($url), [
42
            'headers' => $this->getHeaders(),
43
            'auth' => $this->getAuth(),
44
            'query' => $params,
45
        ]);
46
47
        return json_decode((string) $response->getBody(), true);
48
    }
49
50
    /**
51
     * @param string $url
52
     * @param string $name
53
     * @param array  $params
54
     * @return array
55
     */
56
    public function postMultipart($url, $name, array $params = [])
57
    {
58
        $multipart = [];
59
60
        foreach ($params as $key => $param) {
61
            if ($key !== 'attachments') {
62
                $multipart[] = [
63
                    'name' => "{$name}[{$key}]",
64
                    'contents' => $param,
65
                ];
66
            }
67
        }
68
69
        if (isset($params['attachments']) && $attachments = (array) $params['attachments']) {
70
            foreach ($attachments as $key => $attachment) {
71
                $multipart[] = [
72
                    'name' => "{$name}[attachments][{$key}]",
73
                    'contents' => fopen($attachment, 'rb'),
74
                ];
75
            }
76
        }
77
78
        $response = $this->client->post($this->getUrl($url), [
79
            'auth' => $this->getAuth(),
80
            'multipart' => $multipart,
81
        ]);
82
83
        return json_decode((string) $response->getBody(), true);
84
    }
85
86
    /**
87
     * @param string $url
88
     * @param array  $params
89
     * @return array
90
     */
91 View Code Duplication
    public function get($url, array $params = [])
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...
92
    {
93
        $response = $this->client->get($this->getUrl($url), [
94
            'headers' => $this->getHeaders(),
95
            'auth' => $this->getAuth(),
96
            'query' => $params,
97
        ]);
98
99
        return json_decode((string) $response->getBody(), true);
100
    }
101
102
    /**
103
     * @param string $url
104
     * @param array  $params
105
     * @return array
106
     */
107 View Code Duplication
    public function put($url, array $params = [])
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...
108
    {
109
        $response = $this->client->put($this->getUrl($url), [
110
            'headers' => $this->getHeaders(),
111
            'auth' => $this->getAuth(),
112
            'json' => $params,
113
        ]);
114
115
        return json_decode((string) $response->getBody(), true);
116
    }
117
118
    /**
119
     * @param string $url
120
     * @return int
121
     */
122
    public function delete($url)
123
    {
124
        $response = $this->client->delete($this->getUrl($url), [
125
            'headers' => $this->getHeaders(),
126
            'auth' => $this->getAuth(),
127
        ]);
128
129
        return $response->getStatusCode();
130
    }
131
132
    /**
133
     * @param string $url
134
     * @return string
135
     */
136
    protected function getUrl($url)
137
    {
138
        return sprintf('https://%s.omnidesk.ru/api/%s.json', $this->config['domain'], $url);
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    protected function getHeaders()
145
    {
146
        return [
147
            'Content-Type' => 'application/json',
148
            'Content-Length' => 0,
149
        ];
150
    }
151
152
    /**
153
     * @return array
154
     */
155
    protected function getAuth()
156
    {
157
        return [
158
            $this->config['email'],
159
            $this->config['key'],
160
        ];
161
    }
162
}
163