Conditions | 19 |
Paths | 44 |
Total Lines | 89 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
100 | private function closeJobInternal(JobInterface $job, string $finalState, array &$visited = []): void |
||
101 | { |
||
102 | if (\in_array($job, $visited, true)) { |
||
103 | return; |
||
104 | } |
||
105 | $visited[] = $job; |
||
106 | |||
107 | if ($job->isInFinalState()) { |
||
108 | return; |
||
109 | } |
||
110 | |||
111 | if (null !== $this->eventDispatcher && ($job->isRetryJob() || 0 === count($job->getRetryJobs()))) { |
||
112 | $event = new StateChangeEvent($job, $finalState); |
||
113 | $this->eventDispatcher->dispatch(SetonoSyliusSchedulerPluginEvent::JOB_STATE_CHANGED, $event); |
||
114 | $finalState = $event->getNewState(); |
||
115 | } |
||
116 | |||
117 | switch ($finalState) { |
||
118 | case JobInterface::STATE_CANCELED: |
||
119 | $job->setState(JobInterface::STATE_CANCELED); |
||
120 | $this->entityManager->persist($job); |
||
121 | |||
122 | if ($job->isRetryJob()) { |
||
123 | $this->closeJobInternal($job->getOriginalJob(), JobInterface::STATE_CANCELED, $visited); |
||
124 | |||
125 | return; |
||
126 | } |
||
127 | |||
128 | foreach ($this->jobRepository->findIncomingDependencies($job) as $dep) { |
||
129 | $this->closeJobInternal($dep, JobInterface::STATE_CANCELED, $visited); |
||
130 | } |
||
131 | |||
132 | return; |
||
133 | case JobInterface::STATE_FAILED: |
||
134 | case JobInterface::STATE_TERMINATED: |
||
135 | case JobInterface::STATE_INCOMPLETE: |
||
136 | if ($job->isRetryJob()) { |
||
137 | $job->setState($finalState); |
||
138 | $this->entityManager->persist($job); |
||
139 | |||
140 | $this->closeJobInternal($job->getOriginalJob(), $finalState); |
||
141 | |||
142 | return; |
||
143 | } |
||
144 | |||
145 | // The original job has failed, and we are allowed to retry it. |
||
146 | if ($job->isRetryAllowed()) { |
||
147 | $retryJob = $this->jobFactory->createRetryJob($job); |
||
148 | $retryJob->setExecuteAfter( |
||
149 | $this->retryScheduler->scheduleNextRetry($job) |
||
150 | ); |
||
151 | |||
152 | $job->addRetryJob($retryJob); |
||
153 | $this->entityManager->persist($retryJob); |
||
154 | $this->entityManager->persist($job); |
||
155 | |||
156 | return; |
||
157 | } |
||
158 | |||
159 | $job->setState($finalState); |
||
160 | $this->entityManager->persist($job); |
||
161 | |||
162 | // The original job has failed, and no retries are allowed. |
||
163 | /** @var JobInterface $dep */ |
||
164 | foreach ($this->jobRepository->findIncomingDependencies($job) as $dep) { |
||
165 | // This is a safe-guard to avoid blowing up if there is a database inconsistency. |
||
166 | if (!$dep->isPending() && !$dep->isNew()) { |
||
167 | continue; |
||
168 | } |
||
169 | |||
170 | $this->closeJobInternal($dep, JobInterface::STATE_CANCELED, $visited); |
||
171 | } |
||
172 | |||
173 | return; |
||
174 | case JobInterface::STATE_FINISHED: |
||
175 | if ($job->isRetryJob()) { |
||
176 | $job->getOriginalJob()->setState($finalState); |
||
177 | $this->entityManager->persist( |
||
178 | $job->getOriginalJob() |
||
179 | ); |
||
180 | } |
||
181 | $job->setState($finalState); |
||
182 | $this->entityManager->persist($job); |
||
183 | |||
184 | return; |
||
185 | default: |
||
186 | throw new \LogicException(sprintf( |
||
187 | 'Non allowed state "%s" in closeJobInternal().', |
||
188 | $finalState |
||
189 | )); |
||
193 |