1 | <?php |
||
19 | class QueueService |
||
20 | { |
||
21 | /** @var QueueRepository */ |
||
22 | protected $repository; |
||
23 | |||
24 | /** @var EntityFactoryInterface */ |
||
25 | protected $entityFactory; |
||
26 | |||
27 | /** @var EventDispatcherInterface */ |
||
28 | protected $eventDispatcher; |
||
29 | |||
30 | /** @var int */ |
||
31 | protected $maxAttempts; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $queueSuitableStatuses = [ |
||
35 | QueueEntityMappedSuperclass::STATUS_NEW, |
||
36 | QueueEntityMappedSuperclass::STATUS_IN_PROGRESS |
||
37 | ]; |
||
38 | |||
39 | 9 | public function __construct( |
|
40 | QueueRepository $repository, |
||
41 | EntityFactoryInterface $entityFactory, |
||
42 | EventDispatcherInterface $eventDispatcher, |
||
43 | int $maxAttempts |
||
44 | ) { |
||
45 | 9 | $this->repository = $repository; |
|
46 | 9 | $this->entityFactory = $entityFactory; |
|
47 | 9 | $this->eventDispatcher = $eventDispatcher; |
|
48 | 9 | $this->maxAttempts = $maxAttempts; |
|
49 | 9 | } |
|
50 | |||
51 | /** |
||
52 | * @param int $limit |
||
53 | * @param int | null $offset |
||
54 | * |
||
55 | * @throws \Doctrine\ORM\TransactionRequiredException |
||
56 | * |
||
57 | * @return array|QueueEntityInterface[] |
||
58 | */ |
||
59 | 1 | public function getToRestore(int $limit, ?int $offset = null): array |
|
60 | { |
||
61 | 1 | return $this->repository->getToRestore($this->maxAttempts, $limit, $offset); |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param int $id |
||
66 | * |
||
67 | * @throws Exception |
||
68 | * |
||
69 | * @return QueueEntityInterface |
||
70 | */ |
||
71 | 5 | public function getToProcess(int $id): QueueEntityInterface |
|
72 | { |
||
73 | 5 | $queueEntity = $this->repository->find($id); |
|
74 | |||
75 | 5 | if (!($queueEntity instanceof QueueEntityInterface)) { |
|
76 | 1 | throw new UnexpectedValueException(sprintf(ConstantMessage::QUEUE_ENTITY_NOT_FOUND, $id)); |
|
77 | } |
||
78 | |||
79 | 4 | if (!in_array($queueEntity->getStatus(), $this->queueSuitableStatuses, true)) { |
|
80 | 1 | throw new UnexpectedValueException(sprintf( |
|
81 | 1 | ConstantMessage::QUEUE_ENTITY_NOT_FOUND_IN_SUITABLE_STATUS, |
|
82 | 1 | $queueEntity->getName(), |
|
83 | 1 | $queueEntity->getJobName(), |
|
84 | 1 | $queueEntity->getStatusAsString() |
|
85 | )); |
||
86 | } |
||
87 | |||
88 | 3 | $attemptsReached = $queueEntity->isMaxAttemptsReached($this->maxAttempts); |
|
89 | 3 | if ($attemptsReached) { |
|
90 | 1 | $queueEntity->setAttemptsReached(); |
|
91 | } else { |
||
92 | 2 | $queueEntity->setInProgress(); |
|
93 | } |
||
94 | |||
95 | 3 | $this->repository->save($queueEntity); |
|
96 | |||
97 | 3 | if ($attemptsReached) { |
|
98 | 1 | $this->eventDispatcher->dispatch(QueueAttemptsReachedEvent::NAME, new QueueAttemptsReachedEvent($queueEntity)); |
|
99 | |||
100 | 1 | throw new AttemptsReachedException(sprintf(ConstantMessage::QUEUE_ATTEMPTS_REACHED, $queueEntity->getName())); |
|
101 | } |
||
102 | |||
103 | 2 | return $queueEntity; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param int $limit |
||
108 | * @param int|null $offset |
||
109 | * |
||
110 | * @throws \Doctrine\ORM\TransactionRequiredException |
||
111 | * |
||
112 | * @return array|QueueEntityInterface[] |
||
113 | */ |
||
114 | public function getToRepublish(int $limit, ?int $offset = null): array |
||
115 | { |
||
116 | return $this->repository->getToRepublish($limit, $offset); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param QueueInterface $queueable |
||
121 | * |
||
122 | * @throws UnexpectedValueException |
||
123 | * |
||
124 | * @return QueueEntityInterface |
||
125 | */ |
||
126 | 1 | public function createQueue(QueueInterface $queueable): QueueEntityInterface |
|
127 | { |
||
128 | 1 | $queue = $this->entityFactory->createQueue($queueable); |
|
129 | 1 | return $this->save($queue); |
|
130 | } |
||
131 | |||
132 | public function flush(QueueEntityInterface $entity = null): void |
||
133 | { |
||
134 | $this->repository->flush($entity); |
||
135 | } |
||
136 | |||
137 | 2 | public function save(QueueEntityInterface $entity): QueueEntityInterface |
|
143 | |||
144 | 1 | public function isTransactionActive(): bool |
|
148 | |||
149 | public function beginTransaction(): void |
||
153 | |||
154 | public function rollback(): void |
||
158 | |||
159 | public function commit(): void |
||
163 | } |
||
164 |