Completed
Branch FET/asset-manager (433489)
by
unknown
32:42 queued 18:11
created

AttendeesReport::continue_job()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 19

Duplication

Lines 4
Ratio 15.38 %

Importance

Changes 0
Metric Value
cc 2
eloc 19
nc 2
nop 2
dl 4
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspressoBatchRequest\JobHandlers;
4
5
use EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandlerFile;
6
use EventEspressoBatchRequest\Helpers\BatchRequestException;
7
use EventEspressoBatchRequest\Helpers\JobParameters;
8
use EventEspressoBatchRequest\Helpers\JobStepResponse;
9
10
/**
11
 * Class {name}
12
 * Description here
13
 *
14
 * @package               Event Espresso
15
 * @subpackage
16
 * @author                Mike Nelson
17
 * @since                 4.8.26
18
 */
19
class AttendeesReport extends JobHandlerFile
20
{
21
22
    // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
23
    public function create_job(JobParameters $job_parameters)
24
    {
25
        if (! \EE_Capabilities::instance()->current_user_can('ee_read_contacts', 'generating_report')) {
26
            throw new BatchRequestException(
27
                __('You do not have permission to view contacts', 'event_espresso')
28
            );
29
        }
30
        $filepath = $this->create_file_from_job_with_name(
31
            $job_parameters->job_id(),
32
            __('contact-list-report.csv', 'event_espresso')
33
        );
34
        $job_parameters->add_extra_data('filepath', $filepath);
35
        $job_parameters->set_job_size($this->count_units_to_process());
36
        // we should also set the header columns
37
        $csv_data_for_row = $this->get_csv_data(0, 1);
38
        \EEH_Export::write_data_array_to_csv($filepath, $csv_data_for_row, true);
39
        // if we actually processed a row there, record it
40
        if ($job_parameters->job_size()) {
41
            $job_parameters->mark_processed(1);
42
        }
43
        return new JobStepResponse(
44
            $job_parameters,
45
            __('Contacts report started successfully...', 'event_espresso')
46
        );
47
    }
48
49
50
    public function continue_job(JobParameters $job_parameters, $batch_size = 50)
51
    {
52
        $csv_data = $this->get_csv_data($job_parameters->units_processed(), $batch_size);
53
        \EEH_Export::write_data_array_to_csv(
54
            $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_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...
55
            $csv_data,
56
            false
57
        );
58
        $units_processed = count($csv_data);
59
        $job_parameters->mark_processed($units_processed);
60
        $extra_response_data = array(
61
            'file_url' => '',
62
        );
63 View Code Duplication
        if ($units_processed < $batch_size) {
64
            $job_parameters->set_status(JobParameters::status_complete);
65
            $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...
66
        }
67
        return new JobStepResponse(
68
            $job_parameters,
69
            sprintf(
70
                __('Wrote %1$s rows to report CSV file...', 'event_espresso'),
71
                count($csv_data)
72
            ),
73
            $extra_response_data
74
        );
75
    }
76
77
78 View Code Duplication
    public function cleanup_job(JobParameters $job_parameters)
79
    {
80
        $this->_file_helper->delete(
81
            \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...
82
            true,
83
            'd'
84
        );
85
        return new JobStepResponse($job_parameters, __('Cleaned up temporary file', 'event_espresso'));
86
    }
87
88
    public function count_units_to_process()
89
    {
90
        return \EEM_Attendee::instance()->count(array('caps' => \EEM_Base::caps_read_admin));
91
    }
92
93
    public function get_csv_data($offset, $limit)
94
    {
95
        $attendee_rows = \EEM_Attendee::instance()->get_all_wpdb_results(
96
            array(
97
                'limit'      => array($offset, $limit),
98
                'force_join' => array('State', 'Country'),
99
                'caps'       => \EEM_Base::caps_read_admin,
100
            )
101
        );
102
        $csv_data = array();
103
        foreach ($attendee_rows as $attendee_row) {
104
            $csv_row = array();
105
            foreach (\EEM_Attendee::instance()->field_settings() as $field_name => $field_obj) {
106
                if ($field_name == 'STA_ID') {
107
                    $state_name_field = \EEM_State::instance()->field_settings_for('STA_name');
108
                    $csv_row[ __('State', 'event_espresso') ] = $attendee_row[ $state_name_field->get_qualified_column() ];
109
                } elseif ($field_name == 'CNT_ISO') {
110
                    $country_name_field = \EEM_Country::instance()->field_settings_for('CNT_name');
111
                    $csv_row[ __('Country', 'event_espresso') ] = $attendee_row[ $country_name_field->get_qualified_column() ];
112
                } else {
113
                    $csv_row[ wp_specialchars_decode($field_obj->get_nicename(), ENT_QUOTES) ] = $attendee_row[ $field_obj->get_qualified_column() ];
114
                }
115
            }
116
            $csv_data[] = apply_filters(
117
                'FHEE___EventEspresso_core_libraries_batch_JobHandlers_AttendeesReport__get_csv_data__row',
118
                $csv_row,
119
                $attendee_row
120
            );
121
        }
122
        return $csv_data;
123
    }
124
}
125