-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApruveRestClient.php
82 lines (66 loc) · 2.29 KB
/
ApruveRestClient.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
<?php
namespace Oro\Bundle\ApruveBundle\Client;
use Oro\Bundle\ApruveBundle\Client\Exception\UnsupportedMethodException;
use Oro\Bundle\ApruveBundle\Client\Request\ApruveRequestInterface;
use Oro\Bundle\IntegrationBundle\Provider\Rest\Client\RestClientInterface;
class ApruveRestClient implements ApruveRestClientInterface
{
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_DELETE = 'DELETE';
/**
* @var RestClientInterface
*/
private $restClient;
public function __construct(RestClientInterface $restClient)
{
$this->restClient = $restClient;
}
#[\Override]
public function execute(ApruveRequestInterface $apruveRequest)
{
$classMethod = $this->getClientClassMethodByRequest($apruveRequest);
$classMethodArguments = $this->getClientArgumentsByRequest($apruveRequest);
return call_user_func_array([$this->restClient, $classMethod], $classMethodArguments);
}
/**
* @param ApruveRequestInterface $apruveRequest
*
* @return string
*/
private function getClientClassMethodByRequest(ApruveRequestInterface $apruveRequest)
{
$httpMethod = $apruveRequest->getMethod();
$clientMethodsByHttpMethodArray = $this->getClientClassMethodsByHttpMethodsArray();
if (!array_key_exists($httpMethod, $clientMethodsByHttpMethodArray)) {
$msg = sprintf('Rest client does not support method "%s"', $httpMethod);
throw new UnsupportedMethodException($msg);
}
return $clientMethodsByHttpMethodArray[$httpMethod];
}
/**
* @return string[]
*/
private function getClientClassMethodsByHttpMethodsArray()
{
return [
self::METHOD_GET => 'get',
self::METHOD_POST => 'post',
self::METHOD_PUT => 'put',
self::METHOD_DELETE => 'delete',
];
}
/**
* @param ApruveRequestInterface $apruveRequest
*
* @return array
*/
private function getClientArgumentsByRequest(ApruveRequestInterface $apruveRequest)
{
if ($apruveRequest->getMethod() === self::METHOD_DELETE) {
return [$apruveRequest->getUri()];
}
return [$apruveRequest->getUri(), $apruveRequest->getData()];
}
}