1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mcorten87\rabbitmq_api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use mcorten87\rabbitmq_api\exceptions\NoMapperForJob; |
7
|
|
|
use mcorten87\rabbitmq_api\exceptions\WrongServiceContainerMappingException; |
8
|
|
|
use mcorten87\rabbitmq_api\jobs\JobBase; |
9
|
|
|
use mcorten87\rabbitmq_api\mappers\BaseMapper; |
10
|
|
|
use mcorten87\rabbitmq_api\objects\JobResult; |
11
|
|
|
use mcorten87\rabbitmq_api\services\JobService; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
|
14
|
|
|
class MqManagementFactory |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** @var MqManagementConfig */ |
18
|
|
|
private $config; |
19
|
|
|
|
20
|
39 |
|
public function getConfig() : MqManagementConfig |
21
|
|
|
{ |
22
|
39 |
|
return $this->config; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ContainerBuilder |
27
|
|
|
*/ |
28
|
|
|
private $container; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
$container = new ContainerBuilder(); |
34
|
|
|
$this->container = $container; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param MqManagementConfig $config |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
public function register(MqManagementConfig $config) |
42
|
|
|
{ |
43
|
|
|
$this->config = $config; |
44
|
|
|
|
45
|
|
|
$this->container->register(Client::class, Client::class); |
46
|
|
|
|
47
|
|
|
$this->container->register(JobService::class, JobService::class) |
48
|
|
|
->addArgument($this) |
49
|
|
|
->addArgument($this->container->get(Client::class)) |
50
|
|
|
; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
protected function registerMapper(BaseMapper $mapper, JobBase $job) |
54
|
|
|
{ |
55
|
1 |
|
$this->container->register( |
56
|
1 |
|
get_class($job), |
57
|
1 |
|
get_class($mapper) |
58
|
|
|
) |
59
|
1 |
|
->addArgument($this->config); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return JobService |
64
|
|
|
* @throws \Exception |
65
|
|
|
*/ |
66
|
39 |
|
public function getJobService() : JobService |
67
|
|
|
{ |
68
|
39 |
|
$jobService = $this->container->get(JobService::class); |
69
|
39 |
|
if (!$jobService instanceof JobService) { |
70
|
|
|
throw WrongServiceContainerMappingException::expectedOtherMapping( |
71
|
|
|
$jobService, |
72
|
|
|
JobService::class |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
39 |
|
return $jobService; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $result |
81
|
|
|
* @return JobResult |
82
|
|
|
*/ |
83
|
|
|
public function getJobResult($result) |
84
|
|
|
{ |
85
|
|
|
return new JobResult($result); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets a mapper for the job, if non found it throws an NoMapperForJob exception |
90
|
|
|
* |
91
|
|
|
* @param JobBase $job |
92
|
|
|
* @return BaseMapper |
93
|
|
|
* @throws NoMapperForJob |
94
|
|
|
* @throws WrongServiceContainerMappingException |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
57 |
|
public function getJobMapper(JobBase $job) : BaseMapper |
98
|
|
|
{ |
99
|
|
|
try { |
100
|
57 |
|
return $this->getJobMapperFromContainer($job); |
101
|
56 |
|
} catch (NoMapperForJob $e) { |
102
|
55 |
|
return $this->getJobMapperFromAutoload($job); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param JobBase $job |
108
|
|
|
* @return BaseMapper |
109
|
|
|
* @throws NoMapperForJob |
110
|
|
|
* @throws WrongServiceContainerMappingException |
111
|
|
|
* @throws \Exception |
112
|
|
|
*/ |
113
|
57 |
|
private function getJobMapperFromContainer(JobBase $job) |
114
|
|
|
{ |
115
|
57 |
|
$class = get_class($job); |
116
|
|
|
|
117
|
57 |
|
if (false === $this->container->has($class)) { |
118
|
55 |
|
throw new NoMapperForJob($job); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
$mapper = $this->container->get($class); |
122
|
2 |
|
if (!$mapper instanceof BaseMapper) { |
123
|
1 |
|
throw WrongServiceContainerMappingException::expectedOtherMapping($job, BaseMapper::class); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return $mapper; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param JobBase $job |
131
|
|
|
* @return mixed |
132
|
|
|
* @throws NoMapperForJob |
133
|
|
|
*/ |
134
|
55 |
|
private function getJobMapperFromAutoload(JobBase $job) |
135
|
|
|
{ |
136
|
55 |
|
$class = get_class($job); |
137
|
55 |
|
$class = str_replace('\\jobs\\', '\\mappers\\', $class); |
138
|
55 |
|
$class .= 'Mapper'; |
139
|
|
|
|
140
|
|
|
try { |
141
|
55 |
|
return new $class($this->config); |
142
|
1 |
|
} catch (\Throwable $e) { |
143
|
1 |
|
throw new NoMapperForJob($job); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|