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 |
||
| 17 | class JobManager extends PriorityJobManager |
||
| 18 | { |
||
| 19 | /** @var AMQPChannel */ |
||
| 20 | protected $channel; |
||
| 21 | |||
| 22 | /** @var AbstractConnection */ |
||
| 23 | protected $connection; |
||
| 24 | protected $queueArgs; |
||
| 25 | protected $exchangeArgs; |
||
| 26 | |||
| 27 | protected $channelSetup = false; |
||
| 28 | |||
| 29 | protected $hostname; |
||
| 30 | protected $pid; |
||
| 31 | |||
| 32 | 2 | public function __construct(RunManager $runManager, JobTimingManager $jobTimingManager, $jobClass) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $exchange |
||
| 41 | * @param string $type |
||
| 42 | * @param bool $passive |
||
| 43 | * @param bool $durable |
||
| 44 | * @param bool $autoDelete |
||
| 45 | */ |
||
| 46 | 1 | public function setExchangeArgs($exchange, $type, $passive, $durable, $autoDelete) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $queue |
||
| 53 | * @param bool $passive |
||
| 54 | * @param bool $durable |
||
| 55 | * @param bool $exclusive |
||
| 56 | * @param bool $autoDelete |
||
| 57 | */ |
||
| 58 | 1 | public function setQueueArgs($queue, $passive, $durable, $exclusive, $autoDelete) |
|
| 70 | |||
| 71 | 1 | public function setAMQPConnection(AbstractConnection $connection) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @return AMQPChannel |
||
| 79 | */ |
||
| 80 | public function getChannel() |
||
| 84 | |||
| 85 | 7 | protected function checkChannelArgs() |
|
| 94 | |||
| 95 | 1 | protected function performChannelSetup() |
|
| 105 | |||
| 106 | 7 | public function setupChannel() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 118 | * |
||
| 119 | * @return \Dtc\QueueBundle\Model\Job |
||
| 120 | * |
||
| 121 | * @throws ClassNotSubclassException |
||
| 122 | */ |
||
| 123 | 6 | public function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Attach a unique id to a job since RabbitMQ will not. |
||
| 144 | * |
||
| 145 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 146 | */ |
||
| 147 | 6 | protected function setJobId(\Dtc\QueueBundle\Model\Job $job) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Sets the priority of the AMQPMessage. |
||
| 156 | * |
||
| 157 | * @param AMQPMessage $msg |
||
| 158 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 159 | */ |
||
| 160 | 6 | protected function setMsgPriority(AMQPMessage $msg, \Dtc\QueueBundle\Model\Job $job) |
|
| 167 | |||
| 168 | 6 | protected function calculatePriority($priority) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 180 | * |
||
| 181 | * @throws PriorityException |
||
| 182 | * @throws ClassNotSubclassException |
||
| 183 | */ |
||
| 184 | 6 | protected function validateSaveable(\Dtc\QueueBundle\Model\Job $job) |
|
| 194 | |||
| 195 | 7 | protected function verifyGetJobArgs($workerName = null, $methodName = null, $prioritize = true) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $workerName |
||
| 204 | */ |
||
| 205 | 7 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param bool $expiredJob |
||
| 220 | * @param $runId |
||
| 221 | * |
||
| 222 | * @return Job|null |
||
| 223 | */ |
||
| 224 | 6 | protected function findJob(&$expiredJob, $runId) |
|
| 246 | |||
| 247 | // Save History get called upon completion of the job |
||
| 248 | 1 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
|
| 258 | |||
| 259 | 3 | public function __destruct() |
|
| 265 | } |
||
| 266 |