Completed
Branch FET/attendee-importer (b47c55)
by
unknown
34:18 queued 25:45
created

EE_File_Input::__construct()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 62

Duplication

Lines 16
Ratio 25.81 %

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 1
dl 16
loc 62
rs 8.5178
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * EE_File_Input
5
 *
6
 * For uploading a file
7
 * Note: if you will be using this in a form, make sure it uses `enctype="multipart/form-data"`, and that the request
8
 * data is `array_merge($_REQUEST, $_FILES)`.
9
 *
10
 * @package         Event Espresso
11
 * @subpackage
12
 * @author              Mike Nelson
13
 *
14
 * This input has a default validation strategy of plaintext (which can be removed after construction)
15
 */
16
class EE_File_Input extends EE_Form_Input_Base
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $allowed_file_extensions;
22
23
    /**
24
     * @var
25
     */
26
    protected $allowed_mime_types;
27
28
    /**
29
     * @param array $options
30
     * @throws InvalidArgumentException
31
     */
32
    public function __construct($options = array())
33
    {
34 View Code Duplication
        if (isset($options['allowed_file_extensions'])) {
35
            if (!is_array($options['allowed_file_extensions'])) {
36
                throw new InvalidArgumentException(esc_html__('A valid allowed_file_extensions array was not provided to EE_File_Input', 'event_espresso'));
37
            }
38
            $this->allowed_file_extensions = $options['allowed_file_extensions'];
39
        } else {
40
            $this->allowed_file_extensions = ['csv'];
41
        }
42 View Code Duplication
        if (isset($options['allowed_mime_types'])) {
43
            if (!is_array($options['allowed_mime_types'])) {
44
                throw new InvalidArgumentException(esc_html__('A valid allowed_mime_types array was not provided to EE_File_Input', 'event_espresso'));
45
            }
46
            $this->allowed_mime_types = $options['allowed_file_extensions'];
47
        } else {
48
            $this->allowed_mime_types = ['text/csv'];
49
        }
50
51
        $this->_set_display_strategy(new EE_File_Input_Display_Strategy());
52
        $this->_set_normalization_strategy(new EE_File_Normalization());
53
        $this->add_validation_strategy(
54
            new EE_Text_Validation_Strategy(
55
                sprintf(
56
                // translators: %1$s is a list of allowed file extensions.
57
                    esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'),
58
                    implode(', ', $this->allowed_file_extensions)
59
                ),
60
                '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~'
61
            )
62
        );
63
        parent::__construct($options);
64
65
//        It would be great to add this HTML attribute, but jQuery validate chokes on it.
66
        $this->set_other_html_attributes(
67
            $this->other_html_attributes()
68
            . ' extension="'
69
            . implode(
70
                ',',
71
//                array_merge(
72
//                    array_map(
73
//                        function ($mime_type) {
74
//                            if(strpos($mime_type, '/') === false) {
75
//                                return $mime_type . '/*';
76
//                            } else {
77
//                                return $mime_type;
78
//                            }
79
//
80
//                        },
81
//                        $this->allowed_mime_types
82
//                    )
83
                    array_map(
84
                        function ($file_extension) {
85
                            return  $file_extension;
86
                        },
87
                        $this->allowed_file_extensions
88
                    )
89
//                )
90
            )
91
            . '"'
92
        );
93
    }
94
95
    /**
96
     * Takes into account that $_FILES does a weird thing when you have hierarchical form names (eg `<input type="file"
97
     * name="my[hierarchical][form]">`): it leaves the top-level form part alone, but replaces the SECOND part with
98
     * "name", "size", "temp_file", etc. So that file's data is located at "my[name][hierarchical][form]",
99
     * "my[size][hierarchical][form]", "my[temp_name][hierarchical][form]", etc. It's really weird.
100
     * @since $VID:$
101
     * @param array $req_data
102
     * @return array|mixed|NULL
103
     * @throws EE_Error
104
     */
105
    public function find_form_data_for_this_section($req_data)
106
    {
107
        $name_parts = $this->getInputNameParts();
108
        // now get the value for the input
109
        $value = $this->findFileData($name_parts, $req_data);
110
111
        if (empty($value)) {
112
            $value = $this->findFileData($name_parts, $_FILES);
113
        }
114
        if (empty($value)) {
115
            array_shift($name_parts);
116
            // check if this thing's name is at the TOP level of the request data
117
            $value = $this->findFileData($name_parts, $req_data);
118
        }
119
        return $value;
120
    }
121
122
    /**
123
     * Look for the file's data in this request data.
124
     * @since $VID:$
125
     * @param $name_parts
126
     * @param $req_data
127
     * @return array
128
     */
129
    protected function findFileData($name_parts, $req_data)
130
    {
131
        $file_parts = [
132
            'name',
133
            'error',
134
            'size',
135
            'tmp_name',
136
            'type'
137
        ];
138
        $file_data = [];
139
        foreach($file_parts as $file_part){
140
            $datum = $this->findRequestForSectionUsingNameParts($this->getFileDataNameParts($name_parts, $file_part),$req_data);
141
            if(!empty($datum)) {
142
                $file_data[$file_part] = $datum;
143
            }
144
        }
145
        return $file_data;
146
    }
147
148
    /**
149
     * Finds the file name parts for the desired file data.
150
     * @since $VID:$
151
     * @param $original_name_parts
152
     * @param $file_data_sought
153
     * @return array
154
     */
155
    protected function getFileDataNameParts($original_name_parts, $file_data_sought){
156
        return
157
            array_merge(
158
                [
159
                    $original_name_parts[0],
160
                    $file_data_sought
161
                ],
162
            array_slice($original_name_parts, 1)
163
        );
164
    }
165
}
166