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 | 2 | public function __construct() |
|
| 30 | { |
||
| 31 | 2 | $this->hostname = gethostname() ?: ''; |
|
| 32 | 2 | $this->pid = getmypid(); |
|
| 33 | 2 | } |
|
| 34 | |||
| 35 | /** |
||
| 36 | * @param string $exchange |
||
| 37 | * @param string $type |
||
| 38 | * @param bool $passive |
||
| 39 | * @param bool $durable |
||
| 40 | * @param bool $autoDelete |
||
| 41 | */ |
||
| 42 | 1 | public function setExchangeArgs($exchange, $type, $passive, $durable, $autoDelete) |
|
| 43 | { |
||
| 44 | 1 | $this->exchangeArgs = [$exchange, $type, $passive, $durable, $autoDelete]; |
|
| 45 | 1 | } |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $queue |
||
| 49 | * @param bool $passive |
||
| 50 | * @param bool $durable |
||
| 51 | * @param bool $exclusive |
||
| 52 | * @param bool $autoDelete |
||
| 53 | */ |
||
| 54 | 1 | public function setQueueArgs($queue, $passive, $durable, $exclusive, $autoDelete) |
|
| 66 | |||
| 67 | 1 | public function setAMQPConnection(AbstractConnection $connection) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @return AMQPChannel |
||
| 75 | */ |
||
| 76 | public function getChannel() |
||
| 80 | |||
| 81 | 7 | protected function checkChannelArgs() |
|
| 90 | |||
| 91 | 1 | protected function performChannelSetup() |
|
| 101 | |||
| 102 | 7 | 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 | 6 | 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 | 6 | 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 | 6 | protected function setMsgPriority(AMQPMessage $msg, \Dtc\QueueBundle\Model\Job $job) |
|
| 163 | |||
| 164 | 6 | protected function calculatePriority($priority) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 176 | * |
||
| 177 | * @throws PriorityException |
||
| 178 | * @throws ClassNotSubclassException |
||
| 179 | */ |
||
| 180 | 6 | protected function validateSaveable(\Dtc\QueueBundle\Model\Job $job) |
|
| 190 | |||
| 191 | 7 | protected function verifyGetJobArgs($workerName = null, $methodName = null, $prioritize = true) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $workerName |
||
| 200 | */ |
||
| 201 | 7 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
| 202 | { |
||
| 203 | 7 | $this->verifyGetJobArgs($workerName, $methodName, $prioritize); |
|
| 204 | 6 | $this->setupChannel(); |
|
| 205 | |||
| 206 | do { |
||
| 207 | 6 | $expiredJob = false; |
|
| 208 | 6 | $job = $this->findJob($expiredJob, $runId); |
|
| 209 | 6 | } while ($expiredJob); |
|
| 210 | |||
| 211 | 6 | return $job; |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param bool $expiredJob |
||
| 216 | * @param $runId |
||
| 217 | * |
||
| 218 | * @return Job|null |
||
| 219 | */ |
||
| 220 | 6 | protected function findJob(&$expiredJob, $runId) |
|
| 241 | |||
| 242 | // Save History get called upon completion of the job |
||
| 243 | 1 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
|
| 244 | { |
||
| 245 | 1 | if (!$job instanceof Job) { |
|
| 246 | 1 | throw new ClassNotSubclassException("Expected \Dtc\QueueBundle\RabbitMQ\Job, got ".get_class($job)); |
|
| 247 | } |
||
| 248 | 1 | $deliveryTag = $job->getDeliveryTag(); |
|
| 249 | 1 | $this->channel->basic_ack($deliveryTag); |
|
| 250 | |||
| 251 | 1 | return; |
|
| 252 | } |
||
| 253 | |||
| 254 | 3 | public function __destruct() |
|
| 260 | } |
||
| 261 |