-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathMediaService.php
95 lines (79 loc) · 3.58 KB
/
MediaService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php declare(strict_types=1);
namespace Vin\ShopwareSdk\Service;
use GuzzleHttp\Exception\BadResponseException;
use Vin\ShopwareSdk\Exception\ShopwareResponseException;
/**
* Gateway for the API end point "media"
*
* Class AdminActionService
* @package Vin\ShopwareSdk\Service
*/
class MediaService extends ApiService
{
/**
* @param string|resource|array $data
*/
public function uploadMediaById(string $mediaId, string $mimeType, $data, string $extension, ?string $fileName = null): ApiResponse
{
$fileName = $fileName ?? $mediaId;
$params = [
'fileName' => $fileName,
'extension' => $extension,
];
$path = sprintf('/api/_action/media/%s/upload', $mediaId);
try {
$response = $this->httpClient->post($this->buildQueryUrl($path, $params), [
'headers' => $this->getBasicHeaders([
'Content-Type' => $mimeType
]),
'body' => is_array($data) ? json_encode($data) : $data,
]);
$contents = self::handleResponse($response->getBody()->getContents(), $response->getHeaders());
return new ApiResponse($contents, $response->getHeaders(), $response->getStatusCode());
} catch (BadResponseException $exception) {
$message = $exception->getResponse()->getBody()->getContents();
throw new ShopwareResponseException($message, $exception->getResponse()->getStatusCode(), $exception);
}
}
public function uploadMediaFromUrl(string $mediaId, string $url, string $extension, ?string $fileName = null): ApiResponse
{
$data = [
'url' => $url,
];
return $this->uploadMediaById($mediaId, 'application/json', $data, $extension, $fileName);
}
public function renameMedia(string $mediaId, string $fileName): ApiResponse
{
$path = sprintf('/api/_action/media/%s/rename', $mediaId);
try {
$response = $this->httpClient->post($this->getFullUrl($path), [
'headers' => $this->getBasicHeaders(),
'body' => json_encode([ 'fileName' => $fileName ]),
]);
$contents = self::handleResponse($response->getBody()->getContents(), $response->getHeaders());
return new ApiResponse($contents, $response->getHeaders(), $response->getStatusCode());
} catch (BadResponseException $exception) {
$message = $exception->getResponse()->getBody()->getContents();
throw new ShopwareResponseException($message, $exception->getResponse()->getStatusCode(), $exception);
}
}
public function provideName(string $fileName, string $extension, ?string $mediaId = null): string
{
$path = '/api/_action/media/provide-name';
$params = array_filter([
'extension' => $extension,
'fileName' => $fileName,
'mediaId' => $mediaId
]);
try {
$response = $this->httpClient->get($this->buildQueryUrl($path, $params), [
'headers' => $this->getBasicHeaders(),
]);
$contents = self::handleResponse($response->getBody()->getContents(), $response->getHeaders());
return (new ApiResponse($contents, $response->getHeaders(), $response->getStatusCode()))->getContents()['fileName'];
} catch (BadResponseException $exception) {
$message = $exception->getResponse()->getBody()->getContents();
throw new ShopwareResponseException($message, $exception->getResponse()->getStatusCode(), $exception);
}
}
}