Completed
Branch FET/event-question-group-refac... (8c9768)
by
unknown
27:04 queued 17:53
created

EE_File_Input   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 10
loc 85
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 38 3
A find_form_data_for_this_section() 0 8 1
A _sanitize() 0 4 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
use EventEspresso\core\exceptions\InvalidDataTypeException;
4
use EventEspresso\core\exceptions\InvalidInterfaceException;
5
use EventEspresso\core\services\loaders\LoaderFactory;
6
use EventEspresso\core\services\request\files\FileSubmissionInterface;
7
8
/**
9
 * EE_File_Input
10
 *
11
 * For uploading a file
12
 * Note: if you will be using this in a form, make sure it uses `enctype="multipart/form-data"`.
13
 *
14
 * @package         Event Espresso
15
 * @subpackage
16
 * @author              Mike Nelson
17
 *
18
 * This input has a default validation strategy of plaintext (which can be removed after construction)
19
 */
20
class EE_File_Input extends EE_Form_Input_Base
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $allowed_file_extensions;
26
27
    /**
28
     * @var array
29
     */
30
    protected $allowed_mime_types;
31
32
    /**
33
     * @param array $options
34
     * @throws InvalidArgumentException
35
     */
36
    public function __construct($options = array())
37
    {
38 View Code Duplication
        if (isset($options['allowed_file_extensions'])) {
39
            $this->allowed_file_extensions = (array) $options['allowed_file_extensions'];
40
        } else {
41
            $this->allowed_file_extensions = ['csv'];
42
        }
43 View Code Duplication
        if (isset($options['allowed_mime_types'])) {
44
            $this->allowed_mime_types = (array) $options['allowed_file_extensions'];
45
        } else {
46
            $this->allowed_mime_types = ['text/csv'];
47
        }
48
49
        $this->_set_display_strategy(new EE_File_Input_Display_Strategy());
50
        $this->_set_normalization_strategy(new EE_File_Normalization());
51
        $this->add_validation_strategy(
52
            new EE_Text_Validation_Strategy(
53
                sprintf(
54
                    // translators: %1$s is a list of allowed file extensions.
55
                    esc_html__('Please provide a file of the requested filetype: %1$s', 'event_espresso'),
56
                    implode(', ', $this->allowed_file_extensions)
57
                ),
58
                '~.*\.(' . implode('|', $this->allowed_file_extensions) . ')$~'
59
            )
60
        );
61
        parent::__construct($options);
62
63
//        It would be great to add this HTML attribute, but jQuery validate chokes on it.
64
        $this->set_other_html_attributes(
65
            $this->other_html_attributes()
66
            . ' extension="'
67
            . implode(
68
                ',',
69
                $this->allowed_file_extensions
70
            )
71
            . '"'
72
        );
73
    }
74
75
    /**
76
     * $_FILES has a really weird structure. So we let `FilesDataHandler` take care of finding the file info for
77
     * this input.
78
     * @since 4.9.80.p
79
     * @param array $req_data
80
     * @return FileSubmissionInterface
81
     * @throws InvalidArgumentException
82
     * @throws InvalidDataTypeException
83
     * @throws InvalidInterfaceException
84
     */
85
    public function find_form_data_for_this_section($req_data)
86
    {
87
        // ignore $req_data. Files are in the files data handler.
88
        $fileDataHandler = LoaderFactory::getLoader()->getShared(
89
            'EventEspresso\core\services\request\files\FilesDataHandler'
90
        );
91
        return $fileDataHandler->getFileObject($this->html_name());
92
    }
93
94
    /**
95
     * Don't transform the file submission object into a string, thanks.
96
     *
97
     * @param string $value
98
     * @return null|string
99
     */
100
    protected function _sanitize($value)
101
    {
102
        return $value;
103
    }
104
}
105