-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathManager.php
More file actions
117 lines (99 loc) · 2.58 KB
/
Manager.php
File metadata and controls
117 lines (99 loc) · 2.58 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace Octava\Bundle\JobQueueBundle;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use JMS\JobQueueBundle\Entity\Job;
use JMS\JobQueueBundle\Entity\Repository\JobRepository;
use Octava\Bundle\JobQueueBundle\Model\JobCollection;
/**
* Class Manager
* @package Octava\Bundle\JobQueueBundle
*/
class Manager
{
/**
* @var EntityManager
*/
protected $entityManager;
/**
* @var Config
*/
protected $config;
/**
* @var \ReflectionClass
*/
protected $reflection;
/**
* Manager constructor.
* @param EntityManager $entityManager
* @param Config $config
*/
public function __construct(EntityManager $entityManager, Config $config)
{
$this->entityManager = $entityManager;
$this->config = $config;
}
/**
* @param Job $job
* @return Job[]
*/
public function broadcast(Job $job)
{
$result = [];
foreach ($this->config->getQueues() as $queue) {
$newJob = $this->cloneJob($job, $queue);
$result[] = $newJob;
$this->entityManager->persist($newJob);
}
return $result;
}
/**
* @param Job $job
* @return Job
*/
public function distinct(Job $job)
{
$result = $this->cloneJob($job, $this->config->getDefaultQueue());
$this->entityManager->persist($result);
return $result;
}
public function flush($entity)
{
$this->entityManager->flush($entity);
}
/**
* @return EntityRepository|JobRepository
*/
protected function getRepository()
{
return $this->entityManager->getRepository(Job::class);
}
/**
* @param Job $job
* @param string $queue
* @return Job
*/
protected function cloneJob(Job $job, $queue)
{
$command = $job->getCommand();
$newQueueName = $this->config->buildQueueName($queue, $command);
$result = clone $job;
foreach ($this->getReflection()->getProperties() as $property) {
$property->setAccessible(true);
if ('queue' == $property->getName()) {
$property->setValue($result, $newQueueName);
} else {
$value = $property->getValue($job);
$property->setValue($result, $value);
}
}
return $result;
}
protected function getReflection()
{
if (is_null($this->reflection)) {
$this->reflection = new \ReflectionClass(Job::class);
}
return $this->reflection;
}
}