Total Complexity | 43 |
Total Lines | 230 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 0 | Features | 0 |
Complex classes like Cron 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.
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 Cron, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Cron { |
||
18 | |||
19 | /** |
||
20 | * Holds class instance |
||
21 | * |
||
22 | * @since 1.0.0 |
||
23 | * |
||
24 | * @var object|Module_Template |
||
|
|||
25 | */ |
||
26 | protected static $instance = null; |
||
27 | |||
28 | /** |
||
29 | * Initialize the plugin by setting localization, filters, and administration functions. |
||
30 | * |
||
31 | * @since 1.0.0 |
||
32 | * |
||
33 | * @access private |
||
34 | */ |
||
35 | public function __construct() { |
||
36 | add_filter( 'cron_schedules', array( $this, 'register_schedule' ), 10, 1 ); |
||
37 | add_action( 'lsx_wetu_importer_settings_before', array( $this, 'watch_for_trigger' ), 200 ); |
||
38 | add_action( 'lsx_wetu_accommodation_images_cron', array( $this, 'process' ), 10, 1 ); |
||
39 | add_action( 'lsx_wetu_accommodation_images_sync', array( $this, 'cron_callback' ), 10, 1 ); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Return an instance of this class. |
||
44 | * |
||
45 | * @since 1.0.0 |
||
46 | * |
||
47 | * @return object Cron() A single instance of this class. |
||
48 | */ |
||
49 | public static function get_instance() { |
||
50 | // If the single instance hasn't been set, set it now. |
||
51 | if ( null === self::$instance ) { |
||
52 | self::$instance = new self(); |
||
53 | } |
||
54 | return self::$instance; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Registers a 5 min schedule for us to use. |
||
59 | * |
||
60 | * @param array $schedules |
||
61 | * @return array |
||
62 | */ |
||
63 | public function register_schedule( $schedules ) { |
||
64 | $schedules['wetu-5-minutes'] = array( |
||
65 | 'interval' => 5 * MINUTE_IN_SECONDS, |
||
66 | 'display' => __( 'Every 5 minutes', 'lsx-wetu-importer' ), |
||
67 | ); |
||
68 | return $schedules; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Watches for changes in the button triggers. |
||
73 | * |
||
74 | * @return void |
||
75 | */ |
||
76 | public function watch_for_trigger() { |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Remove our cron from the shedule. |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | public function deactivate( $task = 'lsx_wetu_accommodation_images_cron' ) { |
||
109 | wp_clear_scheduled_hook( $task, array( $task ) ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * This function will schedule the cron event. |
||
114 | * |
||
115 | * @param string $task |
||
116 | * @param string $schedule |
||
117 | * @param string $time |
||
118 | * @return void |
||
119 | */ |
||
120 | public function schedule( $task = 'lsx_wetu_accommodation_images_cron', $schedule = 'daily', $time = '' ) { |
||
121 | if ( '' === $time ) { |
||
122 | $time = time(); |
||
123 | } |
||
124 | wp_schedule_event( $time, $schedule, $task, array( $task ) ); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * This is the function that will be triggered by the cron event. |
||
129 | * |
||
130 | * @return void |
||
131 | */ |
||
132 | public function process( $task = '' ) { |
||
133 | switch ( $task ) { |
||
134 | case 'lsx_wetu_accommodation_images_cron': |
||
135 | $this->register_accommodation_images_sync(); |
||
136 | break; |
||
137 | |||
138 | default: |
||
139 | break; |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * This is the function that will be triggered by the cron event. |
||
145 | * |
||
146 | * @return void |
||
147 | */ |
||
148 | public function register_accommodation_images_sync() { |
||
153 | } |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * This is the function that will be triggered by the cron event. |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | public function cron_callback( $task = '' ) { |
||
162 | $has_accommodation = get_option( $task ); |
||
163 | if ( false !== $has_accommodation && ! empty( $has_accommodation ) ) { |
||
164 | $next_time = array_slice( $has_accommodation, 5 ); |
||
165 | $this_time = array_slice( $has_accommodation, 0, 4 ); |
||
166 | |||
167 | $api_key = $this->get_api_key(); |
||
168 | $url = 'https://wetu.com/API/Pins/' . $api_key . '/Get?all=include&ids='; |
||
169 | |||
170 | // Run through the current items. |
||
171 | foreach ( $this_time as $accommodation ) { |
||
172 | $wetu_id = get_post_meta( $accommodation, 'lsx_wetu_id', true ); |
||
173 | $last_date = get_post_meta( $accommodation, 'lsx_wetu_modified_date', true ); |
||
174 | |||
175 | $accommodation_info = wp_remote_get( $url . $wetu_id ); |
||
176 | if ( ! empty( $accommodation_info ) && isset( $accommodation_info['response'] ) && isset( $accommodation_info['response']['code'] ) && 200 === $accommodation_info['response']['code'] ) { |
||
177 | $adata = json_decode( $accommodation_info['body'], true ); |
||
178 | |||
179 | if ( isset( $adata[0] ) && isset( $adata[0]['last_modified'] ) && '' !== $adata[0]['last_modified'] ) { |
||
180 | $modified_time = strtotime( $adata[0]['last_modified'] ); |
||
181 | if ( $modified_time > $last_date ) { |
||
182 | $accommodation_importer = new \LSX_WETU_Importer_Accommodation(); |
||
183 | $accommodation_importer->create_main_gallery( $adata, $accommodation ); |
||
184 | update_post_meta( $accommodation, 'lsx_wetu_modified_date', $modified_time, $last_date ); |
||
185 | } |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | |||
190 | // Save the values for next time. |
||
191 | if ( ! empty( $next_time ) ) { |
||
192 | update_option( $task, $next_time ); |
||
193 | } else { |
||
194 | delete_option( $task ); |
||
195 | $this->deactivate( $task ); |
||
196 | } |
||
197 | } else { |
||
198 | $this->deactivate( $task ); |
||
199 | update_option( 'lsx_wetu_nexttime', $task ); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * This will grab the accommodation ids and load them up into an option field. |
||
205 | * |
||
206 | * @param string $task |
||
207 | * @return void |
||
208 | */ |
||
209 | public function load_items_to_sync( $task = 'accommodation_images' ) { |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Gets the API key stored in the options table. |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public function get_api_key() { |
||
250 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths