Passed
Push — master ( 495efb...ace576 )
by Brian
06:14 queued 01:46
created

ActionMigrator::migrate()   C

Complexity

Conditions 12
Paths 156

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 38
c 1
b 0
f 0
nc 156
nop 1
dl 0
loc 62
rs 6.4999

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
4
namespace Action_Scheduler\Migration;
5
6
/**
7
 * Class ActionMigrator
8
 *
9
 * @package Action_Scheduler\Migration
10
 *
11
 * @since 3.0.0
12
 *
13
 * @codeCoverageIgnore
14
 */
15
class ActionMigrator {
16
	/** var ActionScheduler_Store */
17
	private $source;
18
19
	/** var ActionScheduler_Store */
20
	private $destination;
21
22
	/** var LogMigrator */
23
	private $log_migrator;
24
25
	/**
26
	 * ActionMigrator constructor.
27
	 *
28
	 * @param ActionScheduler_Store $source_store Source store object.
0 ignored issues
show
Bug introduced by
The type Action_Scheduler\Migration\ActionScheduler_Store was not found. Did you mean ActionScheduler_Store? If so, make sure to prefix the type with \.
Loading history...
29
	 * @param ActionScheduler_Store $destination_store Destination store object.
30
	 * @param LogMigrator           $log_migrator Log migrator object.
31
	 */
32
	public function __construct( \ActionScheduler_Store $source_store, \ActionScheduler_Store $destination_store, LogMigrator $log_migrator ) {
33
		$this->source       = $source_store;
34
		$this->destination  = $destination_store;
35
		$this->log_migrator = $log_migrator;
36
	}
37
38
	/**
39
	 * Migrate an action.
40
	 *
41
	 * @param int $source_action_id Action ID.
42
	 *
43
	 * @return int 0|new action ID
44
	 */
45
	public function migrate( $source_action_id ) {
46
		try {
47
			$action = $this->source->fetch_action( $source_action_id );
48
			$status = $this->source->get_status( $source_action_id );
49
		} catch ( \Exception $e ) {
50
			$action = null;
51
			$status = '';
52
		}
53
54
		if ( is_null( $action ) || empty( $status ) || ! $action->get_schedule()->get_date() ) {
0 ignored issues
show
Bug introduced by
The method get_date() does not exist on ActionScheduler_Schedule. It seems like you code against a sub-type of said class. However, the method does not exist in ActionScheduler_Schedule_Deprecated. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
		if ( is_null( $action ) || empty( $status ) || ! $action->get_schedule()->/** @scrutinizer ignore-call */ get_date() ) {
Loading history...
55
			// null action or empty status means the fetch operation failed or the action didn't exist
56
			// null schedule means it's missing vital data
57
			// delete it and move on
58
			try {
59
				$this->source->delete_action( $source_action_id );
60
			} catch ( \Exception $e ) {
61
				// nothing to do, it didn't exist in the first place
62
			}
63
			do_action( 'action_scheduler/no_action_to_migrate', $source_action_id, $this->source, $this->destination );
64
65
			return 0;
66
		}
67
68
		try {
69
70
			// Make sure the last attempt date is set correctly for completed and failed actions
71
			$last_attempt_date = ( $status !== \ActionScheduler_Store::STATUS_PENDING ) ? $this->source->get_date( $source_action_id ) : null;
72
73
			$destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date );
0 ignored issues
show
Unused Code introduced by
The call to ActionScheduler_Store::save_action() has too many arguments starting with $last_attempt_date. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
			/** @scrutinizer ignore-call */ 
74
   $destination_action_id = $this->destination->save_action( $action, null, $last_attempt_date );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
74
		} catch ( \Exception $e ) {
75
			do_action( 'action_scheduler/migrate_action_failed', $source_action_id, $this->source, $this->destination );
76
77
			return 0; // could not save the action in the new store
78
		}
79
80
		try {
81
			switch ( $status ) {
82
				case \ActionScheduler_Store::STATUS_FAILED :
83
					$this->destination->mark_failure( $destination_action_id );
84
					break;
85
				case \ActionScheduler_Store::STATUS_CANCELED :
86
					$this->destination->cancel_action( $destination_action_id );
87
					break;
88
			}
89
90
			$this->log_migrator->migrate( $source_action_id, $destination_action_id );
0 ignored issues
show
Bug introduced by
$destination_action_id of type string is incompatible with the type integer expected by parameter $destination_action_id of Action_Scheduler\Migration\LogMigrator::migrate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

90
			$this->log_migrator->migrate( $source_action_id, /** @scrutinizer ignore-type */ $destination_action_id );
Loading history...
91
			$this->source->delete_action( $source_action_id );
92
93
			$test_action = $this->source->fetch_action( $source_action_id );
94
			if ( ! is_a( $test_action, 'ActionScheduler_NullAction' ) ) {
95
				throw new \RuntimeException( sprintf( __( 'Unable to remove source migrated action %s', 'action-scheduler' ), $source_action_id ) );
96
			}
97
			do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
98
99
			return $destination_action_id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $destination_action_id returns the type string which is incompatible with the documented return type integer.
Loading history...
100
		} catch ( \Exception $e ) {
101
			// could not delete from the old store
102
			$this->source->mark_migrated( $source_action_id );
103
			do_action( 'action_scheduler/migrate_action_incomplete', $source_action_id, $destination_action_id, $this->source, $this->destination );
104
			do_action( 'action_scheduler/migrated_action', $source_action_id, $destination_action_id, $this->source, $this->destination );
105
106
			return $destination_action_id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $destination_action_id returns the type string which is incompatible with the documented return type integer.
Loading history...
107
		}
108
	}
109
}
110