Completed
Push — fix/4.4.1-typos ( 3db521 )
by
unknown
25s
created

VideoPress_CLI   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 162
Duplicated Lines 26.54 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 0
cbo 1
dl 43
loc 162
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A import() 5 9 3
A cleanup_videos() 0 5 1
B list_crons() 0 29 6
A activate_cron() 16 16 3
A deactivate_cron() 16 16 3
A cron_status() 6 21 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
if ( defined( 'WP_CLI' ) && WP_CLI ) {
4
5
	/**
6
	 * VideoPress command line utilities.
7
	 */
8
	class VideoPress_CLI extends WP_CLI_Command {
9
		/**
10
		 * Import a VideoPress Video
11
		 *
12
		 * ## OPTIONS
13
		 *
14
		 * <guid>: Import the video with the specified guid
15
		 *
16
		 * ## EXAMPLES
17
		 *
18
		 * wp videopress import kUJmAcSf
19
		 *
20
		 */
21
		public function import( $args ) {
22
			$guid = $args[0];
23
			$attachment_id = create_local_media_library_for_videopress_guid( $guid );
24 View Code Duplication
			if ( $attachment_id && ! is_wp_error( $attachment_id ) ) {
25
				WP_CLI::success( sprintf( __( 'The video has been imported as Attachment ID %d', 'jetpack' ), $attachment_id ) );
26
			} else {
27
				WP_CLI::error( __( 'An error has been encountered.', 'jetpack' ) );
28
			}
29
		}
30
31
		/**
32
		 * Manually runs the job to cleanup videos from the media library that failed during the upload process.
33
		 *
34
		 * ## EXAMPLES
35
		 *
36
		 * wp videopress cleanup_videos
37
		 */
38
		public function cleanup_videos() {
39
			$num_cleaned = videopress_cleanup_media_library();
40
41
			WP_CLI::success( sprintf( __( 'Cleaned up a total of %d videos.', 'jetpack' ), $num_cleaned ) );
42
		}
43
44
		/**
45
		 * List out all of the crons that can be run.
46
		 *
47
		 * ## EXAMPLES
48
		 *
49
		 * wp videopress list_crons
50
		 */
51
		public function list_crons() {
52
53
			$scheduler = VideoPress_Scheduler::init();
54
			$crons = $scheduler->get_crons();
55
56
			$schedules = wp_get_schedules();
57
58
59
			if ( count( $crons ) === 0 ) {
60
				WP_CLI::success( __( 'Found no available cron jobs.', 'jetpack' ) );
61
62
			} elseif ( count( $crons ) === 1 ) {
63
				WP_CLI::success( __( 'Found 1 available cron job.', 'jetpack' ) );
64
65
			} else {
66
				WP_CLI::success( sprintf( __( 'Found %d available cron jobs.', 'jetpack' ), count( $crons ) ) );
67
			}
68
69
			foreach ( $crons as $cron_name => $cron ) {
70
				$interval = isset( $schedules[ $cron['interval'] ]['display'] ) ? $schedules[ $cron['interval'] ]['display'] : $cron['interval'];
71
				$runs_next = $scheduler->check_cron( $cron_name );
72
				$status = $runs_next ? sprintf( 'Scheduled - Runs Next at %s GMT', gmdate( 'Y-m-d H:i:s', $runs_next ) ) : 'Not Scheduled';
73
74
				WP_CLI::log( 'Name: ' . $cron_name );
75
				WP_CLI::log( 'Method: ' . $cron['method'] );
76
				WP_CLI::log( 'Interval: ' . $interval );
77
				WP_CLI::log( 'Status: ' . $status );
78
			}
79
		}
80
81
		/**
82
		 * Checks for the current status of a cron job.
83
		 *
84
		 * ## OPTIONS
85
		 *
86
		 * <cron_name>: The name of the cron job to check
87
		 *
88
		 * ## EXAMPLES
89
		 *
90
		 * wp videopress cron_status cleanup
91
		 */
92
		public function cron_status( $args ) {
93
94
			if ( ! isset( $args[0] ) ) {
95
				return WP_CLI::error( __( 'You need to provide the name of the cronjob to schedule.', 'jetpack' ) );
96
			}
97
98
			$scheduler = VideoPress_Scheduler::init();
99
100
			if ( ! $scheduler->is_cron_valid( $args[0] ) ) {
101
				return WP_CLI::error( sprintf( __( 'There is no cron named %s.', 'jetpack' ), $args[0] ) );
102
			}
103
104
			$time = $scheduler->check_cron( $args[0] );
105
106 View Code Duplication
			if ( ! $time ) {
107
				WP_CLI::success( __( 'The cron is not scheduled to run.', 'jetpack' ) );
108
109
			} else {
110
				WP_CLI::success( sprintf( __( 'Cron will run at: %s GMT' ), 'jetpack' ), gmdate( 'Y-m-d H:i:s', $time ) );
111
			}
112
		}
113
114
		/**
115
		 * Actives the given cron job
116
		 *
117
		 * ## OPTIONS
118
		 *
119
		 * <cron_name>: The name of the cron job to check
120
		 *
121
		 * ## EXAMPLES
122
		 *
123
		 * wp videopress activate_cron cleanup
124
		 */
125 View Code Duplication
		public function activate_cron( $args ) {
126
127
			if ( ! isset( $args[0] ) ) {
128
				WP_CLI::error( __( 'You need to provide the name of the cronjob to schedule.', 'jetpack' ) );
129
			}
130
131
			$scheduler = VideoPress_Scheduler::init();
132
133
			if ( ! $scheduler->is_cron_valid( $args[0] ) ) {
134
				return WP_CLI::error( sprintf( __( 'There is no cron named %s.', 'jetpack' ), $args[0] ) );
135
			}
136
137
			$scheduler->activate_cron( $args[0] );
138
139
			WP_CLI::success( sprintf( __( 'The cron named `%s` was scheduled.', 'jetpack' ), $args[0] ) );
140
		}
141
142
		/**
143
		 * Actives the given cron job
144
		 *
145
		 * ## OPTIONS
146
		 *
147
		 * <cron_name>: The name of the cron job to check
148
		 *
149
		 * ## EXAMPLES
150
		 *
151
		 * wp videopress deactivate_cron cleanup
152
		 */
153 View Code Duplication
		public function deactivate_cron( $args ) {
154
155
			if ( ! isset( $args[0] ) ) {
156
				WP_CLI::error( __( 'You need to provide the name of the cronjob to schedule.', 'jetpack' ) );
157
			}
158
159
			$scheduler = VideoPress_Scheduler::init();
160
161
			if ( ! $scheduler->is_cron_valid( $args[0] ) ) {
162
				return WP_CLI::error( sprintf( __( 'There is no cron named %s.', 'jetpack' ), $args[0] ) );
163
			}
164
165
			$scheduler->deactivate_cron( $args[0] );
166
167
			WP_CLI::success( sprintf( __( 'The cron named `%s` was removed from the schedule.', 'jetpack' ), $args[0] ) );
168
		}
169
	}
170
171
	WP_CLI::add_command( 'videopress', 'VideoPress_CLI' );
172
}