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 |
||
10 | class JobManager extends PriorityJobManager |
||
11 | { |
||
12 | /** @var AMQPChannel */ |
||
13 | protected $channel; |
||
14 | |||
15 | /** @var AbstractConnection */ |
||
16 | protected $connection; |
||
17 | protected $queueArgs; |
||
18 | protected $exchangeArgs; |
||
19 | |||
20 | protected $channelSetup = false; |
||
21 | |||
22 | protected $hostname; |
||
23 | protected $pid; |
||
24 | |||
25 | 2 | public function __construct() |
|
30 | |||
31 | /** |
||
32 | * @param string $exchange |
||
33 | * @param string $type |
||
34 | * @param bool $passive |
||
35 | * @param bool $durable |
||
36 | * @param bool $autoDelete |
||
37 | */ |
||
38 | 1 | public function setExchangeArgs($exchange, $type, $passive, $durable, $autoDelete) |
|
42 | |||
43 | /** |
||
44 | * @param string $queue |
||
45 | * @param bool $passive |
||
46 | * @param bool $durable |
||
47 | * @param bool $exclusive |
||
48 | * @param bool $autoDelete |
||
49 | */ |
||
50 | 1 | public function setQueueArgs($queue, $passive, $durable, $exclusive, $autoDelete) |
|
51 | { |
||
52 | 1 | $arguments = [$queue, $passive, $durable, $exclusive, $autoDelete]; |
|
53 | |||
54 | 1 | $this->queueArgs = $arguments; |
|
55 | 1 | if (!ctype_digit(strval($this->maxPriority))) { |
|
56 | 1 | throw new \Exception('Max Priority ('.$this->maxPriority.') needs to be a non-negative integer'); |
|
57 | } |
||
58 | 1 | if (strval(intval($this->maxPriority)) !== strval($this->maxPriority)) { |
|
59 | throw new \Exception('Priority is higher than '.PHP_INT_MAX); |
||
60 | } |
||
61 | 1 | } |
|
62 | |||
63 | 1 | public function setAMQPConnection(AbstractConnection $connection) |
|
68 | |||
69 | /** |
||
70 | * @return AMQPChannel |
||
71 | */ |
||
72 | public function getChannel() |
||
76 | |||
77 | 7 | protected function checkChannelArgs() |
|
86 | |||
87 | 1 | protected function performChannelSetup() |
|
97 | |||
98 | 7 | public function setupChannel() |
|
107 | |||
108 | /** |
||
109 | * @param \Dtc\QueueBundle\Model\Job $job |
||
110 | * |
||
111 | * @return \Dtc\QueueBundle\Model\Job |
||
112 | * |
||
113 | * @throws \Exception |
||
114 | */ |
||
115 | 6 | public function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
133 | |||
134 | /** |
||
135 | * Attach a unique id to a job since RabbitMQ will not. |
||
136 | * |
||
137 | * @param \Dtc\QueueBundle\Model\Job $job |
||
138 | */ |
||
139 | 6 | protected function setJobId(\Dtc\QueueBundle\Model\Job $job) |
|
145 | |||
146 | /** |
||
147 | * Sets the priority of the AMQPMessage. |
||
148 | * |
||
149 | * @param AMQPMessage $msg |
||
150 | * @param \Dtc\QueueBundle\Model\Job $job |
||
151 | */ |
||
152 | 6 | protected function setMsgPriority(AMQPMessage $msg, \Dtc\QueueBundle\Model\Job $job) |
|
159 | |||
160 | 6 | protected function calculatePriority($priority) |
|
169 | |||
170 | /** |
||
171 | * @param \Dtc\QueueBundle\Model\Job $job |
||
172 | * |
||
173 | * @throws \Exception |
||
174 | */ |
||
175 | 6 | protected function validateSaveable(\Dtc\QueueBundle\Model\Job $job) |
|
185 | |||
186 | 7 | protected function verifyGetJobArgs($workerName = null, $methodName = null, $prioritize = true) |
|
192 | |||
193 | /** |
||
194 | * @param string $workerName |
||
195 | */ |
||
196 | 7 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
208 | |||
209 | /** |
||
210 | * @param bool $expiredJob |
||
211 | * @param $runId |
||
212 | * |
||
213 | * @return Job|null |
||
214 | */ |
||
215 | 6 | protected function findJob(&$expiredJob, $runId) |
|
236 | |||
237 | // Save History get called upon completion of the job |
||
238 | 1 | public function saveHistory(\Dtc\QueueBundle\Model\Job $job) |
|
248 | |||
249 | 3 | public function __destruct() |
|
255 | } |
||
256 |