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 |
||
| 14 | class JobManager extends PriorityJobManager |
||
| 15 | { |
||
| 16 | /** @var AMQPChannel */ |
||
| 17 | protected $channel; |
||
| 18 | |||
| 19 | /** @var AbstractConnection */ |
||
| 20 | protected $connection; |
||
| 21 | protected $queueArgs; |
||
| 22 | protected $exchangeArgs; |
||
| 23 | |||
| 24 | protected $channelSetup = false; |
||
| 25 | |||
| 26 | protected $hostname; |
||
| 27 | protected $pid; |
||
| 28 | |||
| 29 | public function __construct() |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param string $exchange |
||
| 37 | * @param string $type |
||
| 38 | * @param bool $passive |
||
| 39 | * @param bool $durable |
||
| 40 | * @param bool $autoDelete |
||
| 41 | */ |
||
| 42 | public function setExchangeArgs($exchange, $type, $passive, $durable, $autoDelete) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $queue |
||
| 49 | * @param bool $passive |
||
| 50 | * @param bool $durable |
||
| 51 | * @param bool $exclusive |
||
| 52 | * @param bool $autoDelete |
||
| 53 | */ |
||
| 54 | public function setQueueArgs($queue, $passive, $durable, $exclusive, $autoDelete) |
||
| 66 | |||
| 67 | public function setAMQPConnection(AbstractConnection $connection) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return AMQPChannel |
||
| 75 | */ |
||
| 76 | public function getChannel() |
||
| 80 | |||
| 81 | protected function checkChannelArgs() |
||
| 90 | |||
| 91 | protected function performChannelSetup() |
||
| 101 | |||
| 102 | public function setupChannel() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 114 | * |
||
| 115 | * @return \Dtc\QueueBundle\Model\Job |
||
| 116 | * |
||
| 117 | * @throws ClassNotSubclassException |
||
| 118 | */ |
||
| 119 | public function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Attach a unique id to a job since RabbitMQ will not. |
||
| 140 | * |
||
| 141 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 142 | */ |
||
| 143 | protected function setJobId(\Dtc\QueueBundle\Model\Job $job) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Sets the priority of the AMQPMessage. |
||
| 152 | * |
||
| 153 | * @param AMQPMessage $msg |
||
| 154 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 155 | */ |
||
| 156 | protected function setMsgPriority(AMQPMessage $msg, \Dtc\QueueBundle\Model\Job $job) |
||
| 163 | |||
| 164 | protected function calculatePriority($priority) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 176 | * |
||
| 177 | * @throws PriorityException |
||
| 178 | * @throws ClassNotSubclassException |
||
| 179 | */ |
||
| 180 | protected function validateSaveable(\Dtc\QueueBundle\Model\Job $job) |
||
| 190 | |||
| 191 | protected function verifyGetJobArgs($workerName = null, $methodName = null, $prioritize = true) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $workerName |
||
| 200 | */ |
||
| 201 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param bool $expiredJob |
||
| 216 | * @param $runId |
||
| 217 | * |
||
| 218 | * @return Job|null |
||
| 219 | */ |
||
| 220 | protected function findJob(&$expiredJob, $runId) |
||
| 241 | |||
| 242 | // Save History get called upon completion of the job |
||
| 243 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
||
| 253 | |||
| 254 | public function __destruct() |
||
| 260 | } |
||
| 261 |