Completed
Branch BUG-9140-has-billing-form-igno... (f963e1)
by
unknown
239:42 queued 225:27
created

AttendeesReport::create_job()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 25
rs 8.8571
c 1
b 0
f 1
cc 3
eloc 17
nc 3
nop 1
1
<?php
2
/**
3
 *
4
 * Class {name}
5
 *
6
 * Description here
7
 *
8
 * @package         Event Espresso
9
 * @subpackage    
10
 * @author				Mike Nelson
11
 * @since		 	   4.8.26
12
 *
13
 */
14
15
namespace EventEspressoBatchRequest\JobHandlers;
16
use EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandlerFile;
17
use EventEspressoBatchRequest\Helpers\BatchRequestException;
18
use EventEspressoBatchRequest\Helpers\JobParameters;
19
use EventEspressoBatchRequest\Helpers\JobStepResponse;
20
21
if (!defined('EVENT_ESPRESSO_VERSION')) {
22
	exit('No direct script access allowed');
23
}
24
25
26
class AttendeesReport extends JobHandlerFile {
27
	
28
	
29
	public function create_job(JobParameters $job_parameters) {
30
		if( ! \EE_Capabilities::instance()->current_user_can( 'ee_read_contacts', 'generating_report' ) ) {
31
			throw new BatchRequestException(
32
				__( 'You do not have permission to view contacts', 'event_espresso')
33
			);
34
		}
35
		$filepath = $this->create_file_from_job_with_name(
36
			$job_parameters->job_id(),
37
			__('contact-list-report.csv', 'event_espresso')
38
		);
39
		$job_parameters->add_extra_data( 'filepath', $filepath );
40
		$job_parameters->set_job_size( $this->count_units_to_process() );
41
		//we should also set the header columns
42
		$csv_data_for_row = $this->get_csv_data( 0, 1 );
43
		\EE_Registry::instance()->load_helper( 'Export' );
44
		\EEH_Export::write_data_array_to_csv( $filepath, $csv_data_for_row, true );
45
		//if we actually processed a row there, record it
46
		if( $job_parameters->job_size() ) {
47
			$job_parameters->mark_processed( 1 );
48
		}
49
		return new JobStepResponse(
50
			$job_parameters,
51
			__( 'Contacts report started successfully...', 'event_espresso' )
52
		);
53
	}
54
	
55
56
	public function continue_job(JobParameters $job_parameters, $batch_size = 50) {
57
		$csv_data = $this->get_csv_data( $job_parameters->units_processed(), $batch_size );
58
		\EE_Registry::instance()->load_helper( 'Export' );
59
		\EEH_Export::write_data_array_to_csv( $job_parameters->extra_datum( 'filepath' ), $csv_data, false );
0 ignored issues
show
Bug introduced by
It seems like $job_parameters->extra_datum('filepath') targeting EventEspressoBatchReques...rameters::extra_datum() can also be of type array; however, EEH_Export::write_data_array_to_csv() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
60
		$units_processed = count( $csv_data );
61
		$job_parameters->mark_processed( $units_processed );
62
		$extra_response_data = array(
63
			'file_url' => ''
64
		);
65 View Code Duplication
		if( $units_processed < $batch_size ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
			$job_parameters->set_status( JobParameters::status_complete );
67
			$extra_response_data[ 'file_url' ] = $this->get_url_to_file( $job_parameters->extra_datum( 'filepath' ) );
0 ignored issues
show
Bug introduced by
It seems like $job_parameters->extra_datum('filepath') targeting EventEspressoBatchReques...rameters::extra_datum() can also be of type array; however, EventEspressoBatchReques...File::get_url_to_file() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
68
		}
69
		return new JobStepResponse(
70
				$job_parameters,
71
				sprintf(
72
					__( 'Wrote %1$s rows to report CSV file...', 'event_espresso' ),
73
					count( $csv_data ) ),
74
				$extra_response_data );
75
	}
76
77
	
78 View Code Duplication
	public function cleanup_job(JobParameters $job_parameters) {
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...
79
		$this->_file_helper->delete(
80
			\EEH_File::remove_filename_from_filepath( $job_parameters->extra_datum( 'filepath' ) ),
0 ignored issues
show
Bug introduced by
It seems like $job_parameters->extra_datum('filepath') targeting EventEspressoBatchReques...rameters::extra_datum() can also be of type array; however, EEH_File::remove_filename_from_filepath() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
81
			true,
82
			'd'
83
		);
84
		return new JobStepResponse( $job_parameters, __( 'Cleaned up temporary file', 'event_espresso' ) );
85
	}
86
	
87
	public function count_units_to_process() {
88
		return \EEM_Attendee::instance()->count();
89
	}
90
	public function get_csv_data( $offset, $limit ) {
91
		$attendee_rows = \EEM_Attendee::instance()->get_all_wpdb_results( 
92
				array( 
93
					'limit' => array( $offset, $limit ),
94
					'force_join' => array( 'State', 'Country' ) ) );
95
		$csv_data = array();
96 View Code Duplication
		foreach( $attendee_rows as $attendee_row ){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
97
			$csv_row = array();
98
			foreach( \EEM_Attendee::instance()->field_settings() as $field_name => $field_obj ){
99
				if( $field_name == 'STA_ID' ){
100
					$state_name_field = \EEM_State::instance()->field_settings_for( 'STA_name' );
101
					$csv_row[ __( 'State', 'event_espresso' ) ] = $attendee_row[ $state_name_field->get_qualified_column() ];
102
				}elseif( $field_name == 'CNT_ISO' ){
103
					$country_name_field = \EEM_Country::instance()->field_settings_for( 'CNT_name' );
104
					$csv_row[ __( 'Country', 'event_espresso' ) ] = $attendee_row[ $country_name_field->get_qualified_column() ];
105
				}else{
106
					$csv_row[ $field_obj->get_nicename() ] = $attendee_row[ $field_obj->get_qualified_column() ];
107
				}
108
			}
109
			$csv_data[] = $csv_row;
110
		}
111
		return $csv_data;
112
	}
113
114
}
115