Complex classes like JobManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JobManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class JobManager extends DoctrineJobManager |
||
| 17 | { |
||
| 18 | use CommonTrait; |
||
| 19 | protected static $saveInsertCalled = null; |
||
| 20 | protected static $resetInsertCalled = null; |
||
| 21 | |||
| 22 | 3 | public function countJobsByStatus($objectName, $status, $workerName = null, $method = null) |
|
| 23 | { |
||
| 24 | /** @var EntityManager $objectManager */ |
||
| 25 | 3 | $objectManager = $this->getObjectManager(); |
|
| 26 | |||
| 27 | $queryBuilder = $objectManager |
||
| 28 | 3 | ->createQueryBuilder() |
|
| 29 | 3 | ->select('count(a.id)') |
|
| 30 | 3 | ->from($objectName, 'a') |
|
| 31 | 3 | ->where('a.status = :status'); |
|
| 32 | |||
| 33 | 3 | if (null !== $workerName) { |
|
| 34 | 1 | $queryBuilder->andWhere('a.workerName = :workerName') |
|
| 35 | 1 | ->setParameter(':workerName', $workerName); |
|
| 36 | } |
||
| 37 | |||
| 38 | 3 | if (null !== $method) { |
|
| 39 | 1 | $queryBuilder->andWhere('a.method = :method') |
|
| 40 | 1 | ->setParameter(':method', $workerName); |
|
| 41 | } |
||
| 42 | |||
| 43 | 3 | $count = $queryBuilder->setParameter(':status', $status) |
|
| 44 | 3 | ->getQuery()->getSingleScalarResult(); |
|
| 45 | |||
| 46 | 3 | if (!$count) { |
|
| 47 | 1 | return 0; |
|
| 48 | } |
||
| 49 | |||
| 50 | 3 | return $count; |
|
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param string|null $workerName |
||
| 55 | * @param string|null $method |
||
| 56 | * |
||
| 57 | * @return int Count of jobs pruned |
||
| 58 | */ |
||
| 59 | 1 | public function pruneExceptionJobs($workerName = null, $method = null) |
|
| 60 | { |
||
| 61 | /** @var EntityManager $objectManager */ |
||
| 62 | 1 | $objectManager = $this->getObjectManager(); |
|
| 63 | 1 | $queryBuilder = $objectManager->createQueryBuilder()->delete($this->getJobArchiveClass(), 'j'); |
|
| 64 | 1 | $queryBuilder->where('j.status = :status') |
|
| 65 | 1 | ->setParameter(':status', BaseJob::STATUS_EXCEPTION); |
|
| 66 | |||
| 67 | 1 | $this->addWorkerNameCriterion($queryBuilder, $workerName, $method); |
|
| 68 | 1 | $query = $queryBuilder->getQuery(); |
|
| 69 | |||
| 70 | 1 | return intval($query->execute()); |
|
| 71 | } |
||
| 72 | |||
| 73 | 21 | protected function resetSaveOk($function) |
|
| 74 | { |
||
| 75 | 21 | $objectManager = $this->getObjectManager(); |
|
| 76 | 21 | $splObjectHash = spl_object_hash($objectManager); |
|
| 77 | |||
| 78 | 21 | if ('save' === $function) { |
|
| 79 | $compare = static::$resetInsertCalled; |
||
| 80 | } else { |
||
| 81 | 21 | $compare = static::$saveInsertCalled; |
|
| 82 | } |
||
| 83 | |||
| 84 | 21 | if ($splObjectHash === $compare) { |
|
| 85 | // Insert SQL is cached... |
||
| 86 | $msg = "Can't call save and reset within the same process cycle (or using the same EntityManager)"; |
||
| 87 | throw new LogicException($msg); |
||
| 88 | } |
||
| 89 | |||
| 90 | 21 | if ('save' === $function) { |
|
| 91 | static::$saveInsertCalled = spl_object_hash($objectManager); |
||
| 92 | } else { |
||
| 93 | 21 | static::$resetInsertCalled = spl_object_hash($objectManager); |
|
| 94 | } |
||
| 95 | 21 | } |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $workerName |
||
| 99 | * @param string $method |
||
| 100 | */ |
||
| 101 | 13 | protected function addWorkerNameCriterion(QueryBuilder $queryBuilder, $workerName = null, $method = null) |
|
| 102 | { |
||
| 103 | 13 | if (null !== $workerName) { |
|
| 104 | 5 | $queryBuilder->andWhere('j.workerName = :workerName')->setParameter(':workerName', $workerName); |
|
| 105 | } |
||
| 106 | |||
| 107 | 13 | if (null !== $method) { |
|
| 108 | 4 | $queryBuilder->andWhere('j.method = :method')->setParameter(':method', $method); |
|
| 109 | } |
||
| 110 | 13 | } |
|
| 111 | |||
| 112 | 1 | protected function updateExpired($workerName = null, $method = null) |
|
| 113 | { |
||
| 114 | /** @var EntityManager $objectManager */ |
||
| 115 | 1 | $objectManager = $this->getObjectManager(); |
|
| 116 | 1 | $queryBuilder = $objectManager->createQueryBuilder()->update($this->getJobClass(), 'j'); |
|
| 117 | 1 | $queryBuilder->set('j.status', ':newStatus'); |
|
| 118 | 1 | $queryBuilder->where('j.expiresAt <= :expiresAt') |
|
| 119 | 1 | ->setParameter(':expiresAt', Util::getMicrotimeDateTime()); |
|
| 120 | 1 | $queryBuilder->andWhere('j.status = :status') |
|
| 121 | 1 | ->setParameter(':status', BaseJob::STATUS_NEW) |
|
| 122 | 1 | ->setParameter(':newStatus', Job::STATUS_EXPIRED); |
|
| 123 | |||
| 124 | 1 | $this->addWorkerNameCriterion($queryBuilder, $workerName, $method); |
|
| 125 | 1 | $query = $queryBuilder->getQuery(); |
|
| 126 | |||
| 127 | 1 | return intval($query->execute()); |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Removes archived jobs older than $olderThan. |
||
| 132 | * |
||
| 133 | * @param \DateTime $olderThan |
||
| 134 | */ |
||
| 135 | 1 | public function pruneArchivedJobs(\DateTime $olderThan) |
|
| 136 | { |
||
| 137 | 1 | return $this->removeOlderThan( |
|
| 138 | 1 | $this->getJobArchiveClass(), |
|
| 139 | 1 | 'updatedAt', |
|
| 140 | 1 | $olderThan |
|
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | 2 | public function getJobCount($workerName = null, $method = null) |
|
| 145 | { |
||
| 146 | /** @var EntityManager $objectManager */ |
||
| 147 | 2 | $objectManager = $this->getObjectManager(); |
|
| 148 | 2 | $queryBuilder = $objectManager->createQueryBuilder(); |
|
| 149 | |||
| 150 | 2 | $queryBuilder = $queryBuilder->select('count(j)')->from($this->getJobClass(), 'j'); |
|
| 151 | |||
| 152 | 2 | $where = 'where'; |
|
| 153 | 2 | if (null !== $workerName) { |
|
| 154 | if (null !== $method) { |
||
| 155 | $queryBuilder->where($queryBuilder->expr()->andX( |
||
| 156 | $queryBuilder->expr()->eq('j.workerName', ':workerName'), |
||
| 157 | $queryBuilder->expr()->eq('j.method', ':method') |
||
| 158 | )) |
||
| 159 | ->setParameter(':method', $method); |
||
| 160 | } else { |
||
| 161 | $queryBuilder->where('j.workerName = :workerName'); |
||
| 162 | } |
||
| 163 | $queryBuilder->setParameter(':workerName', $workerName); |
||
| 164 | $where = 'andWhere'; |
||
| 165 | 2 | } elseif (null !== $method) { |
|
| 166 | $queryBuilder->where('j.method = :method')->setParameter(':method', $method); |
||
| 167 | $where = 'andWhere'; |
||
| 168 | } |
||
| 169 | |||
| 170 | // Filter |
||
| 171 | $queryBuilder |
||
| 172 | 2 | ->$where($queryBuilder->expr()->orX( |
|
| 173 | 2 | $queryBuilder->expr()->isNull('j.whenUs'), |
|
| 174 | 2 | $queryBuilder->expr()->lte('j.whenUs', ':whenUs') |
|
| 175 | )) |
||
| 176 | 2 | ->andWhere($queryBuilder->expr()->orX( |
|
| 177 | 2 | $queryBuilder->expr()->isNull('j.expiresAt'), |
|
| 178 | 2 | $queryBuilder->expr()->gt('j.expiresAt', ':expiresAt') |
|
| 179 | )) |
||
| 180 | 2 | ->setParameter(':whenUs', Util::getMicrotimeDecimal()) |
|
| 181 | 2 | ->setParameter(':expiresAt', Util::getMicrotimeDateTime()); |
|
| 182 | |||
| 183 | 2 | $query = $queryBuilder->getQuery(); |
|
| 184 | |||
| 185 | 2 | return $query->getSingleScalarResult(); |
|
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get Jobs statuses. |
||
| 190 | */ |
||
| 191 | 3 | public function getStatus() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $entityName |
||
| 214 | */ |
||
| 215 | 3 | protected function getStatusByEntityName($entityName, array &$result) |
|
| 216 | { |
||
| 217 | /** @var EntityManager $objectManager */ |
||
| 218 | 3 | $objectManager = $this->getObjectManager(); |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Get the next job to run (can be filtered by workername and method name). |
||
| 243 | * |
||
| 244 | * @param string $workerName |
||
| 245 | * @param string $methodName |
||
| 246 | * @param bool $prioritize |
||
| 247 | * @param int $runId |
||
| 248 | * |
||
| 249 | * @return Job|null |
||
| 250 | */ |
||
| 251 | 11 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param string|null $workerName |
||
| 275 | * @param string|null $methodName |
||
| 276 | * @param bool $prioritize |
||
| 277 | * |
||
| 278 | * @return QueryBuilder |
||
| 279 | */ |
||
| 280 | 11 | public function getJobQueryBuilder($workerName = null, $methodName = null, $prioritize = true) |
|
| 297 | |||
| 298 | 12 | protected function addStandardPredicate(QueryBuilder $queryBuilder) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param int $runId |
||
| 319 | */ |
||
| 320 | 9 | protected function takeJob($jobId, $runId = null) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Tries to update the nearest job as a batch. |
||
| 350 | * |
||
| 351 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 352 | * |
||
| 353 | * @return null|Job |
||
| 354 | */ |
||
| 355 | 1 | public function updateNearestBatch(\Dtc\QueueBundle\Model\Job $job) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @param int $newPriority |
||
| 388 | * @param string $newWhenUs |
||
| 389 | */ |
||
| 390 | 1 | protected function updateBatchJob(Job $existingJob, $newPriority, $newWhenUs) |
|
| 418 | |||
| 419 | 1 | public function getWorkersAndMethods() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @param string $workerName |
||
| 442 | * @param string $methodName |
||
| 443 | */ |
||
| 444 | 2 | public function countLiveJobs($workerName = null, $methodName = null) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * @param string $workerName |
||
| 458 | * @param string $methodName |
||
| 459 | * @param \Closure $progressCallback |
||
| 460 | */ |
||
| 461 | 1 | public function archiveAllJobs($workerName = null, $methodName = null, $progressCallback) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Move jobs in 'archive' status to the archive table. |
||
| 481 | * |
||
| 482 | * This is a bit of a hack to run a lower level query so as to process the INSERT INTO SELECT |
||
| 483 | * All on the server as "INSERT INTO SELECT" is not supported natively in Doctrine. |
||
| 484 | * |
||
| 485 | * @param string|null $workerName |
||
| 486 | * @param string|null $methodName |
||
| 487 | * @param \Closure $progressCallback |
||
| 488 | */ |
||
| 489 | 1 | protected function runArchive($workerName = null, $methodName = null, $progressCallback) |
|
| 518 | } |
||
| 519 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.