Give_Cron   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 171
Duplicated Lines 4.68 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 8
loc 171
rs 10
c 0
b 0
f 0
wmc 15
lcom 2
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_instance() 8 8 2
A setup() 0 4 1
A __add_schedules() 0 9 1
A __schedule_events() 0 4 1
A weekly_events() 0 5 2
A daily_events() 0 5 2
A get_cron_action() 0 13 2
A add_event() 0 4 1
A add_weekly_event() 0 3 1
A add_daily_event() 0 3 1

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
 * Cron
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_Cron
7
 * @copyright   Copyright (c) 2016, GiveWP
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.3.2
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
18
/**
19
 * Give_Cron Class
20
 *
21
 * This class handles scheduled events.
22
 *
23
 * @since 1.3.2
24
 */
25
class Give_Cron {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
26
27
	/**
28
	 * Instance.
29
	 *
30
	 * @since  1.8.13
31
	 * @access private
32
	 * @var
33
	 */
34
	private static $instance;
35
36
	/**
37
	 * Singleton pattern.
38
	 *
39
	 * @since  1.8.13
40
	 * @access private
41
	 */
42
	private function __construct() {
43
	}
44
45
46
	/**
47
	 * Get instance.
48
	 *
49
	 * @since  1.8.13
50
	 * @access public
51
	 * @return static
52
	 */
53 View Code Duplication
	public static function get_instance() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
54
		if ( null === static::$instance ) {
55
			self::$instance = new static();
56
			self::$instance->setup();
57
		}
58
59
		return self::$instance;
60
	}
61
62
63
	/**
64
	 * Setup
65
	 *
66
	 * @since 1.8.13
67
	 */
68
	private function setup() {
69
		add_filter( 'cron_schedules', array( self::$instance, '__add_schedules' ) );
70
		add_action( 'wp', array( self::$instance, '__schedule_events' ) );
71
	}
72
73
	/**
74
	 * Registers new cron schedules
75
	 *
76
	 * @since  1.3.2
77
	 * @access public
78
	 *
79
	 * @param  array $schedules An array of non-default cron schedules.
80
	 *
81
	 * @return array            An array of non-default cron schedules.
82
	 */
83
	public function __add_schedules( $schedules = array() ) {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Cron::__add_schedules" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
84
		// Adds once weekly to the existing schedules.
85
		$schedules['weekly'] = array(
86
			'interval' => 604800,
87
			'display'  => __( 'Once Weekly', 'give' ),
88
		);
89
90
		return $schedules;
91
	}
92
93
	/**
94
	 * Schedules our events
95
	 *
96
	 * @since  1.3.2
97
	 * @access public
98
	 *
99
	 * @return void
100
	 */
101
	public function __schedule_events() {
0 ignored issues
show
Coding Style introduced by
Method name "Give_Cron::__schedule_events" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
102
		$this->weekly_events();
103
		$this->daily_events();
104
	}
105
106
	/**
107
	 * Schedule weekly events
108
	 *
109
	 * @since  1.3.2
110
	 * @access private
111
	 *
112
	 * @return void
113
	 */
114
	private function weekly_events() {
115
		if ( ! wp_next_scheduled( 'give_weekly_scheduled_events' ) ) {
116
			wp_schedule_event( current_time( 'timestamp' ), 'weekly', 'give_weekly_scheduled_events' );
117
		}
118
	}
119
120
	/**
121
	 * Schedule daily events
122
	 *
123
	 * @since  1.3.2
124
	 * @access private
125
	 *
126
	 * @return void
127
	 */
128
	private function daily_events() {
129
		if ( ! wp_next_scheduled( 'give_daily_scheduled_events' ) ) {
130
			wp_schedule_event( current_time( 'timestamp' ), 'daily', 'give_daily_scheduled_events' );
131
		}
132
	}
133
134
	/**
135
	 * get cron job action name
136
	 *
137
	 * @since  1.8.13
138
	 * @access public
139
	 *
140
	 * @param string $type
141
	 *
142
	 * @return string
143
	 */
144
	public static function get_cron_action( $type = 'weekly' ) {
145
		switch ( $type ) {
146
			case 'daily':
147
				$cron_action = 'give_daily_scheduled_events';
148
				break;
149
150
			default:
151
				$cron_action = 'give_weekly_scheduled_events';
152
				break;
153
		}
154
155
		return $cron_action;
156
	}
157
158
	/**
159
	 * Add action to cron action
160
	 *
161
	 * @since  1.8.13
162
	 * @access private
163
	 *
164
	 * @param        $action
165
	 * @param string $type
166
	 */
167
	private static function add_event( $action, $type = 'weekly' ) {
168
		$cron_event = self::get_cron_action( $type );
169
		add_action( $cron_event, $action );
170
	}
171
172
	/**
173
	 * Add weekly event
174
	 *
175
	 * @since  1.8.13
176
	 * @access public
177
	 *
178
	 * @param $action
179
	 */
180
	public static function add_weekly_event( $action ) {
181
		self::add_event( $action, 'weekly' );
182
	}
183
184
	/**
185
	 * Add daily event
186
	 *
187
	 * @since  1.8.13
188
	 * @access public
189
	 *
190
	 * @param $action
191
	 */
192
	public static function add_daily_event( $action ) {
193
		self::add_event( $action, 'daily' );
194
	}
195
}
196
197
// Initiate class.
198
Give_Cron::get_instance();
199