Conditions | 6 |
Paths | 22 |
Total Lines | 71 |
Code Lines | 48 |
Lines | 13 |
Ratio | 18.31 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
22 | public function perform() { |
||
23 | echo "[-] DataTransferJob starting" . PHP_EOL; |
||
24 | $log = new DeploynautLogFile($this->args['logfile']); |
||
25 | $dataTransfer = DNDataTransfer::get()->byID($this->args['dataTransferID']); |
||
26 | $environment = $dataTransfer->Environment(); |
||
27 | $backupDataTransfer = null; |
||
28 | |||
29 | if(!empty($this->args['backupBeforePush']) && $dataTransfer->Direction == 'push') { |
||
30 | $backupDataTransfer = DNDataTransfer::create(); |
||
31 | $backupDataTransfer->EnvironmentID = $environment->ID; |
||
32 | $backupDataTransfer->Direction = 'get'; |
||
33 | $backupDataTransfer->Mode = $dataTransfer->Mode; |
||
34 | $backupDataTransfer->DataArchiveID = null; |
||
35 | $backupDataTransfer->ResqueToken = $dataTransfer->ResqueToken; |
||
36 | $backupDataTransfer->AuthorID = $dataTransfer->AuthorID; |
||
37 | $backupDataTransfer->write(); |
||
38 | |||
39 | $dataTransfer->BackupDataTransferID = $backupDataTransfer->ID; |
||
40 | $dataTransfer->write(); |
||
41 | } |
||
42 | |||
43 | // This is a bit icky, but there is no easy way of capturing a failed run by using the PHP Resque |
||
44 | try { |
||
45 | // Disallow concurrent jobs (don't rely on queuing implementation to restrict this) |
||
46 | // Only consider data transfers started in the last 30 minutes (older jobs probably got stuck) |
||
47 | $runningTransfers = DNDataTransfer::get() |
||
48 | ->filter(array( |
||
49 | 'EnvironmentID' => $environment->ID, |
||
50 | 'Status' => array('Queued', 'Started'), |
||
51 | 'Created:GreaterThan' => strtotime('-30 minutes') |
||
52 | )) |
||
53 | ->exclude('ID', $dataTransfer->ID); |
||
54 | |||
55 | View Code Duplication | if($runningTransfers->count()) { |
|
|
|||
56 | $runningTransfer = $runningTransfers->first(); |
||
57 | $log->write(sprintf( |
||
58 | '[-] Error: another transfer is in progress (started at %s by %s)', |
||
59 | $runningTransfer->dbObject('Created')->Nice(), |
||
60 | $runningTransfer->Author()->Title |
||
61 | )); |
||
62 | throw new RuntimeException(sprintf( |
||
63 | 'Another transfer is in progress (started at %s by %s)', |
||
64 | $runningTransfer->dbObject('Created')->Nice(), |
||
65 | $runningTransfer->Author()->Title |
||
66 | )); |
||
67 | } |
||
68 | |||
69 | |||
70 | // before we push data to an environment, we'll make a backup first |
||
71 | if($backupDataTransfer) { |
||
72 | $log->write('Backing up existing data'); |
||
73 | $environment->Backend()->dataTransfer( |
||
74 | $backupDataTransfer, |
||
75 | $log |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | $environment->Backend()->dataTransfer( |
||
80 | $dataTransfer, |
||
81 | $log |
||
82 | ); |
||
83 | } catch(RuntimeException $exc) { |
||
84 | $log->write($exc->getMessage()); |
||
85 | |||
86 | echo "[-] DataTransferJob failed" . PHP_EOL; |
||
87 | throw $exc; |
||
88 | } |
||
89 | |||
90 | $this->updateStatus('Finished'); |
||
91 | echo "[-] DataTransferJob finished" . PHP_EOL; |
||
92 | } |
||
93 | |||
119 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.