1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mcorten87\rabbitmq_api\services; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
use GuzzleHttp\Psr7\Response; |
8
|
|
|
use mcorten87\rabbitmq_api\jobs\JobBase; |
9
|
|
|
use mcorten87\rabbitmq_api\MqManagementFactory; |
10
|
|
|
use mcorten87\rabbitmq_api\objects\JobResult; |
11
|
|
|
|
12
|
|
|
class JobService |
13
|
|
|
{ |
14
|
|
|
/** @var MqManagementFactory */ |
15
|
|
|
private $factory; |
16
|
|
|
|
17
|
|
|
/** @var Client */ |
18
|
|
|
private $client; |
19
|
|
|
|
20
|
|
|
public function __construct(MqManagementFactory $factory, Client $client) |
21
|
|
|
{ |
22
|
|
|
$this->factory = $factory; |
23
|
|
|
$this->client = $client; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param JobBase $job |
28
|
|
|
* @return JobResult |
29
|
|
|
* @throws \mcorten87\rabbitmq_api\exceptions\NoMapperForJob |
30
|
|
|
*/ |
31
|
|
|
public function execute(JobBase $job) : JobResult { |
32
|
|
|
$mapper = $this->factory->getJobMapper($job); |
33
|
|
|
|
34
|
|
|
$mapResult = $mapper->map($job); |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
$res = $this->client->request($mapResult->getMethod(), $this->factory->getConfig()->getUrl().$mapResult->getUrl(), $mapResult->getConfig()); |
38
|
|
|
} catch (ClientException $e) { |
39
|
|
|
$data = json_decode($e->getResponse()->getBody()); |
40
|
|
|
|
41
|
|
|
// find out what kind of error happend and give some extra help |
42
|
|
|
if (strpos($data->reason,'inequivalent arg \'durable\'') !== false) { |
43
|
|
|
$data->cause = 'Queue already exists with different durable stat, delete the queue first'; |
44
|
|
|
$res = new Response($e->getCode(),$e->getResponse()->getHeaders(), json_encode($data)); |
45
|
|
|
} elseif ($data->error === 'Object Not Found') { |
46
|
|
|
$res = new Response($e->getCode(),$e->getResponse()->getHeaders(), json_encode($data)); |
47
|
|
|
} else { |
48
|
|
|
throw $e; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
$jobResult = $this->factory->getJobResult($res); |
53
|
|
|
return $jobResult; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|